-
Notifications
You must be signed in to change notification settings - Fork 199
/
dockerfile_ignore.go
126 lines (102 loc) · 2.63 KB
/
dockerfile_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package path_matcher
import (
"fmt"
"sort"
"strings"
"github.com/docker/docker/pkg/fileutils"
"github.com/werf/werf/pkg/util"
)
func newDockerfileIgnorePathMatcher(dockerignorePatterns []string) dockerfileIgnorePathMatcher {
m, err := fileutils.NewPatternMatcher(dockerignorePatterns)
if err != nil {
panic(err)
}
return dockerfileIgnorePathMatcher{
patternMatcher: m,
}
}
type dockerfileIgnorePathMatcher struct {
patternMatcher *fileutils.PatternMatcher
}
func (m dockerfileIgnorePathMatcher) IsPathMatched(path string) bool {
path = formatPath(path)
if m.patternMatcher == nil {
return true
}
isMatched, err := m.patternMatcher.Matches(path)
if err != nil {
panic(err)
}
return !isMatched
}
type pattern struct {
pattern string
exclusion bool
isMatched bool
isInProgress bool
}
func (m dockerfileIgnorePathMatcher) IsDirOrSubmodulePathMatched(path string) bool {
return m.IsPathMatched(path) || m.ShouldGoThrough(path)
}
func (m dockerfileIgnorePathMatcher) ShouldGoThrough(path string) bool {
return m.shouldGoThrough(formatPath(path))
}
func (m dockerfileIgnorePathMatcher) shouldGoThrough(path string) bool {
if m.patternMatcher == nil || len(m.patternMatcher.Patterns()) == 0 {
return false
}
if path == "" {
return true
}
pathParts := util.SplitFilepath(path)
var patterns []*pattern
for _, p := range m.patternMatcher.Patterns() {
patterns = append(patterns, &pattern{
pattern: p.String(),
exclusion: p.Exclusion(),
isInProgress: true,
})
}
for _, pathPart := range pathParts {
for _, p := range patterns {
if !p.isInProgress {
continue
}
inProgressGlob, matchedGlob := matchGlob(pathPart, p.pattern)
if inProgressGlob != "" {
p.pattern = inProgressGlob
} else if matchedGlob != "" {
p.isMatched = true
p.isInProgress = false
} else {
p.isInProgress = false
}
}
}
shouldGoThrough := false
for _, pattern := range patterns {
if pattern.isMatched {
shouldGoThrough = false
} else if pattern.isInProgress {
shouldGoThrough = true
}
}
return shouldGoThrough
}
func (m dockerfileIgnorePathMatcher) ID() string {
var cleanedPatterns []string
for _, pattern := range m.patternMatcher.Patterns() {
cleanedPatterns = append(cleanedPatterns, pattern.String())
}
if len(cleanedPatterns) == 0 {
return ""
}
sort.Strings(cleanedPatterns)
var args []string
args = append(args, "dockerfileIgnore")
args = append(args, cleanedPatterns...)
return util.Sha256Hash(strings.Join(args, ":::"))
}
func (m dockerfileIgnorePathMatcher) String() string {
return fmt.Sprintf("{ patternMatcher=%v }", m.patternMatcher.Patterns())
}