Skip to content

plumbing: format/gitignore, Backtrack non-trailing ** expansions. Fixes #2317 - #2318

Open
codeAnqiang-ma wants to merge 1 commit into
go-git:mainfrom
codeAnqiang-ma:fix/gitignore-doublestar-backtracking
Open

codeAnqiang-ma wants to merge 1 commit into
go-git:mainfrom
codeAnqiang-ma:fix/gitignore-doublestar-backtracking

Conversation

@codeAnqiang-ma

Copy link
Copy Markdown

Fixes #2317

What

In plumbing/format/gitignore, globMatch handled a non-trailing ** by locking onto the first path component that matched the following segment. When a later segment then failed, the whole match was rejected without retrying the remaining candidates, so patterns like **/a/b did not match a/x/a/b (or **/node_modules/lodash vs web/node_modules/x/node_modules/lodash) even though git check-ignore ignores them.

This PR threads the segment slice through globMatch and, at a non-trailing **, recurses over every candidate expansion instead of committing to the first one. All other behaviour is preserved line-for-line: empty segments still reset traversal, a trailing ** still requires a remaining component or a directory endpoint (#2278), the "files matching dir globs don't match" rule and the final dirOnly check are untouched.

Why this shape

** is documented as matching zero or more directories (gitignore(5)), i.e. an existential choice — a correct matcher must be able to retry longer expansions when a shorter one dead-ends. Canonical git implements this with recursion in wildmatch.c (the ** branch of dowild tries the remainder at every position). Recursing on the existing segment matcher is the minimal equivalent here and keeps the subtle semantics that previous fixes (#2278, #154) encoded in this function.

The new conformance cases run against the git check-ignore oracle like the rest of the suite. These patterns keep ** in the documented positions, so they are unaffected by the pre-2.52 wildmatch corner case the suite already guards against.

Verification against real git

$ git init -q repro && cd repro
$ echo '**/a/b' > .gitignore
$ git check-ignore -v a/x/a/b
.gitignore:1:**/a/b	a/x/a/b        # ignored (git 2.48.1; same on 2.39.5)

go-git on main returns no match for the same input; with this fix it matches.

Tests

Regression tests fail on main before the fix and pass with it:

$ go test -run 'TestConformanceSuite/(TestIgnoreDoubleStarBacktracking|TestIgnoreDoubleStarMiddleBacktracking)' -v ./plumbing/format/gitignore/
--- PASS: TestConformanceSuite (0.45s)
    --- PASS: TestConformanceSuite/TestIgnoreDoubleStarBacktracking (0.27s)
    --- PASS: TestConformanceSuite/TestIgnoreDoubleStarMiddleBacktracking (0.17s)

# before the fix (pattern.go stashed):
    --- FAIL: TestConformanceSuite/TestIgnoreDoubleStarBacktracking
    --- FAIL: TestConformanceSuite/TestIgnoreDoubleStarMiddleBacktracking

Full run: go test -short ./... passes on darwin/arm64 with Go 1.26.5, except three tests that need network access to github.com/git-fixtures and hit a transient TLS timeout; all three passed on a re-run (TestFetchByHashThenResolveRevision, TestWorktreeSuite/TestCheckoutRelativePathSubmoduleInitialized, TestRepositorySuite/TestCloneMirror). Not run: plumbing/transport/git (requires a local git daemon, per AGENTS.md). go vet and gofmt are clean. The full (non--short) gitignore conformance suite passes against the git 2.52+ oracle expectations; on this machine's older git the pre-existing TestIgnoreDoubleStarPrefix oracle note applies, unrelated to this change.

Disclosure

This contribution was produced with AI assistance (Claude Fable 5, via Cursor); the commit carries an Assisted-by: trailer per AI_POLICY.md. I reproduced the bug locally, verified the behaviour against the reference git implementation, and reviewed every line.

go-git#2317

A non-trailing `**` locked onto the first path component matching the
segment after it; when a later segment then failed, the whole match was
rejected without retrying the remaining candidates. `**/a/b` therefore
did not match a/x/a/b even though `git check-ignore` ignores it. Recurse
over every candidate expansion so later starting points are retried,
mirroring the backtracking git's wildmatch performs for `**`.

Assisted-by: Claude Fable 5 <[email protected]>
Signed-off-by: codeAnqiang-ma <[email protected]>
@KaizenShogun

Copy link
Copy Markdown

I measured this PR against an outside bench before saying anything about it. This isn't a code review — I read the diff, I did not audit it. It's the behaviour, against git.

Setup. One Go helper (ReadPatterns + Matcher.Match, and Worktree.Status() as a second entry point), built three times from the same source, with only the tree it points at changing:

column tree
main refs/heads/main, fetched today
+2318 refs/pull/2318/merge — main + this PR, as GitHub computes it (head 70665cd)
+2311 refs/pull/2311/merge, for contrast

I checked the drift rather than assuming it: against main, the +2318 tree differs in exactly two files, plumbing/format/gitignore/pattern.go and conformance_test.go. So the column measures the patch and nothing else.

The corpus is 33 real repositories' full .gitignore trees (root plus nested rule files, materialised on disk), each in a files and a directories variant — 4,463 matcher queries and 2,224 through Status(). The oracle is git check-ignore -v --non-matching --no-index (git 2.55.0), asked again in every tree; nothing is remembered from a previous run.

The result, compared as sets rather than counts:

column matcher Status()
main 2 / 4,463 1 / 2,224
+2318 2 / 4,463 — the same two 1 / 2,224 — the same one

Zero fixed, zero broken, zero moved. The two that stay are the .vscode + !.vscode/extensions.json family (go-git#694, go-git#877) and a Status()-only regression, neither of which this patch claims.

And it does fix what it claims. Both repros from #2317, verbatim, with git asked in the same tree:

tree query git main +2318
**/a/b a/x/a/b ignored not ignored ignored ✔
a/**/b/c a/b/x/b/c ignored not ignored ignored ✔
**/a/b (control) z/a/b/f.txt ignored ignored ignored

Why a real fix moves nothing in 4,463 queries — worth stating, because "no change" could equally mean the bench never exercised it. It did: 78 of the corpus's 3,427 rule lines carry a non-trailing ** (/vendor/**/*.py, x-pack/.../**/golden_tests/**/*.actual, and so on). None changes verdict, because the missing backtracking only bites when the segment after ** also appears earlier in the path — a/b/x/b/c, not a/b/c. Real .gitignore files hit that shape rarely; when they do, they get a silently wrong answer. So take this as evidence of safety, not of impact: the patch fixes its two cases and disturbs nothing else I can measure.

The bench, the corpus and the go-git adapter are public if you want to re-run any of it: https://github.com/KaizenShogun/gitignore-conformance

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: non-trailing ** does not backtrack — **/a/b fails to match a/x/a/b

2 participants