1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
package gitignore
import (
"os"
"os/user"
"strconv"
"strings"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
. "gopkg.in/check.v1"
)
type MatcherSuite struct {
GFS billy.Filesystem // git repository root
RFS billy.Filesystem // root that contains user home
RFSR billy.Filesystem // root that contains user home, but with relative ~/.gitignore_global
RFSU billy.Filesystem // root that contains user home, but with relative ~user/.gitignore_global
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 .gitignore
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()
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)
_, err = f.Write([]byte("ignore.crlf\r\n"))
c.Assert(err, IsNil)
err = f.Close()
c.Assert(err, IsNil)
err = fs.MkdirAll("vendor", os.ModePerm)
c.Assert(err, IsNil)
f, err = fs.Create("vendor/.gitignore")
c.Assert(err, IsNil)
_, err = f.Write([]byte("!github.com/\n"))
c.Assert(err, IsNil)
err = f.Close()
c.Assert(err, IsNil)
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)
c.Assert(err, IsNil)
err = fs.MkdirAll("vendor/gopkg.in", os.ModePerm)
c.Assert(err, IsNil)
err = fs.MkdirAll("multiple/sub/ignores/first", os.ModePerm)
c.Assert(err, IsNil)
err = fs.MkdirAll("multiple/sub/ignores/second", os.ModePerm)
c.Assert(err, IsNil)
f, err = fs.Create("multiple/sub/ignores/first/.gitignore")
c.Assert(err, IsNil)
_, err = f.Write([]byte("ignore_dir\n"))
c.Assert(err, IsNil)
err = f.Close()
c.Assert(err, IsNil)
f, err = fs.Create("multiple/sub/ignores/second/.gitignore")
c.Assert(err, IsNil)
_, err = f.Write([]byte("ignore_dir\n"))
c.Assert(err, IsNil)
err = f.Close()
c.Assert(err, IsNil)
err = fs.MkdirAll("multiple/sub/ignores/first/ignore_dir", os.ModePerm)
c.Assert(err, IsNil)
err = fs.MkdirAll("multiple/sub/ignores/second/ignore_dir", os.ModePerm)
c.Assert(err, IsNil)
s.GFS = fs
// setup root that contains user home
home, err := os.UserHomeDir()
c.Assert(err, IsNil)
fs = memfs.New()
err = fs.MkdirAll(home, os.ModePerm)
c.Assert(err, IsNil)
f, err = fs.Create(fs.Join(home, gitconfigFile))
c.Assert(err, IsNil)
_, err = f.Write([]byte("[core]\n"))
c.Assert(err, IsNil)
_, err = f.Write([]byte(" excludesfile = " + strconv.Quote(fs.Join(home, ".gitignore_global")) + "\n"))
c.Assert(err, IsNil)
err = f.Close()
c.Assert(err, IsNil)
f, err = fs.Create(fs.Join(home, ".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 with relative ~/.gitignore_global
fs = memfs.New()
err = fs.MkdirAll(home, os.ModePerm)
c.Assert(err, IsNil)
f, err = fs.Create(fs.Join(home, gitconfigFile))
c.Assert(err, IsNil)
_, err = f.Write([]byte("[core]\n"))
c.Assert(err, IsNil)
_, err = f.Write([]byte(" excludesfile = ~/.gitignore_global" + "\n"))
c.Assert(err, IsNil)
err = f.Close()
c.Assert(err, IsNil)
f, err = fs.Create(fs.Join(home, ".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.RFSR = fs
// root that contains user home, but with relative ~user/.gitignore_global
fs = memfs.New()
err = fs.MkdirAll(home, os.ModePerm)
c.Assert(err, IsNil)
f, err = fs.Create(fs.Join(home, gitconfigFile))
c.Assert(err, IsNil)
_, err = f.Write([]byte("[core]\n"))
c.Assert(err, IsNil)
currentUser, err := user.Current()
c.Assert(err, IsNil)
// remove domain for windows
username := currentUser.Username[strings.Index(currentUser.Username, "\\")+1:]
_, err = f.Write([]byte(" excludesfile = ~" + username + "/.gitignore_global" + "\n"))
c.Assert(err, IsNil)
err = f.Close()
c.Assert(err, IsNil)
f, err = fs.Create(fs.Join(home, ".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.RFSU = fs
// root that contains user home, but missing ~/.gitconfig
fs = memfs.New()
err = fs.MkdirAll(home, os.ModePerm)
c.Assert(err, IsNil)
f, err = fs.Create(fs.Join(home, ".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(home, os.ModePerm)
c.Assert(err, IsNil)
f, err = fs.Create(fs.Join(home, 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(home, ".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(home, os.ModePerm)
c.Assert(err, IsNil)
f, err = fs.Create(fs.Join(home, gitconfigFile))
c.Assert(err, IsNil)
_, err = f.Write([]byte("[core]\n"))
c.Assert(err, IsNil)
_, err = f.Write([]byte(" excludesfile = " + strconv.Quote(fs.Join(home, ".gitignore_global")) + "\n"))
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\n"))
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) {
checkPatterns := func(ps []Pattern) {
c.Assert(ps, HasLen, 6)
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)
c.Assert(m.Match([]string{"multiple", "sub", "ignores", "first", "ignore_dir"}, true), Equals, true)
c.Assert(m.Match([]string{"multiple", "sub", "ignores", "second", "ignore_dir"}, true), Equals, true)
}
ps, err := ReadPatterns(s.GFS, nil)
c.Assert(err, IsNil)
checkPatterns(ps)
// passing an empty slice with capacity to check we don't hit a bug where the extra capacity is reused incorrectly
ps, err = ReadPatterns(s.GFS, make([]string, 0, 6))
c.Assert(err, IsNil)
checkPatterns(ps)
}
func (s *MatcherSuite) TestDir_ReadRelativeGlobalGitIgnore(c *C) {
for _, fs := range []billy.Filesystem{s.RFSR, s.RFSU} {
ps, err := LoadGlobalPatterns(fs)
c.Assert(err, IsNil)
c.Assert(ps, HasLen, 2)
m := NewMatcher(ps)
c.Assert(m.Match([]string{".idea/"}, true), Equals, false)
c.Assert(m.Match([]string{"*.iml"}, true), Equals, true)
c.Assert(m.Match([]string{"IntelliJ"}, 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)
}
|