Skip to content

Commit

Permalink
pkg/diff: respect global .gitignore (#25)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
lmb committed Aug 28, 2023
1 parent 977855c commit 9f8ae1c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/diff/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
"golang.org/x/exp/apidiff"
"golang.org/x/tools/go/packages"

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

rootFS, err := osfs.New("/")
if err != nil {
return nil, fmt.Errorf("failed to create root filesystem interface: %v", err)
}

globalIgnores, err := gitignore.LoadGlobalPatterns(rootFS)
if err != nil {
return nil, fmt.Errorf("failed to load global gitignore: %v", err)
}
wt.Excludes = append(wt.Excludes, globalIgnores...)

systemIgnores, err := gitignore.LoadSystemPatterns(rootFS)
if err != nil {
return nil, fmt.Errorf("failed to load system gitignore: %v", err)
}
wt.Excludes = append(wt.Excludes, systemIgnores...)

if stat, err := wt.Status(); err != nil {
return nil, fmt.Errorf("failed to get git status: %w", err)
} else if !stat.IsClean() {
Expand Down

0 comments on commit 9f8ae1c

Please sign in to comment.