-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatterns.go
More file actions
96 lines (78 loc) · 2.22 KB
/
patterns.go
File metadata and controls
96 lines (78 loc) · 2.22 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
package main
import (
"path/filepath"
"strings"
)
type PatternMatcher struct {
includePatterns []string
excludePatterns []string
}
func NewPatternMatcher(include, exclude []string) *PatternMatcher {
if exclude == nil {
exclude = defaultExcludes()
}
return &PatternMatcher{
includePatterns: include,
excludePatterns: exclude,
}
}
func (pm *PatternMatcher) ShouldProcess(path string) bool {
if len(pm.includePatterns) > 0 {
matches := false
for _, pattern := range pm.includePatterns {
if matched := matchPattern(pattern, path); matched {
matches = true
break
}
}
if !matches {
return false
}
}
for _, pattern := range pm.excludePatterns {
if matched := matchPattern(pattern, path); matched {
return false
}
}
return true
}
func matchPattern(pattern, path string) bool {
path = filepath.ToSlash(path)
pattern = filepath.ToSlash(pattern)
if !strings.Contains(pattern, "*") {
return strings.Contains(path, "/"+pattern+"/") ||
strings.HasPrefix(path, pattern+"/") ||
strings.HasSuffix(path, "/"+pattern) ||
path == pattern
}
if strings.HasPrefix(pattern, "**/") {
pattern = pattern[3:]
for {
if matched, _ := filepath.Match(pattern, path); matched {
return true
}
lastSlash := strings.LastIndex(path, "/")
if lastSlash == -1 {
break
}
path = path[lastSlash+1:]
}
return false
}
matched, _ := filepath.Match(pattern, filepath.Base(path))
return matched
}
func defaultExcludes() []string {
return []string{
".git", ".git/**", ".gitignore", ".gitattributes", ".gitmodules",
".svn", ".hg",
"target", "node_modules", "dist", "build",
"package-lock.json",
"*.exe", "*.dll", "*.so", "*.dylib", "*.o", "*.obj",
"*.bin", "*.dat", "*.db", "*.sqlite", "*.sqlite3",
"*.gz", "*.zip", "*.tar", "*.rar", "*.7z", "*.bz2",
"*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp",
"*.ico", "*.svg", "*.webp", "*.tiff", "*.raw", "*.heic",
"__pycache__", ".mypy_cache", ".pytest_cache",
}
}