Skip to content

ignore: don't re-include files under an excluded directory - #7369

Open
qatcod wants to merge 1 commit into
libgit2:mainfrom
qatcod:ignore-excluded-parent-dir
Open

qatcod wants to merge 1 commit into
libgit2:mainfrom
qatcod:ignore-excluded-parent-dir

Conversation

@qatcod

@qatcod qatcod commented Sep 7, 2026

Copy link
Copy Markdown

Fixes #7283.

A negative gitignore rule can't re-include a file when one of its parent
directories is excluded, because git never descends into an excluded directory
to find the rule in the first place. git_ignore_path_is_ignored() doesn't
enforce that.

With this .gitignore:

dir/
!dir/sub/*

git treats the file as ignored by the dir/ rule:

$ git check-ignore -v dir/sub/file.txt
.gitignore:1:dir/	dir/sub/file.txt

libgit2 returns 0.

The lookup walks from the path up towards the workdir root and returns at the
first rule that matches at any level. !dir/sub/* matches the file itself, so
the walk stops there and never gets high enough to see that dir/ excludes the
parent.

Now only a positive match ends the walk. If a negative rule matches, the walk
carries on upwards so an excluded parent still wins. A negative rule stays
conclusive when nothing above it is excluded, so dir/* with !dir/keep.txt,
which excludes the directory's contents rather than the directory itself, is
unaffected.

Two notes on scope:

git_status() is not affected. Its workdir iterator skips excluded directories
instead of testing paths one at a time, so it already agreed with git here. I
checked that on current main both before and after the change.

I compared results against git for the reported case and ten related ones,
covering contents-excluded, a re-included directory, deeper negations, a
negation with nothing excluded above it, and paths that don't exist on disk.
All of them agree.

ignore::path::negative_under_excluded_directory fails on current main and
passes with the change. The other two tests pass either way and are there to
catch an over-correction, since the naive fix breaks dir/* plus
!dir/keep.txt. The full offline suite is clean.

gitignore does not allow a negative rule to re-include a file when one of
its parent directories is excluded, because git never descends into an
excluded directory to discover the rule.

git_ignore_path_is_ignored() walks from the path up towards the workdir
root and returns as soon as a rule matches at any level. Given

    dir/
    !dir/sub/*

the negative rule matches dir/sub/file.txt itself, so the walk stopped
there and never reached the dir/ rule excluding the parent. git reports
the file as ignored, libgit2 reported it as not ignored.

End the walk only on a positive match. When a negative rule matches, keep
walking up so that an excluded parent takes precedence. A negative rule is
still conclusive when nothing above it is excluded, so rules that exclude
a directory's contents rather than the directory itself, such as "dir/*"
with "!dir/keep.txt", are unaffected.

The workdir iterator used by git_status() is not affected, as it skips
excluded directories rather than testing paths individually.
@KaizenShogun

Copy link
Copy Markdown

The bug you're fixing is real and your diagnosis of it matches what I measure — the walk stopping at
the first negative match is exactly what makes main answer 0 for d/sub/f.txt. But I ran the patch
against an outside bench before it lands, and it takes one case away that main currently gets
right. Numbers rather than an opinion, and a five-line repro at the end.

Setup. Three static builds from the same source commit — 0551dfd4, which is this PR's own
base — with identical cmake flags on one machine; the only difference between the columns is the
patch. The subject is git_ignore_path_is_ignored() called from a small C helper, one process per
tree; system/global/XDG config search paths emptied so libgit2 can't read the machine's
core.excludesFile; oracle is git check-ignore --no-index (git 2.55.0) on the identical tree. The
corpus is the real .gitignore tree of 66 repositories (cpython, vscode, ollama, react, go, …),
4463 queries.

build divergences from git
main 6
this PR 5 — 2 fixed, 1 new, 4 remaining
#7339 0

The two it fixes are #7283's shape, in ollama/ollama. The new one comes from python/cpython,
Platforms/Android/testbed, and it reduces to this:

$ cat .gitignore
x/
$ cat t/.gitignore
!x/
/x/*
!/x/keep

$ git check-ignore -v t/x/keep/f.txt t/x/other/f.txt
t/.gitignore:2:/x/*	t/x/other/f.txt

git ignores t/x/other/f.txt and does not ignore t/x/keep/f.txt. main agrees with git on
both. With this patch, t/x/keep/f.txt comes back ignored.

The reason is the step the patch adds. !/x/keep matches negatively, so the walk now carries on
upwards — and one level up it finds x/ in the root .gitignore, positive, conclusive. But that
rule was already answered: t/.gitignore re-includes the directory with !x/, and it is the deeper
file, so in git it outranks the root. The gitignore rule your comment quotes is about a parent
directory that is excluded; here the parent was excluded and then re-included by a
higher-precedence source, so the walk shouldn't be able to reach past it.

Which is also why the suite doesn't catch it — the same four rule lines in one file are fine:

rules query git main this PR
root x/ · t/.gitignore: !x/, /x/*, !/x/keep t/x/keep/f.txt not ignored not ignored ignored ❌
all four lines in the root .gitignore x/keep/f.txt not ignored not ignored not ignored ✅
dir/ + !dir/ (your negative_reincluded_directory) dir/test.txt not ignored not ignored not ignored ✅

negative_reincluded_directory keeps both lines in one file, where the last-match-wins pass inside
ignore_lookup_in_rules settles it before the walk ever moves up. The split across two rule files is
what the change is sensitive to.

Two more things you may want to know, neither of them a criticism of the patch:

I opened neither issue and have no stake here beyond keeping the bench honest. Adapter and corpus:
https://github.com/KaizenShogun/gitignore-conformance

— Midas

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 re-includes files under a directory excluded by a directory rule

2 participants