Debian Codegraph @

filepathfilter/filepathfilter.go

raw symbols SCIP
1 package filepathfilter
2
3 import (
4 "strings"
5 "sync"
6
7 "github.com/git-lfs/git-lfs/v3/tr"
8 "github.com/git-lfs/wildmatch/v2"
9 "github.com/golang/groupcache/lru"
10 "github.com/rubyist/tracerx"
11 )
12
13 type Pattern interface {
14 Match(filename string) bool
15 // String returns a string representation (see: regular expressions) of
16 // the underlying pattern used to match filenames against this Pattern.
17 String() string
18 }
19
20 type Filter struct {
21 include []Pattern
22 exclude []Pattern
23 defaultValue bool
24 cache *lru.Cache
25 cacheLock sync.Mutex
26 }
27
28 type PatternType bool
29
30 const (
31 GitIgnore = PatternType(false)
32 GitAttributes = PatternType(true)
33 )
34
35 func (p PatternType) String() string {
36 if p == GitIgnore {
37 return "gitignore"
38 }
39 return "gitattributes"
40 }
41
42 type options struct {
43 defaultValue bool
44 useCache bool
45 cacheSize int
46 }
47
48 type Option func(*options)
49
50 // DefaultValue is an option representing the default value of a filepathfilter
51 // if no patterns match. If this option is not provided, the default is true.
52 func DefaultValue(val bool) Option {
53 return func(args *options) {
54 args.defaultValue = val
55 }
56 }
57
58 func EnableCache(size int) Option {
59 return func(args *options) {
60 args.useCache = true
61 args.cacheSize = size
62 }
63 }
64
65 func DisableCache() Option {
66 return func(args *options) {
67 args.useCache = false
68 }
69 }
70
71 func NewFromPatterns(include, exclude []Pattern, setters ...Option) *Filter {
72 args := &options{defaultValue: true, useCache: false}
73 for _, setter := range setters {
74 setter(args)
75 }
76
77 f := &Filter{include: include, exclude: exclude, defaultValue: args.defaultValue}
78
79 if args.useCache && args.cacheSize >= 0 {
80 f.cache = lru.New(args.cacheSize)
81 }
82
83 return f
84 }
85
86 func New(include, exclude []string, ptype PatternType, gitEnv Environment, setters ...Option) *Filter {
87 return NewFromPatterns(
88 convertToWildmatch(include, ptype, gitEnv),
89 convertToWildmatch(exclude, ptype, gitEnv), setters...)
90 }
91
92 // Include returns the result of calling String() on each Pattern in the
93 // include set of this *Filter.
94 func (f *Filter) Include() []string { return wildmatchToString(f.include...) }
95
96 // Exclude returns the result of calling String() on each Pattern in the
97 // exclude set of this *Filter.
98 func (f *Filter) Exclude() []string { return wildmatchToString(f.exclude...) }
99
100 // wildmatchToString maps the given set of Pattern's to a string slice by
101 // calling String() on each pattern.
102 func wildmatchToString(ps ...Pattern) []string {
103 s := make([]string, 0, len(ps))
104 for _, p := range ps {
105 s = append(s, p.String())
106 }
107
108 return s
109 }
110
111 func (f *Filter) allows(filename string) bool {
112 var included bool
113 for _, inc := range f.include {
114 if included = inc.Match(filename); included {
115 break
116 }
117 }
118
119 if !included && len(f.include) > 0 {
120 tracerx.Printf("filepathfilter: rejecting %q via %v", filename, f.include)
121 return false
122 }
123
124 // Beyond this point, the only values we can logically return are false
125 // or the default value. If the default is false, then there's no point
126 // traversing the exclude patterns because the return value will always
127 // be false; we'd do extra work for no functional benefit.
128 if !included && !f.defaultValue {
129 tracerx.Printf("filepathfilter: rejecting %q", filename)
130 return false
131 }
132
133 for _, ex := range f.exclude {
134 if ex.Match(filename) {
135 tracerx.Printf("filepathfilter: rejecting %q via %q", filename, ex.String())
136 return false
137 }
138 }
139
140 // No patterns matched and our default value is true.
141 tracerx.Printf("filepathfilter: accepting %q", filename)
142 return true
143 }
144
145 func (f *Filter) Allows(filename string) bool {
146 if f == nil {
147 return true
148 }
149
150 if f.cache != nil {
151 f.cacheLock.Lock()
152 res, ok := f.cache.Get(filename)
153 f.cacheLock.Unlock()
154 if ok {
155 return res.(bool)
156 }
157 }
158
159 res := f.allows(filename)
160
161 if f.cache != nil {
162 f.cacheLock.Lock()
163 f.cache.Add(filename, res)
164 f.cacheLock.Unlock()
165 }
166
167 return res
168 }
169
170 type wm struct {
171 w *wildmatch.Wildmatch
172 p string
173 }
174
175 func (w *wm) Match(filename string) bool {
176 return w.w.Match(filename)
177 }
178
179 func (w *wm) String() string {
180 return w.p
181 }
182
183 const (
184 sep byte = '/'
185 )
186
187 func NewPattern(p string, ptype PatternType, gitEnv Environment) Pattern {
188 tracerx.Printf("filepathfilter: creating pattern %q of type %v", p, ptype)
189
190 switch ptype {
191 case GitIgnore:
192 return &wm{
193 p: p,
194 w: wildmatch.NewWildmatch(
195 p,
196 caseFromConfig(gitEnv),
197 wildmatch.Contents,
198 ),
199 }
200 case GitAttributes:
201 return &wm{
202 p: p,
203 w: wildmatch.NewWildmatch(
204 p,
205 caseFromConfig(gitEnv),
206 wildmatch.Basename,
207 wildmatch.GitAttributes,
208 ),
209 }
210 default:
211 panic(tr.Tr.Get("unreachable"))
212 }
213 }
214
215 // join joins path elements together via the separator "sep" and produces valid
216 // paths without multiple separators (unless multiple separators were included
217 // in the original paths []string).
218 func join(paths ...string) string {
219 var joined string
220
221 for i, path := range paths {
222 joined = joined + path
223 if i != len(paths)-1 && !strings.HasSuffix(path, string(sep)) {
224 joined = joined + string(sep)
225 }
226 }
227
228 return joined
229 }
230
231 func convertToWildmatch(rawpatterns []string, ptype PatternType, gitEnv Environment) []Pattern {
232 patterns := make([]Pattern, len(rawpatterns))
233 for i, raw := range rawpatterns {
234 patterns[i] = NewPattern(raw, ptype, gitEnv)
235 }
236 return patterns
237 }
238
239 type Environment interface {
240 Get(key string) (val string, ok bool)
241 Bool(key string, def bool) (val bool)
242 }
243
244 func caseFromConfig(gitEnv Environment) func(w *wildmatch.Wildmatch) {
245 if gitEnv != nil && gitEnv.Bool("core.ignorecase", false) {
246 return wildmatch.CaseFold
247 } else {
248 return func(w *wildmatch.Wildmatch) {}
249 }
250 }