From 5c202b9c66be4046e4d4ef12bb55428db720d088 Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Mon, 3 Jun 2024 19:10:47 +0000 Subject: [PATCH] git-generate: fix file tree walking to work with git worktrees 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. --- git-generate/main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/git-generate/main.go b/git-generate/main.go index af34479..f0509d9 100644 --- a/git-generate/main.go +++ b/git-generate/main.go @@ -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. @@ -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