Skip to content

Commit

Permalink
git-generate: fix file tree walking to work with git worktrees
Browse files Browse the repository at this point in the history
Currently git-generate snapshots the git directory before it makes
changes so it can identify newly-created files.

The file tree walking it performs explicitly ignores the .git directory,
but sometimes the .git directory is actually a file, like when git
worktrees are in use. In this case, the file tree walking code will
actually return fs.SkipDir through the callback passed to
filepath.WalkDir and will skip iterating over the rest of directories in
the repository.

Fix this by only returning fs.SkipDir only when .git is a directory.
  • Loading branch information
mknyszek committed Jun 3, 2024
1 parent a0ef209 commit 5c202b9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion git-generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.16
// +build go1.16

// Git-generate regenerates a commit from a script kept in the commit message.
Expand Down Expand Up @@ -221,7 +222,14 @@ func gitDir(dir string, args ...string) string {
func walkGit(dir string, f func(path string)) {
filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if d.Name() == ".git" {
return fs.SkipDir
// .git is usually a directory, in which case we want to skip it.
// But in some cases it may be a file (git worktrees), so we need
// to be careful not to return fs.SkipDir or else we'll skip .git's
// parent, which is usually the repository root.
if d.IsDir() {
return fs.SkipDir
}
return nil
}
if d.IsDir() {
return nil
Expand Down

0 comments on commit 5c202b9

Please sign in to comment.