Repository navigation
Let the deepest gitignore decide, so a nested negation re-includes - #5503
tsuyukidev wants to merge 1 commit into
Conversation
chrikrah
left a comment
There was a problem hiding this comment.
@tsuyukidev requesting changes at 2a756ad. Your change fixes the #5376 case and implements the rule proposed there, so this isn't a misreading on your side. That rule also formats a file that git ignores and that main skips.
blocking: a nested !playground/build_cache now overrides an unrelated ancestor pattern for every file in that directory. pathspec lets a directory pattern match its descendants, and git does not. Git applies that negation to the directory only:
$ head .gitignore pkg/.gitignore
==> .gitignore <==
build_cache
*_pb2.py
==> pkg/.gitignore <==
!playground/build_cache
$ git check-ignore -v --no-index pkg/playground/build_cache/msg_pb2.py
.gitignore:2:*_pb2.py pkg/playground/build_cache/msg_pb2.py
$ black --check -v . # 2a756ad, pathspec 1.1.1
would reformat pkg/playground/build_cache/msg_pb2.py
would reformat pkg/playground/build_cache/ok.py
$ black --check -v . # files.py from merge base c9b1148
pkg/playground/build_cache ignored: matches a .gitignore file content
ok.py shows the #5376 fix. msg_pb2.py shows the regression. A comment on #5453 reports the same case: #5453 (comment). No existing gitignore fixture puts a second, independent pattern in the ancestor file.
$ python -m pytest tests/test_black.py -q -n 4 -k gitignore
12 passed # 2a756ad
1 failed, 11 passed # files.py from c9b1148, your test kept: test_nested_gitignore_negation_reincludes
One direction: let a nested negation decide only when it matches the path itself, not a parent directory. @JelleZijlstra you merged #5497 into this gitignore code. Is git parity for nested negations the target here, given that #5376's rule and git disagree on this case?
Fixes #5376.
_path_is_ignoredreturnedTrueas soon as any gitignore matched, iterating least-specific to most-specific. That ORs the files together, so the deepest one — the only one that can say!— is often never consulted. Git does the opposite: the deepest file with a matching pattern wins, and only inside that file does last-match-win apply.git add .trackssettings.py.black .silently skips it and--checkexits 0 with no warning.The change tracks the verdict of the deepest file that has any opinion:
check_filerather thanmatch_filebecausematch_filereturns a bare bool, which cannot distinguish a negation (include=False) from no match at all (include=None). Only the former should override an ancestor.Last-match-wins within a single file was already handled by pathspec and is unaffected.
test_nested_gitignore_negation_reincludescovers it; it fails on the old code. The other 522 tests pass.One thing worth noting: I couldn't use
.vscodeas the fixture directory, since it's inDEFAULT_EXCLUDESand black wouldn't have collected it regardless. The test data usesgeneratedinstead.