From 1406aa78c6e2c85cc4ac98e8b33b77519adac06b Mon Sep 17 00:00:00 2001 From: Grzegorz Dlugoszewski Date: Tue, 2 Jun 2020 14:34:10 +0200 Subject: [PATCH] Load gitignore patterns also from files declared in excludesfile --- go.mod | 1 + pkg/status.go | 19 +++++++++++++++++++ pkg/status_test.go | 2 ++ 3 files changed, 22 insertions(+) diff --git a/go.mod b/go.mod index 9844d7c..b7bdf87 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git-get go 1.14 require ( + github.com/go-git/go-billy/v5 v5.0.0 github.com/go-git/go-git/v5 v5.1.0 github.com/karrick/godirwalk v1.15.6 github.com/mitchellh/go-homedir v1.1.0 diff --git a/pkg/status.go b/pkg/status.go index f9534a7..9ea7f3b 100644 --- a/pkg/status.go +++ b/pkg/status.go @@ -4,6 +4,10 @@ import ( "sort" "strings" + "github.com/go-git/go-billy/v5/osfs" + + "github.com/go-git/go-git/v5/plumbing/format/gitignore" + "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/pkg/errors" @@ -28,6 +32,21 @@ func (r *Repo) LoadStatus() error { return errors.Wrap(err, "Failed getting worktree") } + // worktree.Status doesn't load gitignore patterns that may be defined outside of .gitignore file using excludesfile + // We need to load them explicitly here + // TODO: variables are not expanded so if excludesfile is declared like "~/gitignore_global" or "$HOME/gitignore_global", this will fail to open it + globalPatterns, err := gitignore.LoadGlobalPatterns(osfs.New("")) + if err != nil { + return errors.Wrap(err, "Failed loading global gitignore patterns") + } + wt.Excludes = append(wt.Excludes, globalPatterns...) + + systemPatterns, err := gitignore.LoadSystemPatterns(osfs.New("")) + if err != nil { + return errors.Wrap(err, "Failed loading system gitignore patterns") + } + wt.Excludes = append(wt.Excludes, systemPatterns...) + status, err := wt.Status() if err != nil { return errors.Wrap(err, "Failed getting worktree status") diff --git a/pkg/status_test.go b/pkg/status_test.go index e032363..9f9ff8e 100644 --- a/pkg/status_test.go +++ b/pkg/status_test.go @@ -146,3 +146,5 @@ func TestStatus(t *testing.T) { } // TODO: test branch status when tracking a local branch +// TODO: newRepoWithGlobalGitignore +// TODO: newRepoWithGlobalGitignoreSymlink