-
Notifications
You must be signed in to change notification settings - Fork 7
/
gitignore.go
307 lines (266 loc) · 9.97 KB
/
gitignore.go
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
package gitignore
import (
"io"
"os"
"path/filepath"
"runtime"
"strings"
)
// use an empty GitIgnore for cached lookups
var empty = &ignore{}
// GitIgnore is the interface to .gitignore files and repositories. It defines
// methods for testing files for matching the .gitignore file, and then
// determining whether a file should be ignored or included.
type GitIgnore interface {
// Base returns the directory containing the .gitignore file.
Base() string
// Match attempts to match the path against this GitIgnore, and will
// return its Match if successful. Match will invoke the GitIgnore error
// handler (if defined) if it is not possible to determine the absolute
// path of the given path, or if its not possible to determine if the
// path represents a file or a directory. If an error occurs, Match
// returns nil and the error handler (if defined via New, NewWithErrors
// or NewWithCache) will be invoked.
Match(path string) Match
// Absolute attempts to match an absolute path against this GitIgnore. If
// the path is not located under the base directory of this GitIgnore, or
// is not matched by this GitIgnore, nil is returned.
Absolute(string, bool) Match
// Relative attempts to match a path relative to the GitIgnore base
// directory. isdir is used to indicate whether the path represents a file
// or a directory. If the path is not matched by the GitIgnore, nil is
// returned.
Relative(path string, isdir bool) Match
// Ignore returns true if the path is ignored by this GitIgnore. Paths
// that are not matched by this GitIgnore are not ignored. Internally,
// Ignore uses Match, and will return false if Match() returns nil for path.
Ignore(path string) bool
// Include returns true if the path is included by this GitIgnore. Paths
// that are not matched by this GitIgnore are always included. Internally,
// Include uses Match, and will return true if Match() returns nil for path.
Include(path string) bool
}
// ignore is the implementation of a .gitignore file.
type ignore struct {
_base string
_pattern []Pattern
_errors func(Error) bool
}
// NewGitIgnore creates a new GitIgnore instance from the patterns listed in t,
// representing a .gitignore file in the base directory. If errors is given, it
// will be invoked for every error encountered when parsing the .gitignore
// patterns. Parsing will terminate if errors is called and returns false,
// otherwise, parsing will continue until end of file has been reached.
func New(r io.Reader, base string, errors func(Error) bool) GitIgnore {
// do we have an error handler?
_errors := errors
if _errors == nil {
_errors = func(e Error) bool { return true }
}
// extract the patterns from the reader
_parser := NewParser(r, _errors)
_patterns := _parser.Parse()
return &ignore{_base: base, _pattern: _patterns, _errors: _errors}
} // New()
// NewFromFile creates a GitIgnore instance from the given file. An error
// will be returned if file cannot be opened or its absolute path determined.
func NewFromFile(file string) (GitIgnore, error) {
// define an error handler to catch any file access errors
// - record the first encountered error
var _error Error
_errors := func(e Error) bool {
if _error == nil {
_error = e
}
return true
}
// attempt to retrieve the GitIgnore represented by this file
_ignore := NewWithErrors(file, _errors)
// did we encounter an error?
// - if the error has a zero Position then it was encountered
// before parsing was attempted, so we return that error
if _error != nil {
if _error.Position().Zero() {
return nil, _error.Underlying()
}
}
// otherwise, we ignore the parser errors
return _ignore, nil
} // NewFromFile()
// NewWithErrors creates a GitIgnore instance from the given file.
// If errors is given, it will be invoked for every error encountered when
// parsing the .gitignore patterns. Parsing will terminate if errors is called
// and returns false, otherwise, parsing will continue until end of file has
// been reached. NewWithErrors returns nil if the .gitignore could not be read.
func NewWithErrors(file string, errors func(Error) bool) GitIgnore {
var _err error
// do we have an error handler?
_file := file
_errors := errors
if _errors == nil {
_errors = func(e Error) bool { return true }
} else {
// augment the error handler to include the .gitignore file name
// - we do this here since the parser and lexer interfaces are
// not aware of file names
_errors = func(e Error) bool {
// augment the position with the file name
_position := e.Position()
_position.File = _file
// create a new error with the updated Position
_error := NewError(e.Underlying(), _position)
// invoke the original error handler
return errors(_error)
}
}
// we need the absolute path for the GitIgnore base
_file, _err = filepath.Abs(file)
if _err != nil {
_errors(NewError(_err, Position{}))
return nil
}
_base := filepath.Dir(_file)
// attempt to open the ignore file to create the io.Reader
_fh, _err := os.Open(_file)
if _err != nil {
_errors(NewError(_err, Position{}))
return nil
}
// return the GitIgnore instance
return New(_fh, _base, _errors)
} // NewWithErrors()
// NewWithCache returns a GitIgnore instance (using NewWithErrors)
// for the given file. If the file has been loaded before, its GitIgnore
// instance will be returned from the cache rather than being reloaded. If
// cache is not defined, NewWithCache will behave as NewWithErrors
//
// If NewWithErrors returns nil, NewWithCache will store an empty
// GitIgnore (i.e. no patterns) against the file to prevent repeated parse
// attempts on subsequent requests for the same file. Subsequent calls to
// NewWithCache for a file that could not be loaded due to an error will
// return nil.
//
// If errors is given, it will be invoked for every error encountered when
// parsing the .gitignore patterns. Parsing will terminate if errors is called
// and returns false, otherwise, parsing will continue until end of file has
// been reached.
func NewWithCache(file string, cache Cache, errors func(Error) bool) GitIgnore {
// do we have an error handler?
_errors := errors
if _errors == nil {
_errors = func(e Error) bool { return true }
}
// use the file absolute path as its key into the cache
_abs, _err := filepath.Abs(file)
if _err != nil {
_errors(NewError(_err, Position{}))
return nil
}
var _ignore GitIgnore
if cache != nil {
_ignore = cache.Get(_abs)
}
if _ignore == nil {
_ignore = NewWithErrors(file, _errors)
if _ignore == nil {
// if the load failed, cache an empty GitIgnore to prevent
// further attempts to load this file
_ignore = empty
}
if cache != nil {
cache.Set(_abs, _ignore)
}
}
// return the ignore (if we have it)
if _ignore == empty {
return nil
} else {
return _ignore
}
} // NewWithCache()
// Base returns the directory containing the .gitignore file for this GitIgnore.
func (i *ignore) Base() string {
return i._base
} // Base()
// Match attempts to match the path against this GitIgnore, and will
// return its Match if successful. Match will invoke the GitIgnore error
// handler (if defined) if it is not possible to determine the absolute
// path of the given path, or if its not possible to determine if the
// path represents a file or a directory. If an error occurs, Match
// returns nil and the error handler (if defined via New, NewWithErrors
// or NewWithCache) will be invoked.
func (i *ignore) Match(path string) Match {
// ensure we have the absolute path for the given file
_path, _err := filepath.Abs(path)
if _err != nil {
i._errors(NewError(_err, Position{}))
return nil
}
// is the path a file or a directory?
_info, _err := os.Stat(_path)
if _err != nil {
i._errors(NewError(_err, Position{}))
return nil
}
_isdir := _info.IsDir()
// attempt to match the absolute path
return i.Absolute(_path, _isdir)
} // Match()
// Absolute attempts to match an absolute path against this GitIgnore. If
// the path is not located under the base directory of this GitIgnore, or
// is not matched by this GitIgnore, nil is returned.
func (i *ignore) Absolute(path string, isdir bool) Match {
// does the file share the same directory as this ignore file?
if !strings.HasPrefix(path, i._base) {
return nil
}
// extract the relative path of this file
_prefix := len(i._base) + 1
_rel := string(path[_prefix:])
return i.Relative(_rel, isdir)
} // Absolute()
// Relative attempts to match a path relative to the GitIgnore base
// directory. isdir is used to indicate whether the path represents a file
// or a directory. If the path is not matched by the GitIgnore, nil is
// returned.
func (i *ignore) Relative(path string, isdir bool) Match {
// if we are on Windows, then translate the path to Unix form
_rel := path
if runtime.GOOS == "windows" {
_rel = filepath.ToSlash(_rel)
}
// iterate over the patterns for this ignore file
// - iterate in reverse, since later patterns overwrite earlier
for _i := len(i._pattern) - 1; _i >= 0; _i-- {
_pattern := i._pattern[_i]
if _pattern.Match(_rel, isdir) {
return _pattern
}
}
// we don't match this file
return nil
} // Relative()
// Ignore returns true if the path is ignored by this GitIgnore. Paths
// that are not matched by this GitIgnore are not ignored. Internally,
// Ignore uses Match, and will return false if Match() returns nil for path.
func (i *ignore) Ignore(path string) bool {
_match := i.Match(path)
if _match != nil {
return _match.Ignore()
}
// we didn't match this path, so we don't ignore it
return false
} // Ignore()
// Include returns true if the path is included by this GitIgnore. Paths
// that are not matched by this GitIgnore are always included. Internally,
// Include uses Match, and will return true if Match() returns nil for path.
func (i *ignore) Include(path string) bool {
_match := i.Match(path)
if _match != nil {
return _match.Include()
}
// we didn't match this path, so we include it
return true
} // Include()
// ensure Ignore satisfies the GitIgnore interface
var _ GitIgnore = &ignore{}