forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_ignore.go
87 lines (68 loc) · 1.74 KB
/
cf_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
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
package appfiles
import (
"path"
"strings"
"code.cloudfoundry.org/cli/utils/glob"
)
//go:generate counterfeiter . CfIgnore
type CfIgnore interface {
FileShouldBeIgnored(path string) bool
}
func NewCfIgnore(text string) CfIgnore {
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 cfIgnore(patterns)
}
func (ignore cfIgnore) 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.Glob) {
globs = append(globs, glob.MustCompileGlob(pattern))
globs = append(globs, glob.MustCompileGlob(path.Join(pattern, "*")))
globs = append(globs, glob.MustCompileGlob(path.Join(pattern, "**", "*")))
if !strings.HasPrefix(pattern, "/") {
globs = append(globs, glob.MustCompileGlob(path.Join("**", pattern)))
globs = append(globs, glob.MustCompileGlob(path.Join("**", pattern, "*")))
globs = append(globs, glob.MustCompileGlob(path.Join("**", pattern, "**", "*")))
}
return
}
type ignorePattern struct {
exclude bool
glob glob.Glob
}
type cfIgnore []ignorePattern
var defaultIgnoreLines = []string{
".cfignore",
"/manifest.yml",
".gitignore",
".git",
".hg",
".svn",
"_darcs",
".DS_Store",
}