From 963f6f79768584987dd95299258911bbc88aaba2 Mon Sep 17 00:00:00 2001 From: "Alan D. Cabrera" Date: Mon, 7 May 2018 07:09:59 -0700 Subject: [PATCH 1/8] Provide ability to add excludes to worktree Adding excludes to the worktree seemed to be the best course of action since it minimizes the amount of code changes; neatly sidestepping the issue of ballooning constructors. It also seems to be philosophically in line w/ other code. For example, there is no code to obtain the username and email from the ~/.gitconfig file and yet those two pieces of information are needed for commits, etc. With that said, convenience functions, LoadGlobalPatterns() and LoadGlobalPatterns(), are provided to obtain any excludes that may be declared in ~/.gitconfig and /etc/gitconfig respectively. Signed-off-by: Alan D. Cabrera --- plumbing/format/gitignore/dir.go | 87 ++++++++++++- plumbing/format/gitignore/dir_test.go | 170 +++++++++++++++++++++++++- worktree.go | 4 + worktree_status.go | 3 + worktree_test.go | 30 +++++ 5 files changed, 287 insertions(+), 7 deletions(-) diff --git a/plumbing/format/gitignore/dir.go b/plumbing/format/gitignore/dir.go index 41dd62497..1e88970ef 100644 --- a/plumbing/format/gitignore/dir.go +++ b/plumbing/format/gitignore/dir.go @@ -1,24 +1,31 @@ package gitignore import ( + "bytes" "io/ioutil" "os" + "os/user" "strings" "gopkg.in/src-d/go-billy.v4" + "gopkg.in/src-d/go-git.v4/plumbing/format/config" + gioutil "gopkg.in/src-d/go-git.v4/utils/ioutil" ) const ( commentPrefix = "#" + coreSection = "core" eol = "\n" + excludesfile = "excludesfile" gitDir = ".git" gitignoreFile = ".gitignore" + gitconfigFile = ".gitconfig" + systemFile = "/etc/gitconfig" ) -// ReadPatterns reads gitignore patterns recursively traversing through the directory -// structure. The result is in the ascending order of priority (last higher). -func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error) { - f, err := fs.Open(fs.Join(append(path, gitignoreFile)...)) +// readIgnoreFile reads a specific git ignore file. +func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps []Pattern, err error) { + f, err := fs.Open(fs.Join(append(path, ignoreFile)...)) if err == nil { defer f.Close() @@ -33,6 +40,14 @@ func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error) return nil, err } + return +} + +// ReadPatterns reads gitignore patterns recursively traversing through the directory +// structure. The result is in the ascending order of priority (last higher). +func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error) { + ps, _ = readIgnoreFile(fs, path, gitignoreFile) + var fis []os.FileInfo fis, err = fs.ReadDir(fs.Join(path...)) if err != nil { @@ -55,3 +70,67 @@ func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error) return } + +func loadPatterns(fs billy.Filesystem, path string) (ps []Pattern, err error) { + f, err := fs.Open(path) + if err != nil { + if os.IsNotExist(err) { + return nil, nil + } + return nil, err + } + + defer gioutil.CheckClose(f, &err) + + b, err := ioutil.ReadAll(f) + if err != nil { + return + } + + d := config.NewDecoder(bytes.NewBuffer(b)) + + raw := config.New() + if err = d.Decode(raw); err != nil { + return + } + + s := raw.Section(coreSection) + efo := s.Options.Get(excludesfile) + if efo == "" { + return nil, nil + } + + ps, err = readIgnoreFile(fs, nil, efo) + if os.IsNotExist(err) { + return nil, nil + } + + return +} + +// LoadGlobalPatterns loads gitignore patterns from from the gitignore file +// declared in a user's ~/.gitconfig file. If the ~/.gitconfig file does not +// exist the function will return nil. If the core.excludesfile property +// is not declared, the function will return nil. If the file pointed to by +// the core.excludesfile property does not exist, the function will return nil. +// +// The function assumes fs is rooted at the root filesystem. +func LoadGlobalPatterns(fs billy.Filesystem) (ps []Pattern, err error) { + usr, err := user.Current() + if err != nil { + return + } + + return loadPatterns(fs, fs.Join(usr.HomeDir, gitconfigFile)) +} + +// LoadSystemPatterns loads gitignore patterns from from the gitignore file +// declared in a system's /etc/gitconfig file. If the ~/.gitconfig file does +// not exist the function will return nil. If the core.excludesfile property +// is not declared, the function will return nil. If the file pointed to by +// the core.excludesfile property does not exist, the function will return nil. +// +// The function assumes fs is rooted at the root filesystem. +func LoadSystemPatterns(fs billy.Filesystem) (ps []Pattern, err error) { + return loadPatterns(fs, systemFile) +} diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go index b8a545317..03ed44724 100644 --- a/plumbing/format/gitignore/dir_test.go +++ b/plumbing/format/gitignore/dir_test.go @@ -2,6 +2,7 @@ package gitignore import ( "os" + "os/user" . "gopkg.in/check.v1" "gopkg.in/src-d/go-billy.v4" @@ -9,12 +10,21 @@ import ( ) type MatcherSuite struct { - FS billy.Filesystem + GFS billy.Filesystem // git repository root + + RFS billy.Filesystem // root that contains user home + MCFS billy.Filesystem // root that contains user home, but missing ~/.gitconfig + MEFS billy.Filesystem // root that contains user home, but missing excludesfile entry + MIFS billy.Filesystem // root that contains user home, but missing .gitnignore + + SFS billy.Filesystem // root that contains /etc/gitconfig } var _ = Suite(&MatcherSuite{}) func (s *MatcherSuite) SetUpTest(c *C) { + + // setup generic git repository root fs := memfs.New() f, err := fs.Create(".gitignore") c.Assert(err, IsNil) @@ -36,11 +46,127 @@ func (s *MatcherSuite) SetUpTest(c *C) { fs.MkdirAll("vendor/github.com", os.ModePerm) fs.MkdirAll("vendor/gopkg.in", os.ModePerm) - s.FS = fs + s.GFS = fs + + // setup root that contains user home + usr, err := user.Current() + c.Assert(err, IsNil) + + fs = memfs.New() + err = fs.MkdirAll(usr.HomeDir, os.ModePerm) + c.Assert(err, IsNil) + + f, err = fs.Create(fs.Join(usr.HomeDir, gitconfigFile)) + c.Assert(err, IsNil) + _, err = f.Write([]byte("[core]\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte(" excludesfile = " + fs.Join(usr.HomeDir, ".gitignore_global"))) + c.Assert(err, IsNil) + err = f.Close() + c.Assert(err, IsNil) + + f, err = fs.Create(fs.Join(usr.HomeDir, ".gitignore_global")) + c.Assert(err, IsNil) + _, err = f.Write([]byte("# IntelliJ\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte(".idea/\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte("*.iml\n")) + c.Assert(err, IsNil) + err = f.Close() + c.Assert(err, IsNil) + + s.RFS = fs + + // root that contains user home, but missing ~/.gitconfig + fs = memfs.New() + err = fs.MkdirAll(usr.HomeDir, os.ModePerm) + c.Assert(err, IsNil) + + f, err = fs.Create(fs.Join(usr.HomeDir, ".gitignore_global")) + c.Assert(err, IsNil) + _, err = f.Write([]byte("# IntelliJ\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte(".idea/\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte("*.iml\n")) + c.Assert(err, IsNil) + err = f.Close() + c.Assert(err, IsNil) + + s.MCFS = fs + + // setup root that contains user home, but missing excludesfile entry + fs = memfs.New() + err = fs.MkdirAll(usr.HomeDir, os.ModePerm) + c.Assert(err, IsNil) + + f, err = fs.Create(fs.Join(usr.HomeDir, gitconfigFile)) + c.Assert(err, IsNil) + _, err = f.Write([]byte("[core]\n")) + c.Assert(err, IsNil) + err = f.Close() + c.Assert(err, IsNil) + + f, err = fs.Create(fs.Join(usr.HomeDir, ".gitignore_global")) + c.Assert(err, IsNil) + _, err = f.Write([]byte("# IntelliJ\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte(".idea/\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte("*.iml\n")) + c.Assert(err, IsNil) + err = f.Close() + c.Assert(err, IsNil) + + s.MEFS = fs + + // setup root that contains user home, but missing .gitnignore + fs = memfs.New() + err = fs.MkdirAll(usr.HomeDir, os.ModePerm) + c.Assert(err, IsNil) + + f, err = fs.Create(fs.Join(usr.HomeDir, gitconfigFile)) + c.Assert(err, IsNil) + _, err = f.Write([]byte("[core]\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte(" excludesfile = " + fs.Join(usr.HomeDir, ".gitignore_global"))) + c.Assert(err, IsNil) + err = f.Close() + c.Assert(err, IsNil) + + s.MIFS = fs + + // setup root that contains user home + fs = memfs.New() + err = fs.MkdirAll("/etc", os.ModePerm) + c.Assert(err, IsNil) + + f, err = fs.Create(systemFile) + c.Assert(err, IsNil) + _, err = f.Write([]byte("[core]\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte(" excludesfile = /etc/gitignore_global")) + c.Assert(err, IsNil) + err = f.Close() + c.Assert(err, IsNil) + + f, err = fs.Create("/etc/gitignore_global") + c.Assert(err, IsNil) + _, err = f.Write([]byte("# IntelliJ\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte(".idea/\n")) + c.Assert(err, IsNil) + _, err = f.Write([]byte("*.iml\n")) + c.Assert(err, IsNil) + err = f.Close() + c.Assert(err, IsNil) + + s.SFS = fs } func (s *MatcherSuite) TestDir_ReadPatterns(c *C) { - ps, err := ReadPatterns(s.FS, nil) + ps, err := ReadPatterns(s.GFS, nil) c.Assert(err, IsNil) c.Assert(ps, HasLen, 2) @@ -48,3 +174,41 @@ func (s *MatcherSuite) TestDir_ReadPatterns(c *C) { c.Assert(m.Match([]string{"vendor", "gopkg.in"}, true), Equals, true) c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false) } + +func (s *MatcherSuite) TestDir_LoadGlobalPatterns(c *C) { + ps, err := LoadGlobalPatterns(s.RFS) + c.Assert(err, IsNil) + c.Assert(ps, HasLen, 2) + + m := NewMatcher(ps) + c.Assert(m.Match([]string{"go-git.v4.iml"}, true), Equals, true) + c.Assert(m.Match([]string{".idea"}, true), Equals, true) +} + +func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingGitconfig(c *C) { + ps, err := LoadGlobalPatterns(s.MCFS) + c.Assert(err, IsNil) + c.Assert(ps, HasLen, 0) +} + +func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingExcludesfile(c *C) { + ps, err := LoadGlobalPatterns(s.MEFS) + c.Assert(err, IsNil) + c.Assert(ps, HasLen, 0) +} + +func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingGitignore(c *C) { + ps, err := LoadGlobalPatterns(s.MIFS) + c.Assert(err, IsNil) + c.Assert(ps, HasLen, 0) +} + +func (s *MatcherSuite) TestDir_LoadSystemPatterns(c *C) { + ps, err := LoadSystemPatterns(s.SFS) + c.Assert(err, IsNil) + c.Assert(ps, HasLen, 2) + + m := NewMatcher(ps) + c.Assert(m.Match([]string{"go-git.v4.iml"}, true), Equals, true) + c.Assert(m.Match([]string{".idea"}, true), Equals, true) +} diff --git a/worktree.go b/worktree.go index d2cb29ad1..aeb011a67 100644 --- a/worktree.go +++ b/worktree.go @@ -13,6 +13,7 @@ import ( "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/filemode" + "gopkg.in/src-d/go-git.v4/plumbing/format/gitignore" "gopkg.in/src-d/go-git.v4/plumbing/format/index" "gopkg.in/src-d/go-git.v4/plumbing/object" "gopkg.in/src-d/go-git.v4/plumbing/storer" @@ -34,6 +35,9 @@ type Worktree struct { // Filesystem underlying filesystem. Filesystem billy.Filesystem + // External excludes not found in the repository .gitignore + Excludes []gitignore.Pattern + r *Repository } diff --git a/worktree_status.go b/worktree_status.go index b5f2381e3..0e113d093 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -145,6 +145,9 @@ func (w *Worktree) excludeIgnoredChanges(changes merkletrie.Changes) merkletrie. if err != nil || len(patterns) == 0 { return changes } + + patterns = append(patterns, w.Excludes...) + m := gitignore.NewMatcher(patterns) var res merkletrie.Changes diff --git a/worktree_test.go b/worktree_test.go index 05a205aa0..df191b0a0 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -15,6 +15,7 @@ import ( "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/filemode" + "gopkg.in/src-d/go-git.v4/plumbing/format/gitignore" "gopkg.in/src-d/go-git.v4/plumbing/format/index" "gopkg.in/src-d/go-git.v4/plumbing/object" "gopkg.in/src-d/go-git.v4/storage/memory" @@ -1072,6 +1073,35 @@ func (s *WorktreeSuite) TestAddUntracked(c *C) { c.Assert(obj.Size(), Equals, int64(3)) } +func (s *WorktreeSuite) TestIgnored(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + w.Excludes = make([]gitignore.Pattern, 0) + w.Excludes = append(w.Excludes, gitignore.ParsePattern("foo", nil)) + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + idx, err := w.r.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx.Entries, HasLen, 9) + + err = util.WriteFile(w.Filesystem, "foo", []byte("FOO"), 0755) + c.Assert(err, IsNil) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 0) + + file := status.File("foo") + c.Assert(file.Staging, Equals, Untracked) + c.Assert(file.Worktree, Equals, Untracked) +} + func (s *WorktreeSuite) TestAddModified(c *C) { fs := memfs.New() w := &Worktree{ From 1a0dc78edbe475f7ffc848408c77f36a335a5a70 Mon Sep 17 00:00:00 2001 From: "Alan D. Cabrera" Date: Tue, 8 May 2018 09:06:38 -0700 Subject: [PATCH 2/8] Whitespace commit Try to get AppVeyor to re-run tests. Signed-off-by: Alan D. Cabrera --- plumbing/format/gitignore/dir_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go index 03ed44724..5865db011 100644 --- a/plumbing/format/gitignore/dir_test.go +++ b/plumbing/format/gitignore/dir_test.go @@ -23,7 +23,6 @@ type MatcherSuite struct { var _ = Suite(&MatcherSuite{}) func (s *MatcherSuite) SetUpTest(c *C) { - // setup generic git repository root fs := memfs.New() f, err := fs.Create(".gitignore") From 0b193874d1abbb8805a91d003c3fc8137ae21b3f Mon Sep 17 00:00:00 2001 From: "Alan D. Cabrera" Date: Tue, 8 May 2018 09:36:09 -0700 Subject: [PATCH 3/8] Add missing newlines in tests Signed-off-by: Alan D. Cabrera --- plumbing/format/gitignore/dir_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go index 5865db011..b08062f78 100644 --- a/plumbing/format/gitignore/dir_test.go +++ b/plumbing/format/gitignore/dir_test.go @@ -59,7 +59,7 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = " + fs.Join(usr.HomeDir, ".gitignore_global"))) + _, err = f.Write([]byte(" excludesfile = " + fs.Join(usr.HomeDir, ".gitignore_global") + "\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) @@ -129,7 +129,7 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = " + fs.Join(usr.HomeDir, ".gitignore_global"))) + _, err = f.Write([]byte(" excludesfile = " + fs.Join(usr.HomeDir, ".gitignore_global") + "\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) @@ -145,7 +145,7 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = /etc/gitignore_global")) + _, err = f.Write([]byte(" excludesfile = /etc/gitignore_global\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) @@ -174,7 +174,7 @@ func (s *MatcherSuite) TestDir_ReadPatterns(c *C) { c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false) } -func (s *MatcherSuite) TestDir_LoadGlobalPatterns(c *C) { +func (s *MatcherSuite) TestDir_LoadGlobalPatterns(c *C) { // F ps, err := LoadGlobalPatterns(s.RFS) c.Assert(err, IsNil) c.Assert(ps, HasLen, 2) @@ -196,7 +196,7 @@ func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingExcludesfile(c *C) { c.Assert(ps, HasLen, 0) } -func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingGitignore(c *C) { +func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingGitignore(c *C) { // F ps, err := LoadGlobalPatterns(s.MIFS) c.Assert(err, IsNil) c.Assert(ps, HasLen, 0) From 455265efe0d71b59cc03abdadf5df2d9f32496c0 Mon Sep 17 00:00:00 2001 From: "Alan D. Cabrera" Date: Tue, 8 May 2018 09:43:32 -0700 Subject: [PATCH 4/8] Add debug messages for failed tests Signed-off-by: Alan D. Cabrera --- plumbing/format/gitignore/dir_test.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go index b08062f78..34599301e 100644 --- a/plumbing/format/gitignore/dir_test.go +++ b/plumbing/format/gitignore/dir_test.go @@ -1,6 +1,7 @@ package gitignore import ( + "fmt" "os" "os/user" @@ -174,8 +175,13 @@ func (s *MatcherSuite) TestDir_ReadPatterns(c *C) { c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false) } -func (s *MatcherSuite) TestDir_LoadGlobalPatterns(c *C) { // F +func (s *MatcherSuite) TestDir_LoadGlobalPatterns(c *C) { ps, err := LoadGlobalPatterns(s.RFS) + if err != nil { + usr, err := user.Current() + c.Assert(err, IsNil) + fmt.Println(" excludesfile = " + s.RFS.Join(usr.HomeDir, ".gitignore_global") + "\n") + } c.Assert(err, IsNil) c.Assert(ps, HasLen, 2) @@ -196,8 +202,13 @@ func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingExcludesfile(c *C) { c.Assert(ps, HasLen, 0) } -func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingGitignore(c *C) { // F +func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingGitignore(c *C) { ps, err := LoadGlobalPatterns(s.MIFS) + if err != nil { + usr, err := user.Current() + c.Assert(err, IsNil) + fmt.Println(" excludesfile = " + s.RFS.Join(usr.HomeDir, ".gitignore_global") + "\n") + } c.Assert(err, IsNil) c.Assert(ps, HasLen, 0) } From 5260d9a30fb41b66c4cd8375277ce138ea551e00 Mon Sep 17 00:00:00 2001 From: "Alan D. Cabrera" Date: Tue, 8 May 2018 10:01:38 -0700 Subject: [PATCH 5/8] Add proper handling of filenames in tests Signed-off-by: Alan D. Cabrera --- plumbing/format/gitignore/dir_test.go | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go index 34599301e..37602f4d3 100644 --- a/plumbing/format/gitignore/dir_test.go +++ b/plumbing/format/gitignore/dir_test.go @@ -1,7 +1,6 @@ package gitignore import ( - "fmt" "os" "os/user" @@ -60,7 +59,7 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = " + fs.Join(usr.HomeDir, ".gitignore_global") + "\n")) + _, err = f.Write([]byte(" excludesfile = \"" + fs.Join(usr.HomeDir, ".gitignore_global") + "\"\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) @@ -130,7 +129,7 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = " + fs.Join(usr.HomeDir, ".gitignore_global") + "\n")) + _, err = f.Write([]byte(" excludesfile = \"" + fs.Join(usr.HomeDir, ".gitignore_global") + "\"\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) @@ -139,19 +138,19 @@ func (s *MatcherSuite) SetUpTest(c *C) { // setup root that contains user home fs = memfs.New() - err = fs.MkdirAll("/etc", os.ModePerm) + err = fs.MkdirAll("etc", os.ModePerm) c.Assert(err, IsNil) f, err = fs.Create(systemFile) c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = /etc/gitignore_global\n")) + _, err = f.Write([]byte(" excludesfile = \"" + fs.Join("etc", "gitignore_global") + "\"\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) - f, err = fs.Create("/etc/gitignore_global") + f, err = fs.Create(fs.Join("etc", "gitignore_global")) c.Assert(err, IsNil) _, err = f.Write([]byte("# IntelliJ\n")) c.Assert(err, IsNil) @@ -177,11 +176,6 @@ func (s *MatcherSuite) TestDir_ReadPatterns(c *C) { func (s *MatcherSuite) TestDir_LoadGlobalPatterns(c *C) { ps, err := LoadGlobalPatterns(s.RFS) - if err != nil { - usr, err := user.Current() - c.Assert(err, IsNil) - fmt.Println(" excludesfile = " + s.RFS.Join(usr.HomeDir, ".gitignore_global") + "\n") - } c.Assert(err, IsNil) c.Assert(ps, HasLen, 2) @@ -204,11 +198,6 @@ func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingExcludesfile(c *C) { func (s *MatcherSuite) TestDir_LoadGlobalPatternsMissingGitignore(c *C) { ps, err := LoadGlobalPatterns(s.MIFS) - if err != nil { - usr, err := user.Current() - c.Assert(err, IsNil) - fmt.Println(" excludesfile = " + s.RFS.Join(usr.HomeDir, ".gitignore_global") + "\n") - } c.Assert(err, IsNil) c.Assert(ps, HasLen, 0) } From 9633f6f1a0aecad5cb430c8a1e86f4b14ced6a11 Mon Sep 17 00:00:00 2001 From: "Alan D. Cabrera" Date: Tue, 8 May 2018 10:08:15 -0700 Subject: [PATCH 6/8] Fix quoting of file names in tests Signed-off-by: Alan D. Cabrera --- plumbing/format/gitignore/dir_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go index 37602f4d3..08cf86bbf 100644 --- a/plumbing/format/gitignore/dir_test.go +++ b/plumbing/format/gitignore/dir_test.go @@ -129,7 +129,7 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = \"" + fs.Join(usr.HomeDir, ".gitignore_global") + "\"\n")) + _, err = f.Write([]byte(" excludesfile = '" + fs.Join(usr.HomeDir, ".gitignore_global") + "'\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) @@ -145,7 +145,7 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = \"" + fs.Join("etc", "gitignore_global") + "\"\n")) + _, err = f.Write([]byte(" excludesfile = '" + fs.Join("etc", "gitignore_global") + "'\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) From 232aa40e0f988e37235f45c5fe01f6117abd8830 Mon Sep 17 00:00:00 2001 From: "Alan D. Cabrera" Date: Tue, 8 May 2018 10:19:31 -0700 Subject: [PATCH 7/8] Escape filenames in tests Signed-off-by: Alan D. Cabrera --- plumbing/format/gitignore/dir_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go index 08cf86bbf..6efff016a 100644 --- a/plumbing/format/gitignore/dir_test.go +++ b/plumbing/format/gitignore/dir_test.go @@ -3,6 +3,7 @@ package gitignore import ( "os" "os/user" + "strconv" . "gopkg.in/check.v1" "gopkg.in/src-d/go-billy.v4" @@ -59,7 +60,7 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = \"" + fs.Join(usr.HomeDir, ".gitignore_global") + "\"\n")) + _, err = f.Write([]byte(" excludesfile = " + strconv.Quote(fs.Join(usr.HomeDir, ".gitignore_global")) + "\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) @@ -129,7 +130,7 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = '" + fs.Join(usr.HomeDir, ".gitignore_global") + "'\n")) + _, err = f.Write([]byte(" excludesfile = " + strconv.Quote(fs.Join(usr.HomeDir, ".gitignore_global")) + "\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) @@ -145,12 +146,12 @@ func (s *MatcherSuite) SetUpTest(c *C) { c.Assert(err, IsNil) _, err = f.Write([]byte("[core]\n")) c.Assert(err, IsNil) - _, err = f.Write([]byte(" excludesfile = '" + fs.Join("etc", "gitignore_global") + "'\n")) + _, err = f.Write([]byte(" excludesfile = /etc/gitignore_global\n")) c.Assert(err, IsNil) err = f.Close() c.Assert(err, IsNil) - f, err = fs.Create(fs.Join("etc", "gitignore_global")) + f, err = fs.Create("/etc/gitignore_global") c.Assert(err, IsNil) _, err = f.Write([]byte("# IntelliJ\n")) c.Assert(err, IsNil) From 6cefe3a272bc3eec4864d3afdd2946c90ddccaa1 Mon Sep 17 00:00:00 2001 From: "Alan D. Cabrera" Date: Thu, 10 May 2018 12:57:22 -0700 Subject: [PATCH 8/8] Remove blank lines Signed-off-by: Alan D. Cabrera --- plumbing/format/gitignore/dir_test.go | 1 - worktree.go | 1 - 2 files changed, 2 deletions(-) diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go index 6efff016a..13e2d82b7 100644 --- a/plumbing/format/gitignore/dir_test.go +++ b/plumbing/format/gitignore/dir_test.go @@ -12,7 +12,6 @@ import ( type MatcherSuite struct { GFS billy.Filesystem // git repository root - RFS billy.Filesystem // root that contains user home MCFS billy.Filesystem // root that contains user home, but missing ~/.gitconfig MEFS billy.Filesystem // root that contains user home, but missing excludesfile entry diff --git a/worktree.go b/worktree.go index aeb011a67..ddf6fffd0 100644 --- a/worktree.go +++ b/worktree.go @@ -34,7 +34,6 @@ var ( type Worktree struct { // Filesystem underlying filesystem. Filesystem billy.Filesystem - // External excludes not found in the repository .gitignore Excludes []gitignore.Pattern