Skip to content

Commit 55b186d

Browse files
committed
plumbing: gitignore, Read .git/info/exclude file too.
1 parent 99457e5 commit 55b186d

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

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...))

plumbing/format/gitignore/dir_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
type MatcherSuite struct {
1313
GFS billy.Filesystem // git repository root
14+
IEFS billy.Filesystem // git repository root using info/exclude instead
1415
RFS billy.Filesystem // root that contains user home
1516
MCFS billy.Filesystem // root that contains user home, but missing ~/.gitconfig
1617
MEFS billy.Filesystem // root that contains user home, but missing excludesfile entry
@@ -53,6 +54,39 @@ func (s *MatcherSuite) SetUpTest(c *C) {
5354

5455
s.GFS = fs
5556

57+
// setup generic git repository root using info/exclude instead
58+
fs = memfs.New()
59+
err = fs.MkdirAll(".git/info", os.ModePerm)
60+
c.Assert(err, IsNil)
61+
f, err = fs.Create(".git/info/exclude")
62+
c.Assert(err, IsNil)
63+
_, err = f.Write([]byte("vendor/g*/\n"))
64+
c.Assert(err, IsNil)
65+
_, err = f.Write([]byte("ignore.crlf\r\n"))
66+
c.Assert(err, IsNil)
67+
err = f.Close()
68+
c.Assert(err, IsNil)
69+
70+
err = fs.MkdirAll("vendor", os.ModePerm)
71+
c.Assert(err, IsNil)
72+
f, err = fs.Create("vendor/.gitignore")
73+
c.Assert(err, IsNil)
74+
_, err = f.Write([]byte("!github.com/\n"))
75+
c.Assert(err, IsNil)
76+
err = f.Close()
77+
c.Assert(err, IsNil)
78+
79+
err = fs.MkdirAll("another", os.ModePerm)
80+
c.Assert(err, IsNil)
81+
err = fs.MkdirAll("ignore.crlf", os.ModePerm)
82+
c.Assert(err, IsNil)
83+
err = fs.MkdirAll("vendor/github.com", os.ModePerm)
84+
c.Assert(err, IsNil)
85+
err = fs.MkdirAll("vendor/gopkg.in", os.ModePerm)
86+
c.Assert(err, IsNil)
87+
88+
s.IEFS = fs
89+
5690
// setup root that contains user home
5791
home, err := os.UserHomeDir()
5892
c.Assert(err, IsNil)
@@ -179,6 +213,15 @@ func (s *MatcherSuite) TestDir_ReadPatterns(c *C) {
179213
c.Assert(m.Match([]string{"ignore.crlf"}, true), Equals, true)
180214
c.Assert(m.Match([]string{"vendor", "gopkg.in"}, true), Equals, true)
181215
c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false)
216+
217+
ps, err = ReadPatterns(s.IEFS, nil)
218+
c.Assert(err, IsNil)
219+
c.Assert(ps, HasLen, 3)
220+
221+
m = NewMatcher(ps)
222+
c.Assert(m.Match([]string{"ignore.crlf"}, true), Equals, true)
223+
c.Assert(m.Match([]string{"vendor", "gopkg.in"}, true), Equals, true)
224+
c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false)
182225
}
183226

184227
func (s *MatcherSuite) TestDir_LoadGlobalPatterns(c *C) {

0 commit comments

Comments
 (0)