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
Compare each .gitignore found with an appropiate relative path #3338
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f265a96
Apply .gitignore files considering their location
aaossa 9782c33
Test .gitignore with `*/*` is applied correctly
aaossa f97cccd
Update CHANGES.md
aaossa 320f582
Merge branch 'main' into issue-3315
aaossa 5aee3b7
Small refactor to improve code readability
aaossa c09a816
Make nested function a top-level function
aaossa File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -198,7 +198,7 @@ def gen_python_files( | |
extend_exclude: Optional[Pattern[str]], | ||
force_exclude: Optional[Pattern[str]], | ||
report: Report, | ||
gitignore: Optional[PathSpec], | ||
gitignore_dict: Optional[Dict[Path, PathSpec]], | ||
*, | ||
verbose: bool, | ||
quiet: bool, | ||
|
@@ -211,15 +211,27 @@ def gen_python_files( | |
|
||
`report` is where output about exclusions goes. | ||
""" | ||
|
||
def is_ignored( | ||
aaossa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
gitignore_dict: Dict[Path, PathSpec], child: Path, report: Report | ||
) -> bool: | ||
for _dir, _gitignore in gitignore_dict.items(): | ||
relative_path = normalize_path_maybe_ignore(child, _dir, report) | ||
if relative_path is None: | ||
break | ||
if _gitignore is not None and _gitignore.match_file(relative_path): | ||
report.path_ignored(child, "matches the .gitignore file content") | ||
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. Maybe include the (full) path to the .gitignore file that matched? |
||
return True | ||
return False | ||
|
||
assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}" | ||
for child in paths: | ||
normalized_path = normalize_path_maybe_ignore(child, root, report) | ||
if normalized_path is None: | ||
continue | ||
|
||
# First ignore files matching .gitignore, if passed | ||
if gitignore is not None and gitignore.match_file(normalized_path): | ||
report.path_ignored(child, "matches the .gitignore file content") | ||
if gitignore_dict is not None and is_ignored(gitignore_dict, child, report): | ||
continue | ||
|
||
# Then ignore with `--exclude` `--extend-exclude` and `--force-exclude` options. | ||
|
@@ -244,6 +256,13 @@ def gen_python_files( | |
if child.is_dir(): | ||
# If gitignore is None, gitignore usage is disabled, while a Falsey | ||
# gitignore is when the directory doesn't have a .gitignore file. | ||
if gitignore_dict is not None: | ||
new_gitignore_dict = { | ||
**gitignore_dict, | ||
root / child: get_gitignore(child), | ||
} | ||
else: | ||
new_gitignore_dict = None | ||
yield from gen_python_files( | ||
child.iterdir(), | ||
root, | ||
|
@@ -252,7 +271,7 @@ def gen_python_files( | |
extend_exclude, | ||
force_exclude, | ||
report, | ||
gitignore + get_gitignore(child) if gitignore is not None else None, | ||
new_gitignore_dict, | ||
verbose=verbose, | ||
quiet=quiet, | ||
) | ||
|
Empty file.
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
*/* |
Empty file.
Empty file.
This file contains 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.
What if there are more than two levels? For example, we're formatting
a/b/c/
anda/.gitignore/
,a/b/.gitignore
, anda/b/c/.gitignore
all exist.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.
Good question. Right now, if the command is
black a/b/c/
thena/b/.gitignore
is completely skipped whilea/.gitignore
anda/b/c/.gitignore
are captured byroot_gitignore
andp_gitignore
, respectively. What's the expected behavior?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.
Should this be a separate issue/PR? If its an undefined behavior, then it should be documented properly I think
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.
The current behavior seems to be that
black a/b/c
only detectsa/b/c/.gitignore
and completely ignores other gitignore files. This PR implements the same behavior