This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cf_ignore.go
107 lines (85 loc) · 2.15 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package cf_ignore
import (
"io"
"io/ioutil"
"path"
"strings"
"github.com/cloudfoundry-incubator/ltc/droplet_runner/command_factory/cf_ignore/glob"
)
//go:generate counterfeiter -o fake_cf_ignore/fake_cf_ignore.go . CFIgnore
type CFIgnore interface {
Parse(ignored io.Reader) error
ShouldIgnore(path string) bool
}
type ignorePattern struct {
exclude bool
glob glob.Glob
}
type cfIgnore struct {
patterns []ignorePattern
}
var defaultIgnoreLines = []string{
".cfignore",
"/manifest.yml",
".gitignore",
".git",
".hg",
".svn",
"_darcs",
".DS_Store",
}
func New() CFIgnore {
return &cfIgnore{
parsePatterns(defaultIgnoreLines),
}
}
func (c *cfIgnore) Parse(ignored io.Reader) error {
ignoredBytes, err := ioutil.ReadAll(ignored)
if err != nil {
return err
}
lines := strings.Split(string(ignoredBytes), "\n")
c.patterns = append(c.patterns, parsePatterns(lines)...)
return nil
}
func (c *cfIgnore) ShouldIgnore(path string) bool {
result := false
for _, pattern := range c.patterns {
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
}
func parsePatterns(ignoresLines []string) []ignorePattern {
var patterns []ignorePattern
for _, line := range ignoresLines {
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 patterns
}