Skip to content

Commit

Permalink
fix: Load system gitignore patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Nov 27, 2024
1 parent d6eb9a3 commit 47597bb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ func autoDetermineRefs(repo *git.Repository) (from, to string, err error) {
if err != nil {
return "", "", errors.Wrapf(err, "resolving worktree")
}
wt.Excludes, err = domain.ReadSystemGitignore()
if err != nil {
return "", "", errors.Wrapf(err, "reading system gitignore")
}
wt.Filesystem, err = domain.NewBillyFSGitignore(wt.Filesystem)
if err != nil {
return "", "", errors.Wrapf(err, "making overlay ignore")
}
st, err := wt.Status()
if err != nil {
return "", "", errors.Wrapf(err, "querying worktree status")
Expand Down
35 changes: 31 additions & 4 deletions pkg/domain/tree_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package domain
import (
"fmt"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
"github.com/go-git/go-git/v5/plumbing/object"
Expand Down Expand Up @@ -107,13 +108,42 @@ func (g *goGitCommitTree) Reader(path string) (io.ReadCloser, error) {
type goGitIndexTree struct{} // TODO?

// billyFSGitignore intercepts billy.Filesystem.Readdir() calls to filter out gitignore-d files.
//
// TODO: Ignoring worktree directly by gitignore patterns results in invalid diff
// - that is, if git-tracked file is present in a gitignore-d directory and is checked out,
// ignoring that file by overlay will result in a 'deleted' diff.
type billyFSGitignore struct {
billy.Filesystem
m gitignore.Matcher
}

func ReadSystemGitignore() ([]gitignore.Pattern, error) {
rootFs := osfs.New("/")
system, err := gitignore.LoadSystemPatterns(rootFs)
if err != nil {
return nil, err
}
user, err := gitignore.LoadGlobalPatterns(rootFs)
if err != nil {
return nil, err
}
return append(system, user...), nil
}

func appendSystemPatterns(fs billy.Filesystem) ([]gitignore.Pattern, error) {
systemPatterns, err := ReadSystemGitignore()
if err != nil {
return nil, err
}
repoPatterns, err := gitignore.ReadPatterns(fs, nil)
if err != nil {
return nil, err
}
return append(systemPatterns, repoPatterns...), nil
}

func NewBillyFSGitignore(fs billy.Filesystem) (billy.Filesystem, error) {
patterns, err := gitignore.ReadPatterns(fs, nil)
patterns, err := appendSystemPatterns(fs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -141,9 +171,6 @@ type goGitWorktree struct {
}

func newGoGitWorkTree(worktree *git.Worktree, fs billy.Filesystem) (*goGitWorktree, error) {
// TODO: Ignoring worktree directly by gitignore patterns results in invalid diff
// - that is, if git-tracked file is present in a gitignore-d directory and is checked out,
// ignoring that file by overlay will result in a 'deleted' diff.
fs, err := NewBillyFSGitignore(fs)
if err != nil {
return nil, err
Expand Down

0 comments on commit 47597bb

Please sign in to comment.