feat: add -e/--regex opt-in pattern mode#101
Conversation
Introduce Pattern with literal mode (QuoteMeta) wired into content and rename paths so capture-group replacement can land without changing CLI behavior today. Fixes dolph#37
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6c2aa4962a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } | ||
|
|
||
| func (p *Pattern) Replace(s string) string { | ||
| return p.re.ReplaceAllString(s, p.replace) |
There was a problem hiding this comment.
Preserve dollar literals in default replacements
In the default non-regex mode, replacements containing $ are now interpreted by regexp.ReplaceAllString as capture expansions, so an invocation like find-replace foo '$1' rewrites each foo to an empty string, and price $5 drops $5. Literal mode previously used strings.ReplaceAll, so these replacement bytes were preserved; this can silently corrupt both file contents and renamed paths unless literal mode uses a literal replacement path.
Useful? React with 👍 / 👎.
| if err != nil { | ||
| return err | ||
| } | ||
| fr.pattern = p |
There was a problem hiding this comment.
Guard lazy pattern initialization across walkers
When findReplace is used directly without run's eager ensurePattern call, as the existing tests and benchmark constructors do, WalkDir starts multiple goroutines and each can call ensurePattern through HandleFile, racing on fr.pattern. This violates the repo's required go test -race ./... discipline and can be fixed by compiling before fan-out or protecting initialization with sync.Once/a mutex.
Useful? React with 👍 / 👎.
| case "-e", "--regex": | ||
| regex = true |
There was a problem hiding this comment.
Document the new regex flag in the README
This adds the -e/--regex CLI surface, but the repo guide explicitly requires updating the README in the same PR whenever a flag or subcommand is added. Without that documentation update, the advertised CLI contract remains stale and users will not see the new mode or its replacement semantics.
Useful? React with 👍 / 👎.
Summary
-e/--regexflag; literal matching remains the defaultPatternengine from Build regex engine with capture-group replacement support #37Notes
--rename-onlyand--content-onlyscope flags #36 (scope flags) per issue Add-e/--regexopt-in mode — triggers v2.0.0 release #38.Fixes #38
Test plan
go test ./...TestRun_RegexModeRewritesContent—\d+→NUMTestRun_RegexModeRenamesFiles—(\w+)_test\.go→$1.test.goTestRun_InvalidRegexExitsNonZeroMade with Cursor