Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions find_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,18 @@ func (fr *findReplace) WalkDir(f *File) {
// the rename step; the failure is returned so the walker can log it and
// continue with siblings.
func (fr *findReplace) HandleFile(f *File) error {
if f.Base() == ".git" {
// Skip .git whether it is a directory or a worktree/submodule pointer file.
return nil
}

info, err := f.Info()
if err != nil {
return err
}

// If file is a directory, recurse immediately (depth-first).
if info.IsDir() {
// Ignore certain directories
if f.Base() == ".git" {
return nil
}
fr.WalkDir(f)
} else {
// Replace the contents of regular files.
Expand Down
23 changes: 23 additions & 0 deletions find_replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,29 @@ func withWorkingDir(t *testing.T, dir string) {
t.Cleanup(func() { _ = os.Chdir(prev) })
}


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove extra blank line to satisfy gofmt

With this inserted blank line, gofmt -l find_replace.go find_replace_test.go prints find_replace_test.go; the repository instructions require gofmt -l . to print nothing before a PR, so this can fail the formatting check even though the code builds otherwise.

Useful? React with 👍 / 👎.

func TestHandleFileSkipsGitWorktreeFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, ".git")
const original = "gitdir: /path/to/main/.git/worktrees/foo\n"
if err := os.WriteFile(path, []byte(original), 0o644); err != nil {
t.Fatal(err)
}
f := newFileOrFatal(t, path)

fr := findReplace{find: "git", replace: "got"}
if err := fr.HandleFile(f); err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(got) != original {
t.Fatalf("content = %q; want unchanged worktree .git file", got)
}
}

func CloneRepoToTestDir(b *testing.B, repoUrl string) *File {
b.Helper()
d := newTestDir(b, "", "*")
Expand Down