Skip to content

Commit 934e99b

Browse files
authored
Merge pull request go-git#402 from enisdenjo/gitdir-info-exclude
plumbing: gitignore, Read .git/info/exclude file too.
2 parents 8884a20 + 92c37d5 commit 934e99b

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

Diff for: plumbing/format/gitignore/dir.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ import (
1313
)
1414

1515
const (
16-
commentPrefix = "#"
17-
coreSection = "core"
18-
excludesfile = "excludesfile"
19-
gitDir = ".git"
20-
gitignoreFile = ".gitignore"
21-
gitconfigFile = ".gitconfig"
22-
systemFile = "/etc/gitconfig"
16+
commentPrefix = "#"
17+
coreSection = "core"
18+
excludesfile = "excludesfile"
19+
gitDir = ".git"
20+
gitignoreFile = ".gitignore"
21+
gitconfigFile = ".gitconfig"
22+
systemFile = "/etc/gitconfig"
23+
infoExcludeFile = gitDir + "/info/exclude"
2324
)
2425

2526
// readIgnoreFile reads a specific git ignore file.
@@ -42,10 +43,14 @@ func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps [
4243
return
4344
}
4445

45-
// ReadPatterns reads gitignore patterns recursively traversing through the directory
46-
// structure. The result is in the ascending order of priority (last higher).
46+
// ReadPatterns reads the .git/info/exclude and then the gitignore patterns
47+
// recursively traversing through the directory structure. The result is in
48+
// the ascending order of priority (last higher).
4749
func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error) {
48-
ps, _ = readIgnoreFile(fs, path, gitignoreFile)
50+
ps, _ = readIgnoreFile(fs, path, infoExcludeFile)
51+
52+
subps, _ := readIgnoreFile(fs, path, gitignoreFile)
53+
ps = append(ps, subps...)
4954

5055
var fis []os.FileInfo
5156
fis, err = fs.ReadDir(fs.Join(path...))

Diff for: plumbing/format/gitignore/dir_test.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ var _ = Suite(&MatcherSuite{})
2424
func (s *MatcherSuite) SetUpTest(c *C) {
2525
// setup generic git repository root
2626
fs := memfs.New()
27-
f, err := fs.Create(".gitignore")
27+
28+
err := fs.MkdirAll(".git/info", os.ModePerm)
29+
c.Assert(err, IsNil)
30+
f, err := fs.Create(".git/info/exclude")
31+
c.Assert(err, IsNil)
32+
_, err = f.Write([]byte("exclude.crlf\r\n"))
33+
c.Assert(err, IsNil)
34+
err = f.Close()
35+
c.Assert(err, IsNil)
36+
37+
f, err = fs.Create(".gitignore")
2838
c.Assert(err, IsNil)
2939
_, err = f.Write([]byte("vendor/g*/\n"))
3040
c.Assert(err, IsNil)
@@ -44,6 +54,8 @@ func (s *MatcherSuite) SetUpTest(c *C) {
4454

4555
err = fs.MkdirAll("another", os.ModePerm)
4656
c.Assert(err, IsNil)
57+
err = fs.MkdirAll("exclude.crlf", os.ModePerm)
58+
c.Assert(err, IsNil)
4759
err = fs.MkdirAll("ignore.crlf", os.ModePerm)
4860
c.Assert(err, IsNil)
4961
err = fs.MkdirAll("vendor/github.com", os.ModePerm)
@@ -173,9 +185,10 @@ func (s *MatcherSuite) SetUpTest(c *C) {
173185
func (s *MatcherSuite) TestDir_ReadPatterns(c *C) {
174186
ps, err := ReadPatterns(s.GFS, nil)
175187
c.Assert(err, IsNil)
176-
c.Assert(ps, HasLen, 3)
188+
c.Assert(ps, HasLen, 4)
177189

178190
m := NewMatcher(ps)
191+
c.Assert(m.Match([]string{"exclude.crlf"}, true), Equals, true)
179192
c.Assert(m.Match([]string{"ignore.crlf"}, true), Equals, true)
180193
c.Assert(m.Match([]string{"vendor", "gopkg.in"}, true), Equals, true)
181194
c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false)

0 commit comments

Comments
 (0)