Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

BlackHole1
Copy link

@BlackHole1 BlackHole1 commented Jun 30, 2023

@BlackHole1
Copy link
Author

BlackHole1 commented Jun 30, 2023

pkg/fileutils/fileutils.go Outdated Show resolved Hide resolved
pkg/fileutils/fileutils.go Show resolved Hide resolved
Add `CanSkipDir` to `MatchesResult`

Signed-off-by: Black-Hole1 <bh@bugs.cc>
@BlackHole1
Copy link
Author

Thank you for your CR @vrothberg. Modification Completed.

}
}
}

if res.matches > 0 {
if res.isMatched {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous implementation had a bug. When the rule is ["1", "!1"], it would also enter this conditional statement, but it should not.

Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! @saschagrunert PTAL

@BlackHole1 BlackHole1 changed the title refactor(pattern-matcher): add CanSkipDir method in patternMatcher feat(pattern-matcher): add CanSkipDir method in patternMatcher Jun 30, 2023
@rhatdan
Copy link
Member

rhatdan commented Jul 4, 2023

LGTM

@rhatdan
Copy link
Member

rhatdan commented Jul 5, 2023

@nalind
Copy link
Member

nalind commented Jul 5, 2023

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.

@BlackHole1
Copy link
Author

BlackHole1 commented Jul 5, 2023

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},

@nalind
Copy link
Member

nalind commented Jul 5, 2023

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.

@BlackHole1
Copy link
Author

@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
Copy link
Author

@nalind Ping.

@rhatdan
Copy link
Member

rhatdan commented Sep 11, 2023

@nalind PTAL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants