Skip to content

Honor .gitignore negations of directories (#3694) - #5306

Open
l46983284-cpu wants to merge 1 commit into
psf:mainfrom
l46983284-cpu:fix-3694-gitignore-dir-negation
Open

Honor .gitignore negations of directories (#3694)#5306
l46983284-cpu wants to merge 1 commit into
psf:mainfrom
l46983284-cpu:fix-3694-gitignore-dir-negation

Conversation

@l46983284-cpu

Copy link
Copy Markdown
Contributor

Fixes #3694.

pathspec's GitIgnoreSpec gives file-pattern matches precedence over
directory-pattern matches regardless of their order in the .gitignore
file, and its directory patterns match whole subtrees. Git instead
lets the last matching pattern decide and matches directory patterns
only against the directory itself. Because of that, a negated
directory pattern like !*/ never re-included a directory ignored by
an earlier *, and Black pruned the directory and silently skipped
every tracked file below it.

Use check_file() for the ignore decision, and for trailing-slash
paths apply a last-match-wins scan over the later patterns with an
end-of-path match criterion, reproducing git's exact-directory
semantics. A regression test uses the exact fixture from the issue.

One pre-existing divergence is intentionally left as-is: a negation in
a nested .gitignore cannot re-include a file under a directory ignored
at the root (root-decides-first traversal), which Black already did
not do before this change. Getting that right likely belongs in
pathspec/traversal upstream and is a separate follow-up.

pathspec's GitIgnoreSpec gives file-pattern matches precedence over
directory-pattern matches regardless of order, so `!*/` never
re-included a directory ignored by `*`. Black then pruned the
directory and silently skipped every file below it. Use
check_file() plus a last-match-wins scan over later patterns for
trailing-slash paths, matching what git check-ignore does.

Signed-off-by: Alex Chen <l46983284@gmail.com>
@l46983284-cpu
l46983284-cpu force-pushed the fix-3694-gitignore-dir-negation branch from 09266d3 to bebbb16 Compare August 12, 2026 16:25
@github-actions

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (bebbb16) to main (74371e2):

--preview style: no changes

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

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.

gitignore patterns with trailing slashes are incorrectly handled

1 participant