-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathignore.go
More file actions
223 lines (194 loc) · 4.57 KB
/
ignore.go
File metadata and controls
223 lines (194 loc) · 4.57 KB
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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
type ignorer struct {
alwaysExclude map[string]bool
patterns map[string][]ignorePattern
}
type ignorePattern struct {
pattern string
negated bool
dirOnly bool
anchored bool
}
func newIgnorer(alwaysExclude []string, sourceRoot string) *ignorer {
ae := make(map[string]bool)
for _, e := range alwaysExclude {
ae[e] = true
}
ig := &ignorer{
alwaysExclude: ae,
patterns: make(map[string][]ignorePattern),
}
ig.preload(sourceRoot)
return ig
}
func (ig *ignorer) preload(root string) {
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "warning: %s: %v\n", path, err)
return nil
}
if info.IsDir() {
name := filepath.Base(path)
if ig.alwaysExclude[name] && path != root {
return filepath.SkipDir
}
rel, err := filepath.Rel(root, path)
if err != nil {
fmt.Fprintf(os.Stderr, "warning: cannot compute relative path for %s: %v\n", path, err)
return nil
}
ig.loadGitignore(path, rel)
return nil
}
return nil
})
}
func (ig *ignorer) isExcluded(relPath string, isDir bool) bool {
name := filepath.Base(relPath)
if ig.alwaysExclude[name] {
return true
}
dirParts := strings.Split(filepath.Dir(relPath), string(os.PathSeparator))
dirs := []string{"."}
current := ""
for _, part := range dirParts {
if part == "." {
continue
}
if current == "" {
current = part
} else {
current = current + string(os.PathSeparator) + part
}
dirs = append(dirs, current)
}
excluded := false
for _, dir := range dirs {
patterns, ok := ig.patterns[dir]
if !ok {
continue
}
for _, p := range patterns {
if p.dirOnly && !isDir {
continue
}
if matchPattern(p, relPath, name) {
if p.negated {
excluded = false
} else {
excluded = true
}
}
}
}
return excluded
}
func (ig *ignorer) loadGitignore(absDir string, relDir string) {
gitignorePath := filepath.Join(absDir, ".gitignore")
f, err := os.Open(gitignorePath)
if err != nil {
return
}
defer f.Close()
var patterns []ignorePattern
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
p := ignorePattern{}
if strings.HasPrefix(line, "!") {
p.negated = true
line = line[1:]
}
if strings.HasSuffix(line, "/") {
p.dirOnly = true
line = strings.TrimSuffix(line, "/")
}
if strings.Contains(line, "/") {
p.anchored = true
}
p.pattern = line
patterns = append(patterns, p)
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "warning: reading %s: %v\n", gitignorePath, err)
}
if len(patterns) > 0 {
ig.patterns[relDir] = patterns
}
}
func matchPattern(p ignorePattern, relPath string, baseName string) bool {
pattern := p.pattern
if strings.Contains(pattern, "**") {
return matchDoublestar(pattern, relPath)
}
if p.anchored {
matched, _ := filepath.Match(pattern, relPath)
return matched
}
matched, _ := filepath.Match(pattern, baseName)
if matched {
return true
}
parts := strings.Split(relPath, string(os.PathSeparator))
for i := range parts {
sub := strings.Join(parts[i:], string(os.PathSeparator))
matched, _ = filepath.Match(pattern, sub)
if matched {
return true
}
}
return false
}
func matchDoublestar(pattern string, path string) bool {
if pattern == "**" {
return true
}
if strings.HasPrefix(pattern, "**/") {
rest := pattern[3:]
if matched, _ := filepath.Match(rest, path); matched {
return true
}
parts := strings.Split(path, string(os.PathSeparator))
for i := range parts {
sub := strings.Join(parts[i:], string(os.PathSeparator))
if matched, _ := filepath.Match(rest, sub); matched {
return true
}
}
return false
}
if strings.HasSuffix(pattern, "/**") {
prefix := pattern[:len(pattern)-3]
return strings.HasPrefix(path, prefix+string(os.PathSeparator)) || path == prefix
}
idx := strings.Index(pattern, "/**/")
if idx >= 0 {
prefix := pattern[:idx]
suffix := pattern[idx+4:]
if !strings.HasPrefix(path, prefix+string(os.PathSeparator)) && path != prefix {
return false
}
rest := strings.TrimPrefix(path, prefix+string(os.PathSeparator))
if matched, _ := filepath.Match(suffix, rest); matched {
return true
}
parts := strings.Split(rest, string(os.PathSeparator))
for i := range parts {
sub := strings.Join(parts[i:], string(os.PathSeparator))
if matched, _ := filepath.Match(suffix, sub); matched {
return true
}
}
}
return false
}