-
Notifications
You must be signed in to change notification settings - Fork 290
/
ignore.go
103 lines (85 loc) · 2.22 KB
/
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
package dockerignore
import (
"os"
"path/filepath"
"strings"
"github.com/docker/docker/builder/dockerignore"
"github.com/docker/docker/pkg/fileutils"
"github.com/windmilleng/tilt/internal/ospath"
)
type dockerPathMatcher struct {
repoRoot string
matcher *fileutils.PatternMatcher
}
func (i dockerPathMatcher) Matches(f string) (bool, error) {
rp, err := filepath.Rel(i.repoRoot, f)
if err != nil {
return false, err
}
return i.matcher.Matches(rp)
}
func (i dockerPathMatcher) MatchesEntireDir(f string) (bool, error) {
matches, err := i.Matches(f)
if !matches || err != nil {
return matches, err
}
// We match the dir, but we might exclude files underneath it.
if i.matcher.Exclusions() {
for _, pattern := range i.matcher.Patterns() {
if !pattern.Exclusion() {
continue
}
absPattern := filepath.Join(i.repoRoot, pattern.String())
if ospath.IsChild(f, absPattern) {
// Found an exclusion match -- we don't match this whole dir
return false, nil
}
}
return true, nil
}
return true, nil
}
func NewDockerIgnoreTester(repoRoot string) (*dockerPathMatcher, error) {
absRoot, err := filepath.Abs(repoRoot)
if err != nil {
return nil, err
}
patterns, err := readDockerignorePatterns(absRoot)
if err != nil {
return nil, err
}
return NewDockerPatternMatcher(absRoot, patterns)
}
func NewDockerPatternMatcher(repoRoot string, patterns []string) (*dockerPathMatcher, error) {
absRoot, err := filepath.Abs(repoRoot)
if err != nil {
return nil, err
}
pm, err := fileutils.NewPatternMatcher(patterns)
if err != nil {
return nil, err
}
return &dockerPathMatcher{
repoRoot: absRoot,
matcher: pm,
}, nil
}
func readDockerignorePatterns(repoRoot string) ([]string, error) {
var excludes []string
f, err := os.Open(filepath.Join(repoRoot, ".dockerignore"))
switch {
case os.IsNotExist(err):
return excludes, nil
case err != nil:
return nil, err
}
defer func() { _ = f.Close() }()
return dockerignore.ReadAll(f)
}
func DockerIgnoreTesterFromContents(repoRoot string, contents string) (*dockerPathMatcher, error) {
patterns, err := dockerignore.ReadAll(strings.NewReader(contents))
if err != nil {
return nil, err
}
return NewDockerPatternMatcher(repoRoot, patterns)
}