diff --git a/internal/utils/git_ignore.go b/internal/utils/git_ignore.go index 4902e86..6be4ce6 100644 --- a/internal/utils/git_ignore.go +++ b/internal/utils/git_ignore.go @@ -27,6 +27,9 @@ func NewGitIgnore(baseDir string) GitIgnore { } func (i GitIgnore) SkipFile(path string) (bool, error) { + path = strings.TrimPrefix(path, i.baseDir) + path = strings.TrimPrefix(path, "/") + // Never skip the .git directory or files inside it, even if matched by .gitignore patterns. if path == ".git" || strings.HasPrefix(path, ".git/") || diff --git a/internal/utils/git_ignore_test.go b/internal/utils/git_ignore_test.go index a1f3635..5e0a776 100644 --- a/internal/utils/git_ignore_test.go +++ b/internal/utils/git_ignore_test.go @@ -69,6 +69,27 @@ func TestGitIgnore(t *testing.T) { }) } }) + + t.Run("match only paths within baseDir, not its parent directories", func(t *testing.T) { + backup := setupGlobalGitIgnore(t, "var/\nignore.txt\n") + defer func() { + if backup.originalPath == "" { + unsetGlobalGitIgnoreConfig(t) + } + backup.Restore(t) + }() + + baseDir := filepath.Join(t.TempDir(), "var", "home", "user", "repo") + err := os.MkdirAll(baseDir, 0755) + assert.NoError(t, err) + writeFile(t, filepath.Join(baseDir, "not-ignore.txt"), "not-ignore") + writeFile(t, filepath.Join(baseDir, "ignore.txt"), "ignore") + + gitIgnore := NewGitIgnore(baseDir) + + assertFileNotSkipped(t, &gitIgnore, filepath.Join(baseDir, "not-ignore.txt")) + assertFileSkipped(t, &gitIgnore, filepath.Join(baseDir, "ignore.txt")) + }) } func assertFileSkipped(t *testing.T, gitIgnore *GitIgnore, path string) {