Skip to content

Commit 9f8ae1c

Browse files
authored
pkg/diff: respect global .gitignore (#25)
Respect patterns from ~/.gitignore, /etc/gitconfig or core.excludesFile if present. This avoids the "current git tree is dirty" in some circumstances. Fixes #20 Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
1 parent 977855c commit 9f8ae1c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pkg/diff/run.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/go-git/go-git/v5"
2727
"github.com/go-git/go-git/v5/plumbing"
28+
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
2829
"golang.org/x/exp/apidiff"
2930
"golang.org/x/tools/go/packages"
3031

@@ -57,6 +58,23 @@ func Run(opts Options) (*Diff, error) {
5758
return nil, fmt.Errorf("failed to set worktree filesystem interface: %v", err)
5859
}
5960

61+
rootFS, err := osfs.New("/")
62+
if err != nil {
63+
return nil, fmt.Errorf("failed to create root filesystem interface: %v", err)
64+
}
65+
66+
globalIgnores, err := gitignore.LoadGlobalPatterns(rootFS)
67+
if err != nil {
68+
return nil, fmt.Errorf("failed to load global gitignore: %v", err)
69+
}
70+
wt.Excludes = append(wt.Excludes, globalIgnores...)
71+
72+
systemIgnores, err := gitignore.LoadSystemPatterns(rootFS)
73+
if err != nil {
74+
return nil, fmt.Errorf("failed to load system gitignore: %v", err)
75+
}
76+
wt.Excludes = append(wt.Excludes, systemIgnores...)
77+
6078
if stat, err := wt.Status(); err != nil {
6179
return nil, fmt.Errorf("failed to get git status: %w", err)
6280
} else if !stat.IsClean() {

0 commit comments

Comments
 (0)