Skip to content

Commit 97ede9d

Browse files
author
Aaron Lehmann
committed
Rename Matches to MatchesOrParentMatches
Signed-off-by: Aaron Lehmann <alehmann@netflix.com>
1 parent 9bae4f2 commit 97ede9d

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

builder/remotecontext/detect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func removeDockerfile(c modifiableContext, filesToRemove ...string) error {
130130
f.Close()
131131
filesToRemove = append([]string{".dockerignore"}, filesToRemove...)
132132
for _, fileToRemove := range filesToRemove {
133-
if rm, _ := fileutils.Matches(fileToRemove, excludes); rm {
133+
if rm, _ := fileutils.MatchesOrParentMatches(fileToRemove, excludes); rm {
134134
if err := c.Remove(fileToRemove); err != nil {
135135
logrus.Errorf("failed to remove %s: %v", fileToRemove, err)
136136
}

pkg/archive/archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error)
860860
if len(parentMatched) != 0 {
861861
skip, err = pm.MatchesUsingParentResult(relFilePath, parentMatched[len(parentMatched)-1])
862862
} else {
863-
skip, err = pm.Matches(relFilePath)
863+
skip, err = pm.MatchesOrParentMatches(relFilePath)
864864
}
865865
if err != nil {
866866
logrus.Errorf("Error matching %s: %v", relFilePath, err)

pkg/fileutils/fileutils.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,56 @@ func NewPatternMatcher(patterns []string) (*PatternMatcher, error) {
6161
// The "file" argument should be a slash-delimited path.
6262
//
6363
// Matches is not safe to call concurrently.
64+
//
65+
// This implementation is buggy (it only checks a single parent dir against the
66+
// pattern) and will be removed soon. Use either MatchesOrParentMatches or
67+
// MatchesUsingParentResult instead.
6468
func (pm *PatternMatcher) Matches(file string) (bool, error) {
6569
matched := false
6670
file = filepath.FromSlash(file)
6771
parentPath := filepath.Dir(file)
6872
parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
6973

74+
for _, pattern := range pm.patterns {
75+
// Skip evaluation if this is an inclusion and the filename
76+
// already matched the pattern, or it's an exclusion and it has
77+
// not matched the pattern yet.
78+
if pattern.exclusion != matched {
79+
continue
80+
}
81+
82+
match, err := pattern.match(file)
83+
if err != nil {
84+
return false, err
85+
}
86+
87+
if !match && parentPath != "." {
88+
// Check to see if the pattern matches one of our parent dirs.
89+
if len(pattern.dirs) <= len(parentPathDirs) {
90+
match, _ = pattern.match(strings.Join(parentPathDirs[:len(pattern.dirs)], string(os.PathSeparator)))
91+
}
92+
}
93+
94+
if match {
95+
matched = !pattern.exclusion
96+
}
97+
}
98+
99+
return matched, nil
100+
}
101+
102+
// MatchesOrParentMatches returns true if "file" matches any of the patterns
103+
// and isn't excluded by any of the subsequent patterns.
104+
//
105+
// The "file" argument should be a slash-delimited path.
106+
//
107+
// Matches is not safe to call concurrently.
108+
func (pm *PatternMatcher) MatchesOrParentMatches(file string) (bool, error) {
109+
matched := false
110+
file = filepath.FromSlash(file)
111+
parentPath := filepath.Dir(file)
112+
parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
113+
70114
for _, pattern := range pm.patterns {
71115
// Skip evaluation if this is an inclusion and the filename
72116
// already matched the pattern, or it's an exclusion and it has
@@ -249,6 +293,9 @@ func (p *Pattern) compile() error {
249293

250294
// Matches returns true if file matches any of the patterns
251295
// and isn't excluded by any of the subsequent patterns.
296+
//
297+
// This implementation is buggy (it only checks a single parent dir against the
298+
// pattern) and will be removed soon. Use MatchesOrParentMatches instead.
252299
func Matches(file string, patterns []string) (bool, error) {
253300
pm, err := NewPatternMatcher(patterns)
254301
if err != nil {
@@ -264,6 +311,23 @@ func Matches(file string, patterns []string) (bool, error) {
264311
return pm.Matches(file)
265312
}
266313

314+
// MatchesOrParentMatches returns true if file matches any of the patterns
315+
// and isn't excluded by any of the subsequent patterns.
316+
func MatchesOrParentMatches(file string, patterns []string) (bool, error) {
317+
pm, err := NewPatternMatcher(patterns)
318+
if err != nil {
319+
return false, err
320+
}
321+
file = filepath.Clean(file)
322+
323+
if file == "." {
324+
// Don't let them exclude everything, kind of silly.
325+
return false, nil
326+
}
327+
328+
return pm.MatchesOrParentMatches(file)
329+
}
330+
267331
// CopyFile copies from src to dst until either EOF is reached
268332
// on src or an error occurs. It verifies src exists and removes
269333
// the dst if it exists.

pkg/fileutils/fileutils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,12 @@ func TestMatches(t *testing.T) {
382382
}...)
383383
}
384384

385-
t.Run("Matches", func(t *testing.T) {
385+
t.Run("MatchesOrParentMatches", func(t *testing.T) {
386386
for _, test := range tests {
387387
desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
388388
pm, err := NewPatternMatcher([]string{test.pattern})
389389
assert.NilError(t, err, desc)
390-
res, _ := pm.Matches(test.text)
390+
res, _ := pm.MatchesOrParentMatches(test.text)
391391
assert.Check(t, is.Equal(test.pass, res), desc)
392392
}
393393
})

0 commit comments

Comments
 (0)