From ad7575e85ebf6332d324ca5997db18ea81065df4 Mon Sep 17 00:00:00 2001 From: Bernhard Guillon Date: Mon, 6 Jul 2026 21:36:29 +0200 Subject: Implement nested .gitignore support with GitignoreStack - Replaced go-gitignore with custom GitignoreStack that tracks multiple .gitignore files hierarchically - Patterns are processed in root-to-leaf order, last match wins - Negation patterns in child dirs override parent ignores - Patterns only apply within their directory scope (no leaking) - Added 6 new tests for nested .gitignore: - Scoped: patterns only affect own directory - Dir-scoped: different dirs can ignore different things - Negation override: child !pattern un-ignores parent's *.pattern - Deep nesting: 3+ levels of .gitignore - Parent scope doesn't leak to siblings - All 35 tests passing --- sync.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'sync.go') diff --git a/sync.go b/sync.go index 293f20b..526e143 100644 --- a/sync.go +++ b/sync.go @@ -5,22 +5,19 @@ import ( "log" "os" "path/filepath" - "strings" - - ignore "github.com/sabhiram/go-gitignore" ) type FileMeta struct { - Size int64 - Mode os.FileMode - Symlink string + Size int64 + Mode os.FileMode + Symlink string } type Manifest struct { Files map[string]FileMeta } -func initialSync(hostDir, dataDir string, rules *ignore.GitIgnore, dryRun bool) (*Manifest, error) { +func initialSync(hostDir, dataDir string, stack *GitignoreStack, dryRun bool) (*Manifest, error) { manifest := &Manifest{Files: make(map[string]FileMeta)} err := filepath.Walk(hostDir, func(path string, info os.FileInfo, err error) error { @@ -37,11 +34,18 @@ func initialSync(hostDir, dataDir string, rules *ignore.GitIgnore, dryRun bool) return nil } - if relPath == ".git" || strings.HasPrefix(relPath, ".git"+string(filepath.Separator)) { - return filepath.SkipDir + if isGitDir(relPath) { + if info.IsDir() { + return filepath.SkipDir + } + return nil + } + + if info.IsDir() && hasGitignore(path) { + stack.Push(path) } - if rules.MatchesPath(relPath) { + if stack.IsIgnored(relPath) { if info.IsDir() { return filepath.SkipDir } -- cgit v1.2.3