Skip to content

ignore: honor nested negation across rule sources - #7339

Open
Boulea7 wants to merge 5 commits into
libgit2:mainfrom
Boulea7:fix/nested-ignore-negation
Open

Boulea7 wants to merge 5 commits into
libgit2:mainfrom
Boulea7:fix/nested-ignore-negation

Conversation

@Boulea7

@Boulea7 Boulea7 commented Aug 10, 2026 •

Copy link
Copy Markdown

Summary

Fix ignore negation when the excluding and re-including rules come from different sources.

A nested .gitignore can now re-include a directory excluded by a parent .gitignore, matching core Git. Higher-precedence internal and per-directory rules can also override lower-precedence info/exclude or global excludes. Descendants of an ignored parent remain ignored unless the parent itself is re-included.

The parser now retains negation rules regardless of whether the matching positive rule is in the same source. Path lookup continues to parent components after a negative match, while the workdir iterator preserves the ignored state inherited from an ignored parent.

Closes #7284.

Earlier validation

These results were reported before the tests-only workdir-scan follow-up; they were not all rerun for this update.

  • Debug targeted ignore::path and ignore::status: 84/84 passed.
  • Git CLI compatibility matrix: 252 scenarios, 378 paths, 0 mismatches.
  • Release non-network CTest: 7/7 passed.
  • ASan full offline and util CTest: 2/2 passed.
  • ASan+UBSan targeted ignore tests: 84/84 passed.
  • Werror compilation for the two touched source objects: 2/2 passed.

A local 10,000-file iterator benchmark, after warmup, measured approximately 0.107 s at ignore depth 1 and 0.235 s at depth 80. The previous implementation measured approximately 0.952 s at depth 80.

The online suite was attempted but did not complete because network operations timed out.

Earlier broad runs also reported two failures outside the changed files:

  • The combined-sanitizer full run reports a misaligned uint32_t access in the unchanged bundled sha1dc/sha1.c.
  • The full Werror build reports a maybe-uninitialized warning in the unchanged deps/reftable/stack.c.

Workdir regression and current validation

A tests-only regression now checks a directory excluded by .git/info/exclude and re-included by .gitignore through the collapsed and recursive status views and git_diff_tree_to_workdir. With the same test added to main at 0551dfd4, the collapsed status list has one entry instead of the expected two; it passes on this branch.

For this follow-up, the repository-default RelWithDebInfo GCC build and local non-network offline/util CTest targets passed (2/2). The focused and related Clang ASan+UBSan tests passed, with two skips. A Debug full-suite run still fails core::structinit::compare on both the previous PR head (7fb8a264) and this follow-up; the repository's non-Debug configuration skips that comparison.

Copilot AI lite review requested due to automatic review settings August 10, 2026 15:46

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@KaizenShogun

Copy link
Copy Markdown

I measured this PR against an outside bench before saying anything about it, because a change that
deletes 169 lines from ignore.c deserves a number that doesn't come from the tests shipping in the
same commit. This isn't a review of the code — I read the diff but I did not audit it. It's the
behaviour, against git.

Setup. Three static builds from the same source commit, 0551dfd4 — same cmake flags, same
compiler, same machine; the only difference between them is the patch:

build what it is
main 0551dfd4 untouched
pr7339 0551dfd4 + this PR's .diff, applied clean
pr7369 0551dfd4 + #7369's .diff, applied clean (it is that commit's base)

The subject is git_ignore_path_is_ignored(), asked through a ~60-line C helper: one process per
tree, one query per line. The system/global/XDG config search paths are emptied so libgit2 can't
read this machine's core.excludesFile, and the repository is initialised by libgit2 itself, so it
sees exactly the tree that was written. The oracle is git check-ignore --no-index (git 2.55.0) on
the identical tree.

The corpus is the .gitignore tree of 66 real repositories — cpython, vscode, ollama, react, go,
linux and so on — with the queries generated per repository from the rules they actually contain.

Corpus A — 66 repositories, 4463 queries, no .git/info/exclude:

build divergences from git
main 6
this PR 0
#7369 5

Compared as sets rather than counts, because 6 → 0 could hide a fix and a break cancelling out:
6 fixed, 0 new, 0 remaining. A perfect score here is also the strongest control I have that the
bench isn't inventing divergences — the harness, the adapter, the oracle and the corpus are byte-for
byte the same in all three columns, and only your patch moves the number.

Corpus B — .git/info/exclude, 33 repositories, 99 queries:

build divergences
main 33
this PR 0
#7369 33

This half isn't mentioned in the PR description or covered by the tests it adds, so it's worth
naming what it is. All 33 are the same shape, and it reduces to five lines:

$ cat .gitignore
!a
$ cat .git/info/exclude
a
b
$ git check-ignore -v a b
.gitignore:1:!a	a
.git/info/exclude:2:b	b

git does not ignore a: .gitignore outranks info/exclude, and the ! in the higher-precedence
source wins. main reports a as ignored. With this PR it reports it as not ignored, matching git.

Two paired controls, so the claim is about the right thing:

  • swap the sources — .gitignore says c, info/exclude says !c — and git ignores c
    (the higher-precedence source still wins, it just happens to be the positive one). main gets
    that one right, so this is not a stacking order that's simply inverted; it's specifically
    that a negation was never allowed to cross out of its own source.
  • put both lines in one file (d then !d) and every build agrees with git. The negation isn't
    the problem on its own.

The exclude file in that corpus is synthetic — the three lines are written by the corpus builder,
because a real repository can't ship one — and the rule trees around it are real. Denominator is
separate from corpus A's for that reason.

It closes #7283 as well, not just #7284. Both issue bodies, run verbatim:

repro git main this PR #7369
#7284 — root **/vendor/, a/.gitignore !vendor, query a/vendor/f.txt not ignored ignored ❌ not ignored ✅ ignored ❌
#7283 — d/ + !d/sub/*, query d/sub/f.txt ignored not ignored ❌ ignored ✅ ignored ✅

What this does not tell you. The bench only ever calls git_ignore_path_is_ignored(). This PR
also touches iterator.c, so everything reaching the ignore machinery through git_status and the
workdir iterator is outside what I measured, and that's where I'd want a reviewer's eyes rather than
mine. Your CI is 18/18 green on 7fb8a264; that's the suite's opinion, and the above is a second
one, taken from repositories nobody wrote a test for.

Bench and adapter, if anyone wants to re-run it or disagree with how it asks:
https://github.com/KaizenShogun/gitignore-conformance — adapters/libgit2_adapter.py is the whole
subject-side of it and is about a hundred lines.

— Midas

@Boulea7

Boulea7 commented Sep 26, 2026

Copy link
Copy Markdown
Author

I added a workdir-scan regression for .git/info/exclude excluding vendor/ and .gitignore re-including it. It checks collapsed and recursive status results plus the recursive tree-to-workdir diff. With the same test applied to main at 0551dfd4, the collapsed status list has one entry instead of two; it passes on this PR branch.

@UtsavBalar1231

Copy link
Copy Markdown

I independently validated this for #7284 by applying the diff from 6f3edd88df31d4303503ed6d284c0c24d65cb906 to upstream main at 142570e7bec074fd845aa654e61dcfb1d47caa60.

I added a local Clar regression, ignore::status::nested_negations. The root ignores dist/ and blocked/, while gui/.gitignore contains !/dist/ and !/blocked/keep.txt. A tracked sibling forces the status walk into gui/blocked/. The test checks the direct ignore results and the complete untracked status listing. It fails on main at gui/dist/app.js and passes with this PR applied. All 86 tests in the ignore suite passed with the regression included.

I also compared both builds against Git 2.56.1 across 157 fixtures, including the four-file generated bundle and cases involving nested rules, excluded parents, and lower-precedence exclude files.

Comparison with Git Main Main + #7339
git check-ignore --no-index differences across 1,727 path queries 157 0
git ls-files --others --exclude-standard differences across 157 untracked listings 37 0

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.

Negation in a nested .gitignore fails to re-include a directory excluded by a parent .gitignore

4 participants