Skip to content

Commit

Permalink
Merge pull request go-git#402 from enisdenjo/gitdir-info-exclude
Browse files Browse the repository at this point in the history
plumbing: gitignore, Read .git/info/exclude file too.
  • Loading branch information
mcuadros authored and caitlinelfring committed May 6, 2022
1 parent d070a53 commit 4fbf886
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
23 changes: 13 additions & 10 deletions plumbing/format/gitignore/dir.go
Expand Up @@ -14,13 +14,14 @@ import (
)

const (
commentPrefix = "#"
coreSection = "core"
excludesfile = "excludesfile"
gitDir = ".git"
gitignoreFile = ".gitignore"
gitconfigFile = ".gitconfig"
systemFile = "/etc/gitconfig"
commentPrefix = "#"
coreSection = "core"
excludesfile = "excludesfile"
gitDir = ".git"
gitignoreFile = ".gitignore"
gitconfigFile = ".gitconfig"
systemFile = "/etc/gitconfig"
infoExcludeFile = gitDir + "/info/exclude"
)

// readIgnoreFile reads a specific git ignore file.
Expand All @@ -43,10 +44,12 @@ func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps [
return
}

// ReadPatterns reads gitignore patterns recursively traversing through the directory
// structure. The result is in the ascending order of priority (last higher).
// ReadPatterns reads the .git/info/exclude and then the 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, ignoreFiles []string) (ps []Pattern, err error) {
ps = []Pattern{}
ps, _ = readIgnoreFile(fs, path, infoExcludeFile)

for _, filename := range ignoreFiles {
var subps []Pattern
if subps, err = readIgnoreFile(fs, path, filename); err != nil {
Expand Down
17 changes: 15 additions & 2 deletions plumbing/format/gitignore/dir_test.go
Expand Up @@ -24,7 +24,17 @@ var _ = Suite(&MatcherSuite{})
func (s *MatcherSuite) SetUpTest(c *C) {
// setup generic git repository root
fs := memfs.New()
f, err := fs.Create(".gitignore")

err := fs.MkdirAll(".git/info", os.ModePerm)
c.Assert(err, IsNil)
f, err := fs.Create(".git/info/exclude")
c.Assert(err, IsNil)
_, err = f.Write([]byte("exclude.crlf\r\n"))
c.Assert(err, IsNil)
err = f.Close()
c.Assert(err, IsNil)

f, err = fs.Create(".gitignore")
c.Assert(err, IsNil)
_, err = f.Write([]byte("vendor/g*/\n"))
c.Assert(err, IsNil)
Expand All @@ -44,6 +54,8 @@ func (s *MatcherSuite) SetUpTest(c *C) {

err = fs.MkdirAll("another", os.ModePerm)
c.Assert(err, IsNil)
err = fs.MkdirAll("exclude.crlf", os.ModePerm)
c.Assert(err, IsNil)
err = fs.MkdirAll("ignore.crlf", os.ModePerm)
c.Assert(err, IsNil)
err = fs.MkdirAll("vendor/github.com", os.ModePerm)
Expand Down Expand Up @@ -173,9 +185,10 @@ func (s *MatcherSuite) SetUpTest(c *C) {
func (s *MatcherSuite) TestDir_ReadPatterns(c *C) {
ps, err := ReadPatterns(s.GFS, nil, []string{".gitignore"})
c.Assert(err, IsNil)
c.Assert(ps, HasLen, 3)
c.Assert(ps, HasLen, 4)

m := NewMatcher(ps)
c.Assert(m.Match([]string{"exclude.crlf"}, true), Equals, true)
c.Assert(m.Match([]string{"ignore.crlf"}, true), Equals, true)
c.Assert(m.Match([]string{"vendor", "gopkg.in"}, true), Equals, true)
c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false)
Expand Down

0 comments on commit 4fbf886

Please sign in to comment.