Skip to content
Merged
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
8 changes: 8 additions & 0 deletions internal/utils/git_ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func NewGitIgnore(baseDir string) GitIgnore {
}

func (i GitIgnore) SkipFile(path string) (bool, error) {
// Never skip the .git directory or files inside it, even if matched by .gitignore patterns.
if path == ".git" ||
strings.HasPrefix(path, ".git/") ||
strings.HasSuffix(path, "/.git") ||
strings.Contains(path, "/.git/") {
return false, nil
}

for _, ignorer := range []*ignore.GitIgnore{i.localGitIgnore, i.globalGitIgnore, i.gitInfoExclude} {
if ignorer != nil && ignorer.MatchesPath(path) {
return true, nil
Expand Down
22 changes: 22 additions & 0 deletions internal/utils/git_ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ func TestGitIgnore(t *testing.T) {
assertFileSkipped(t, &gitIgnore, "ignore/this/file.txt")
assertFileNotSkipped(t, &gitIgnore, "some/other/file.txt")
})

t.Run("never skip .git directory or files inside it", func(t *testing.T) {
tmpRepoDir := t.TempDir()
writeFile(t, filepath.Join(tmpRepoDir, ".gitignore"), ".git\n.git/*\n") // would match .git if we didn't special-case it
gitIgnore := NewGitIgnore(tmpRepoDir)

pathsThatMustNotBeSkipped := []string{
".git",
".git/HEAD",
".git/config",
".git/refs/heads/main",
"/Users/example/repo/.git",
"repo/.git",
"subdir/repo/.git/refs",
}
for _, path := range pathsThatMustNotBeSkipped {
path := path
t.Run(path, func(t *testing.T) {
assertFileNotSkipped(t, &gitIgnore, path)
})
}
})
}

func assertFileSkipped(t *testing.T, gitIgnore *GitIgnore, path string) {
Expand Down