storage icon indicating copy to clipboard operation
storage copied to clipboard

feat(pattern-matcher): add `CanSkipDir` method in patternMatcher

Open BlackHole1 opened this issue 2 years ago • 10 comments

Add CanSkipDir to MatchesResult

This PR is mainly aimed at addressing https://github.com/containers/podman/blob/fca3c2ef8476669825564af89b008946bdf87599/pkg/bindings/images/build.go#L699-L701

BlackHole1 avatar Jun 30 '23 01:06 BlackHole1

@rhatdan @giuseppe @vrothberg @saschagrunert PTAL

BlackHole1 avatar Jun 30 '23 01:06 BlackHole1

Thank you for your CR @vrothberg. Modification Completed.

BlackHole1 avatar Jun 30 '23 08:06 BlackHole1

LGTM

rhatdan avatar Jul 04 '23 11:07 rhatdan

@giuseppe @nalind @vrothberg @saschagrunert @mtrmac PTAL

rhatdan avatar Jul 05 '23 11:07 rhatdan

The comment in the podman source is incorrect - podman only needs to (and by that I mean should) descend into a directory that's covered by an ignore pattern if the directory exactly matches the initial portion of a '!' pattern - it doesn't need to descend into directories that might only match a '!' pattern by matching using wild cards. I don't think that this will help podman determine if that's the case.

nalind avatar Jul 05 '23 13:07 nalind

Hey, @nalind. I didn't understand what you meant. Could you please provide a few examples to illustrate your point?

If you could provide an example in the format of a unit test, it would be even better. For example: {[]string{"/prefix*", "!prefix/path"}, "prefix/path", 0, 1, false, false},

BlackHole1 avatar Jul 05 '23 13:07 BlackHole1

Hey, @nalind. I didn't understand what you meant. Could you please provide a few examples to illustrate your point?

If you could provide an example in the format of a unit test, it would be even better. For example: {[]string{"/prefix*", "!prefix/path"}, "prefix/path", 0, 1, false, false},

Specifically, if I have a directory in my build context named "foo" with a file in it named "bar", and my ignores list is ["f*", "!f*/*"], code that wants to do things the way docker build does shouldn't descend into "foo" to pick up "foo/bar". If, on the other hand, the ignores list is ["f*", "!foo/*"], the directory should be scanned, because there's a "!" pattern which has the directory's name, as an exact prefix, after a pattern which would have caused it to be skipped.

In this version of the PR, the last test case in TestMatchesAmount() would probably be {[]string{"/prefix*", "!prefix*/path"}, "prefix/path", 0, 1, false, true}, but take that with the caveat that the goal for this code isn't to be strictly correct, but to be compatible with what docker build does, which has some odd corner cases, including a couple that I've only just noticed that buildah's conformance tests weren't exercising.

nalind avatar Jul 05 '23 21:07 nalind

@nalind I think I may understand the meaning of what you said. Here are my arguments:

TL;DR CanSkipDir only needs to ensure that it is correct when set to true. In this PR, I have a comment:

Only when the pattern is very simple (does not start with ! and does not contain ? and *) and is not influenced by other patterns will it return true.

This comment explains under what conditions CanSkipDir will be true. When CanSkipDir is false, it may be inaccurate, but it doesn't matter. For example, I have a rule: node_modules/*, and it will also return false when matching node_modules. When CanSkipDir is false, it doesn't cause any issues. Simply put, its behavior remains the same as before. When CanSkipDir is true, it must be guaranteed to be 100% correct. If it cannot be guaranteed, it will introduce bugs.

Therefore, in this PR, the conditions for setting it to true are very strict: the filePath passed in must be a normal string and not a glob string (determined by * and ?), and the filePath must be matched by only one rule throughout the entire .dockerignore file. If there are two or more rules that can be applied to filePath, CanSkipDir will return false.

BlackHole1 avatar Jul 06 '23 01:07 BlackHole1

@nalind Ping.

BlackHole1 avatar Jul 18 '23 05:07 BlackHole1

@nalind PTAL

rhatdan avatar Sep 11 '23 16:09 rhatdan