Skip to content

gitignore: non-trailing ** does not backtrack — **/a/b fails to match a/x/a/b #2317

Open

Description

@codeAnqiang-ma

Summary

In plumbing/format/gitignore, a non-trailing ** locks onto the first path component that matches the segment following it. When a later segment then fails, the whole match is reported as a non-match instead of retrying the ** expansion from the next candidate component. Real git backtracks here (wildmatch expands ** recursively), so go-git disagrees with git check-ignore on common patterns like **/a/b.

Expected behaviour

gitignore(5): "A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, a/**/b matches a/b, a/x/b, a/x/y/b and so on." — ** is an existential "zero or more", so every possible expansion must be tried.

Verified against real git (2.48.1, same result on 2.39.5; these patterns keep ** in the positions git documents, so they are unaffected by the foo**/bar wildmatch corner case git fixed in 2.52.0):

$ 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
$ echo 'a/**/b/c' > .gitignore
$ git check-ignore -v a/b/x/b/c
.gitignore:1:a/**/b/c	a/b/x/b/c    # ignored

Actual behaviour

package main

import (
	"fmt"
	"strings"

	"github.com/go-git/go-git/v6/plumbing/format/gitignore"
)

func main() {
	for _, c := range []struct{ pattern, path string }{
		{"**/a/b", "a/x/a/b"},
		{"**/foo/bar", "foo/baz/foo/bar"},
		{"a/**/b/c", "a/b/x/b/c"},
		{"**/node_modules/lodash", "web/node_modules/x/node_modules/lodash"},
	} {
		p := gitignore.ParsePattern(c.pattern, nil)
		r := p.Match(strings.Split(c.path, "/"), false)
		fmt.Printf("%-24q vs %-42q => %v\n", c.pattern, c.path, r == gitignore.Exclude)
	}
}
"**/a/b"                 vs "a/x/a/b"                                  => false   // git: ignored
"**/foo/bar"             vs "foo/baz/foo/bar"                          => false   // git: ignored
"a/**/b/c"               vs "a/b/x/b/c"                                => false   // git: ignored
"**/node_modules/lodash" vs "web/node_modules/x/node_modules/lodash"   => false   // git: ignored

Because Worktree.Status consumes this matcher, files git ignores show up as untracked (the inverse of #2278).

Root cause

pattern.go, globMatch: when canTraverse is set (a non-trailing ** was just consumed), the loop scans forward and breaks on the first component matching the next segment. If a later segment fails afterwards, the function returns false without ever retrying the remaining candidates:

if canTraverse {
canTraverse = false
for len(path) > 0 {
e := path[0]
path = path[1:]
if wildmatch(pattern, e) {
matched = true
break
}
if len(path) == 0 {
// A `**` that never finds the segment following it is a
// definitive non-match. Returning here rather than
// clearing matched keeps a trailing `**` from reviving
// the pattern once the path is exhausted, which would
// make `**/bar/**` match directories containing no bar.
return false
}
}

With **/a/b against a/x/a/b: the leading a is locked, b vs x fails, and the correct expansion (** → a/x) is never attempted.

The existing conformance cases all use paths where the segment after ** occurs exactly once, which is why this never trips the suite.

Environment

go-git main @ 34770e0, Go 1.26.5, darwin/arm64. Oracle: git 2.48.1 and 2.39.5 (Apple).

I have a minimal fix with regression tests ready and will open a PR referencing this issue.

Disclosure: this report was prepared with AI assistance (Claude, via Cursor). I reproduced the behaviour locally and reviewed every claim.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions