-
Notifications
You must be signed in to change notification settings - Fork 1
/
ignore_files.go
83 lines (66 loc) · 1.64 KB
/
ignore_files.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
package dirfiles
import (
"path"
"strings"
)
type IgnoreFiles interface {
FileShouldBeIgnored(path string) bool
}
func NewIgnoreFiles(text string) IgnoreFiles {
patterns := []ignorePattern{}
lines := strings.Split(text, "\n")
lines = append(defaultIgnoreLines, lines...)
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
ignore := true
if strings.HasPrefix(line, "!") {
line = line[1:]
ignore = false
}
for _, p := range globsForPattern(path.Clean(line)) {
patterns = append(patterns, ignorePattern{ignore, p})
}
}
return ignoreFile(patterns)
}
func (ignore ignoreFile) FileShouldBeIgnored(path string) bool {
result := false
for _, pattern := range ignore {
if strings.HasPrefix(pattern.glob.String(), "/") && !strings.HasPrefix(path, "/") {
path = "/" + path
}
if pattern.glob.Match(path) {
result = pattern.exclude
}
}
return result
}
func globsForPattern(pattern string) (globs []Glob) {
globs = append(globs, MustCompileGlob(pattern))
globs = append(globs, MustCompileGlob(path.Join(pattern, "*")))
globs = append(globs, MustCompileGlob(path.Join(pattern, "**", "*")))
if !strings.HasPrefix(pattern, "/") {
globs = append(globs, MustCompileGlob(path.Join("**", pattern)))
globs = append(globs, MustCompileGlob(path.Join("**", pattern, "*")))
globs = append(globs, MustCompileGlob(path.Join("**", pattern, "**", "*")))
}
return
}
type ignorePattern struct {
exclude bool
glob Glob
}
type ignoreFile []ignorePattern
var defaultIgnoreLines = []string{
".zipignore",
".cfignore",
".gitignore",
".git",
".hg",
".svn",
"_darcs",
".DS_Store",
}