Skip to content

worktree: status, Preserve re-included files below /** - #2386

Open
znnnnnnn-wil wants to merge 2 commits into
go-git:mainfrom
znnnnnnn-wil:fix/2369-status-ignore-negation
Open

znnnnnnn-wil wants to merge 2 commits into
go-git:mainfrom
znnnnnnn-wil:fix/2369-status-ignore-negation

Conversation

@znnnnnnn-wil

@znnnnnnn-wil znnnnnnn-wil commented Sep 12, 2026 •

Copy link
Copy Markdown

Summary

Fixes #2369. Worktree.Status() prunes volumes/functions for a rule ending in volumes/functions/**, so a later !volumes/functions/deno.json* cannot expose a matching file. Git's trailing /** excludes the contents, not the parent itself.

Evaluate built-in ignore patterns against the current entry in Scope; excluded ancestors remain handled by Descend. This lets the walk reach re-included files without applying a directory's negation to its descendants. For example, with f/** and !f/deno.json*, a file named f/deno.jsonsample is visible, but f/deno.jsonsample/keep remains ignored when deno.jsonsample is a directory. Earlier explicit exclusions such as f/ still take effect.

Public Pattern.Match and Matcher.Match behavior stays unchanged. No filesystem-walker special case is needed.

Reference behavior

https://git-scm.com/docs/gitignore#_pattern_format documents trailing /** and the restriction on re-including files below excluded directories. The regression tests compare complete untracked sets with git ls-files --others --exclude-standard, including the reported directory-negation cases and nested .gitignore files.

Validation

On Windows, Go 1.26.5:

  • New reference-Git regression cases failed against the previous PR commit and pass with the fix.
  • go test . -run 'TestStatus|TestWorktree.*Status' -count=1: passed.
  • go test ./utils/merkletrie/filesystem -count=1: passed.
  • go test ./plumbing/format/gitignore ./utils/merkletrie/filesystem -short -skip 'TestMatcherSuite/TestDir_ReadRelativeGlobalGitIgnore' -count=1: passed.
  • golangci-lint run: 0 issues.
  • git diff --check: passed.

The unfiltered gitignore package run has two failures also reproduced on the unchanged PR commit: TestDir_ReadRelativeGlobalGitIgnore on this Windows environment, and TestIgnoreDoubleStarPrefix with the installed Git 2.38.1 (the test documents the upstream Git fix in 2.52.0). These are not reported as passing.

Paired runs of gitignore-conformance at 8d202f238ff3827e6e58bdb39a7cee5285f6317b, using its adapter with v6 imports and repository cleanup:

  • Status(): 6,714 file queries across 33 repositories, 5 → 4 divergences compared with main: one existing divergence fixed, with no new regressions. The remaining four apps/docs/.../generated/.gitkeep divergences are pre-existing, identical to main, and untouched by this PR.
  • ReadPatterns + Matcher.Match: 8,953 queries, the same 12 divergences before and after (the same paths, not just the same count).

AI disclosure

Parts of the implementation and tests were assisted by Codex GPT-5 and GPT-6. The validation results above were obtained by running the tests and paired conformance measurements.

…2369

Track when a trailing /** matches an empty directory remainder so Status can
traverse the directory without changing Pattern.Match semantics. Keep pruning
directories that are explicitly excluded or are descendants matched by /**.

Assisted-by: Codex GPT-5 <[email protected]>
Signed-off-by: 唐僧洗发用飘柔 <[email protected]>
@KaizenShogun

Copy link
Copy Markdown

I opened #2369, so read this as an interested party's measurement rather than a review.

Matcher.Match doesn't move: 12 divergences from git check-ignore before and after, the same 12, over 8,953 queries — which is what the commit message promises. Status() goes 5 → 8 over 6,714 queries, compared as sets: it fixes the case I reported and breaks four. Base is da885a30, the parent of the PR commit.

The four share one shape — a negation that re-includes a directory now leaks into that directory's contents, which git doesn't do. Two lines are enough to see it (the real ones come from supabase/supabase's docker/.gitignore):

$ git init -q repro && cd repro
$ printf 'f/**\n!f/deno.json*\n' > .gitignore
$ mkdir -p f/deno.jsonsample && touch f/deno.jsonsample/keep
$ git check-ignore -q f/deno.jsonsample/keep && echo ignored
ignored                     # .gitignore:1:f/**  — and `git status -uall` does not list it

!f/deno.json* re-includes the directory f/deno.jsonsample, so git descends into it, and f/** ignores what's inside. On da885a30 Status() agrees with git here; with this patch the file comes back as untracked. Same for main/_gic_keep under the four-line original, while the case it fixes — !volumes/functions/main/index.ts — is right.

Worth one line: an independent patch for the same bug in another project (treeverse/dvc#11096, Python, different author) breaks those same four paths. Whatever is wrong is in the approach, not in this code.

Bench and adapter: https://github.com/KaizenShogun/gitignore-conformance

Keep trailing /** from excluding its parent, and evaluate rules against
the current entry so re-including a directory does not re-include its
contents. Continue looking at earlier rules when /** does not match the
parent itself. Preserve public Pattern.Match and Matcher.Match behavior.
Add reference-Git regressions for directory negations, nested ignore
files, and an explicitly excluded parent followed by a contents wildcard.

Assisted-by: Codex GPT-6 <[email protected]>
Signed-off-by: 唐僧洗发用飘柔 <[email protected]>
@KaizenShogun

Copy link
Copy Markdown

Re-measured against the new head. The four paths I flagged are gone, and nothing else moved.

Method: 2797bb8f, its parent d0dbb214, and base da885a30 each built through a replace to a worktree of that commit, so the only thing changing between columns is the library. Bench pinned at 8d202f2, the same commit you used. Oracle is git 2.55.0.

build Matcher.Match (8,953 queries) Status() (6,714 queries)
main da885a30 12 5
PR d0dbb214 12 8
PR 2797bb8f 12 4

Compared as sets rather than counts: Status() on the new head fixes all four regressions I reported, introduces none, and keeps the #2369 case (docker/volumes/functions/deno.jsonsample) fixed. The remaining four are the pre-existing **/*/generated/.gitkeep rows, identical to main and untouched by this PR. Matcher.Match is the same 12 rows before and after — same paths, not just the same number.

The two-line repro from my earlier comment now agrees with git on both lines:

$ printf 'f/**\n!f/deno.json*\n' > .gitignore    # + f/deno.json, f/deno.jsonsample/keep
f/deno.json             not ignored   (git: not ignored)
f/deno.jsonsample/keep  ignored       (git: ignored)

One framing note, since it's easy to read the other way: the 8 -> 4 in the description is measured against the previous PR commit. Against main it's 5 -> 4 — one real fix, zero regressions. Which is the number I'd want if I were reviewing this.

@znnnnnnn-wil

Copy link
Copy Markdown
Author

Thanks for re-running the comparison and for pointing out the framing. I've updated the description to compare against main: 5 -> 4 divergences, with one existing divergence fixed and no new regressions.

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.

Status(): a directory matched by X/** is pruned from the walk, so !X/keep never re-includes (regression in 70ab8844)

2 participants