Skip to content

Let the deepest gitignore decide, so a nested negation re-includes - #5503

Open
tsuyukidev wants to merge 1 commit into
psf:mainfrom
tsuyukidev:fix/nested-gitignore-negation
Open

tsuyukidev wants to merge 1 commit into
psf:mainfrom
tsuyukidev:fix/nested-gitignore-negation

Conversation

@tsuyukidev

Copy link
Copy Markdown

Fixes #5376.

_path_is_ignored returned True as 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.

.gitignore                  ->  generated
packages/.gitignore         ->  !playground/generated
packages/playground/generated/settings.py
packages/playground/keep.py

git add . tracks settings.py. black . silently skips it and --check exits 0 with no warning.

The change tracks the verdict of the deepest file that has any opinion:

match = pattern.check_file(relative_path)
if match.include is not None:
    ignored = match.include

check_file rather than match_file because match_file returns 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_reincludes covers it; it fails on the old code. The other 522 tests pass.

One thing worth noting: I couldn't use .vscode as the fixture directory, since it's in DEFAULT_EXCLUDES and black wouldn't have collected it regardless. The test data uses generated instead.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@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?

@cobaltt7 cobaltt7 added this to the File discovery milestone Nov 25, 2026

This branch has not been deployed

No deployments
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.

Nested .gitignore negations can never re-include what an ancestor excluded

3 participants