Repository navigation
plumbing: gitignore, dirOnly re-include patterns must not cascade to descendants. Fixes #2112 - #2311
Conversation
…descendants. Fixes go-git#2112 A pattern like !dir/ only restores the directory entry itself; it does not implicitly re-include everything nested inside it. simpleNameMatch let a dirOnly inclusion pattern matching an ancestor path component stand in for a match on the full descendant path, so a .gitignore like: * !my-dir/ wrongly reported files under my-dir/ as untracked instead of ignored. Signed-off-by: Sahib Nanda <[email protected]>
TestStatusIgnored regressed after the previous commit: a root-anchored
glob like "vendor/g*/" excludes two sibling directories, and a nested
vendor/.gitignore's "!github.com/" re-includes one of them specifically.
Once that override is resolved, globMatch's "leftover suffix still
counts as matched" behavior (needed so an excluded directory excludes
its descendants) let "vendor/g*/" keep reasserting its old verdict on
files inside the re-included directory, even though its own anchor
("vendor", "g*") was already fully consumed at that depth and had
nothing left to say about anything deeper.
A single Pattern has no way to know this: only Scope carries the
cross-pattern, cross-directory context needed. Scope.Descend now drops
glob patterns whose domain-plus-pattern length is fully consumed by the
directory just resolved, unless they contain "**" (unbounded reach) or
are non-glob (any-depth matching is their entire purpose, per
simpleNameMatch). This is the mirror image of the existing excluded
sticky flag: that one pins an exclude verdict forward past a resolved
boundary; this pins the boundary itself, so a pattern that already had
its say can't reach past it again.
Verified against TestStatusIgnored (now passing again) and a new
Scope-level test, TestScopeOverriddenGlobDoesNotReclaimDescendants,
confirmed against "git status --porcelain --ignored". Hand-traced
against every existing scope_test.go case; none change behavior, since
they either never reach the surviving branch with a droppable pattern
or use non-glob patterns that dropExhausted never touches.
Signed-off-by: Sahib Nanda <[email protected]>
|
The That test covers a case real The second commit adds the missing piece. Verified |
|
The bug you're fixing is real, your diagnosis matches what I measure, and the patch fixes more than the issue asks for. It also takes away one case that Setup. One Go helper ( The corpus is 33 real repositories' whole
Compared as sets, not counts: the two First, what it fixes. #2112, verbatim, and a variant the issue doesn't mention:
That last row is the interesting one, and your patch gets it right. What it breaks. Same shape, one thing changed — the pattern that excluded the directory now matches only the directory: mkdir -p t/sub/.idea && cd t && git init -q
printf '.idea/\n!.idea/\n' > .gitignore
touch sub/.idea/f.txt
git check-ignore -v --no-index -- sub/.idea/f.txt # no match: git does NOT ignore it
The real-world case the bench found is the same thing across two rule files, which is what Where I think the line is. Your write-up frames the fix as "a How much is at stake. Only 2 of 4,463 queries move, so this could read as a corner. But the construct isn't rare: 10 of the 33 repositories in the corpus write at least one I'm an interested party, not a maintainer: I opened nothing here, I ran the bench because I keep one. The tests, the corpus and the go-git adapter are public if you want to reproduce any row: https://github.com/KaizenShogun/gitignore-conformance |
Fixes #2112
(Supersedes #2310, closed — that PR's commit didn't follow the repo's commit-message/DCO conventions; this one carries the same change with a corrected, signed-off commit.)
Problem
Given a
.gitignorecontaining:real
gitkeeps everything undermy-dir/ignored except the directory entry itself is un-excluded for traversal purposes — but files nested inside it (e.g.my-dir/sub/file.txt) remain subject to the*exclusion and stay ignored. go-git instead reported those nested files as untracked, soWorktree.Status()surfaced files that realgit statuswould never show.Root cause
plumbing/format/gitignore/pattern.go,simpleNameMatch:This loop lets a bare (non-glob) pattern match a name at any depth in the path component list — which is correct and necessary for patterns like
node_modulesmatchinga/node_modules/file.txtat any nesting level. The bug: that "ancestor match stands in for the whole descendant path" behavior was applied uniformly, but it's only valid for exclusion patterns (excluding an ancestor directory correctly excludes everything below it too). For adirOnlyinclusion pattern (!dir/), a match against an ancestor component (not the final path element) was also being treated as a match for the whole descendant path — incorrectly resurrecting files nested arbitrarily deep under a re-included directory name, even when nothing else in the pattern list re-includes them specifically.Fix
simpleNameMatchnow treats adirOnlyinclusion pattern's match against a non-final (ancestor) path component as no match for that call, instead of a match for the whole path:Exclusion patterns (
p.inclusion == false) are completely unaffected — an excluded ancestor still correctly excludes its descendants, matching existing behavior and existing tests (e.g.TestSimpleMatch_inTheMiddle_dirWanted).Scope note
I verified this fix against the real
gitbinary (this package already has aconformance_test.goharness that cross-checks againstgit check-ignore/git statuswhen available). While investigating, I found realgithas a separate, narrower quirk: a bare*pattern specifically resists being overridden by any later negation for the exact directory entry it matched (i.e. even!my-dir/alone, with no nested descendants involved, doesn't un-ignoremy-diritself when preceded by*— confirmed withgit status --porcelain --ignored). Other exclude patterns (a literal name, or a non-*wildcard likem*) don't have this quirk; negation works normally against them. That's unrelated to the cascading-into-descendants bug this issue reports, so I deliberately left it alone and didn't assert on it in the new tests, to keep this PR minimal and precisely scoped to #2112.Tests added
plumbing/format/gitignore/pattern_test.go: unit-level coverage of the pattern in isolation (ancestor match no longer cascades; the directory itself, matched directly, still returnsInclude).plumbing/format/gitignore/conformance_test.go: a new case cross-checked against the realgitbinary via the existing oracle harness.worktree_status_test.go: an end-to-end regression test reproducing the original report throughWorktree.Status().Testing performed
go test ./plumbing/format/gitignore/...— passes (one pre-existing, unrelatedTestIgnoreDoubleStarPrefixoracle mismatch confirmed present on unmodifiedmaintoo, viagit stash).go test .— no new failures versus a baseline run on unmodifiedmain(same pre-existing, environment-specific failures on both, verified by diffing the failing-test lists).go vet ./...— clean (one pre-existing, unrelated warning in Windows SSH agent code, not part of this change).