-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Don't descend into ignored directories when building context #863
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
Merged
+62
−27
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,38 +107,68 @@ def exclude_paths(root, patterns, dockerfile=None): | |
|
||
exclude_patterns = list(set(patterns) - set(exceptions)) | ||
|
||
all_paths = get_paths(root) | ||
|
||
# Remove all paths that are matched by any exclusion pattern | ||
paths = [ | ||
p for p in all_paths | ||
if not any(match_path(p, pattern) for pattern in exclude_patterns) | ||
] | ||
|
||
# Add back the set of paths that are matched by any inclusion pattern. | ||
# Include parent dirs - if we add back 'foo/bar', add 'foo' as well | ||
for p in all_paths: | ||
if any(match_path(p, pattern) for pattern in include_patterns): | ||
components = p.split('/') | ||
paths += [ | ||
'/'.join(components[:end]) | ||
for end in range(1, len(components) + 1) | ||
] | ||
paths = get_paths(root, exclude_patterns, include_patterns, | ||
has_exceptions=len(exceptions) > 0) | ||
|
||
return set(paths) | ||
|
||
|
||
def get_paths(root): | ||
def should_include(path, exclude_patterns, include_patterns): | ||
""" | ||
Given a path, a list of exclude patterns, and a list of inclusion patterns: | ||
1. Returns True if the path doesn't match any exclusion pattern | ||
2. Returns False if the path matches an exclusion pattern and doesn't match | ||
an inclusion pattern | ||
3. Returns true if the path matches an exclusion pattern and matches an | ||
inclusion pattern | ||
""" | ||
for pattern in exclude_patterns: | ||
if match_path(path, pattern): | ||
for pattern in include_patterns: | ||
if match_path(path, pattern): | ||
return True | ||
return False | ||
return True | ||
|
||
|
||
def get_paths(root, exclude_patterns, include_patterns, has_exceptions=False): | ||
paths = [] | ||
|
||
for parent, dirs, files in os.walk(root, followlinks=False): | ||
for parent, dirs, files in os.walk(root, topdown=True, followlinks=False): | ||
parent = os.path.relpath(parent, root) | ||
if parent == '.': | ||
parent = '' | ||
|
||
# If exception rules exist, we can't skip recursing into ignored | ||
# directories, as we need to look for exceptions in them. | ||
# | ||
# It may be possible to optimize this further for exception patterns | ||
# that *couldn't* match within ignored directores. | ||
# | ||
# This matches the current docker logic (as of 2015-11-24): | ||
# https://github.com/docker/docker/blob/37ba67bf636b34dc5c0c0265d62a089d0492088f/pkg/archive/archive.go#L555-L557 | ||
|
||
if not has_exceptions: | ||
|
||
# Remove excluded patterns from the list of directories to traverse | ||
# by mutating the dirs we're iterating over. | ||
# This looks strange, but is considered the correct way to skip | ||
# traversal. See https://docs.python.org/2/library/os.html#os.walk | ||
|
||
dirs[:] = [d for d in dirs if | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. live mutation of the |
||
should_include(os.path.join(parent, d), | ||
exclude_patterns, include_patterns)] | ||
|
||
for path in dirs: | ||
paths.append(os.path.join(parent, path)) | ||
if should_include(os.path.join(parent, path), | ||
exclude_patterns, include_patterns): | ||
paths.append(os.path.join(parent, path)) | ||
|
||
for path in files: | ||
paths.append(os.path.join(parent, path)) | ||
if should_include(os.path.join(parent, path), | ||
exclude_patterns, include_patterns): | ||
paths.append(os.path.join(parent, path)) | ||
|
||
return paths | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this could use a docstring.