-
Notifications
You must be signed in to change notification settings - Fork 199
/
path_filter.go
122 lines (97 loc) · 2.76 KB
/
path_filter.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
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
package true_git
import (
"fmt"
"path/filepath"
"strings"
"github.com/bmatcuk/doublestar"
)
type PathFilter struct {
// BasePath can be path to the directory or a single file
BasePath string
IncludePaths, ExcludePaths []string
}
func (f *PathFilter) IsFilePathValid(filePath string) bool {
return IsFilePathValid(filePath, f.BasePath, f.IncludePaths, f.ExcludePaths)
}
func (f *PathFilter) TrimFileBasePath(filePath string) string {
return TrimFileBasePath(filePath, f.BasePath)
}
func (f *PathFilter) String() string {
return fmt.Sprintf("BasePath=`%s`, IncludePaths=%v, ExcludePaths=%v", f.BasePath, f.IncludePaths, f.ExcludePaths)
}
func IsFilePathValid(filePath, basePath string, includePaths, excludePaths []string) bool {
if !IsFileInBasePath(filePath, basePath) {
return false
}
filePathInBasePath := TrimFileBasePath(filePath, basePath)
if len(includePaths) > 0 {
if !IsFilePathMatchesOneOfPatterns(filePathInBasePath, includePaths) {
return false
}
}
if len(excludePaths) > 0 {
if IsFilePathMatchesOneOfPatterns(filePathInBasePath, excludePaths) {
return false
}
}
return true
}
/*
* basePath can be path to a directory or a file.
*/
func IsFileInBasePath(filePath, basePath string) bool {
filePath = NormalizeAbsolutePath(filePath)
basePath = NormalizeAbsolutePath(basePath)
if filePath == basePath {
return true
}
return strings.HasPrefix(filePath, NormalizeDirectoryPrefix(basePath))
}
func IsFilePathMatchesPattern(filePath, pattern string) bool {
matched, err := doublestar.PathMatch(pattern, filePath)
if err != nil {
panic(err)
}
if matched {
return true
}
matched, err = doublestar.PathMatch(filepath.Join(pattern, "**", "*"), filePath)
if err != nil {
panic(err)
}
if matched {
return true
}
return false
}
func IsFilePathMatchesOneOfPatterns(filePath string, patterns []string) bool {
for _, pattern := range patterns {
if IsFileInBasePath(filePath, pattern) {
return true
}
if IsFilePathMatchesPattern(filePath, pattern) {
return true
}
}
return false
}
func TrimFileBasePath(filePath, basePath string) string {
filePath = NormalizeAbsolutePath(filePath)
basePath = NormalizeAbsolutePath(basePath)
if filePath == basePath {
// filePath path is always a path to a file, not a directory.
// Thus if BasePath is equal filePath, then BasePath is a path to the file.
// Return file name in this case by convention.
return filepath.Base(filePath)
}
return strings.TrimPrefix(filePath, NormalizeDirectoryPrefix(basePath))
}
func NormalizeAbsolutePath(path string) string {
return filepath.Clean(filepath.Join("/", path))
}
func NormalizeDirectoryPrefix(directoryPrefix string) string {
if directoryPrefix == "/" {
return directoryPrefix
}
return directoryPrefix + "/"
}