-
Notifications
You must be signed in to change notification settings - Fork 9
/
ignore.go
59 lines (47 loc) · 1.12 KB
/
ignore.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
package ignore
import (
"os"
"path/filepath"
"strings"
)
// IgnoreFile is the name of the ignore file.
const IgnoreFile = ".multignore"
// Filter is a list of paths to ignore.
type Filter []Rule
// New returns a new ignore filter.
func New(dir string, patterns ...string) Filter {
var rules []Rule
for _, p := range patterns {
rules = append(rules, ParseRule(dir, p))
}
return Filter(rules)
}
// Load returns the ignore filter from the given directory.
func Load(dir string) (Filter, error) {
data, err := os.ReadFile(filepath.Join(dir, IgnoreFile))
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
patterns := strings.Split(string(data), "\n")
return New(dir, patterns...), nil
}
// Match returns true if the path matches any ignore rules.
func (f Filter) Match(name string) bool {
for _, r := range f {
if match, _ := r.Match(name); match {
return true
}
}
return false
}
// Merge combines the ignore rules with the other ignore.
func (f Filter) Merge(other Filter) Filter {
var merge Filter
for _, r := range other {
merge = append(merge, r)
}
return append(f, merge...)
}