Skip to content

gitignore: a trailing ** matches unrelated directories, hiding untracked files from Worktree.Status #2278

Closed

Description

@glitch-ux

Bug Description

A gitignore pattern whose last segment is ** can match paths that the earlier
segments of the same pattern already ruled out.

The most damaging shape is the common **/<dir>/** form. Given a .gitignore
containing only:

**/node_modules/**

go-git reports every directory in the repository as ignored — including
directories that contain no node_modules component anywhere in their path, and
even when the repository has no node_modules directory at all.

Because Worktree.Status stops descending into a directory the ignore matcher
excludes, this silently hides every untracked file below the repository root.
Status() reports a near-empty result and callers that rely on it to detect
uncommitted work see a clean tree.

A second, narrower case comes from the same code path: a trailing ** reached
when the candidate path has already been fully consumed keeps the preceding
segment's result instead of computing its own, so a/**/*/** matches a/f.txt.

Reference git disagrees in both cases. Per gitignore(5), a trailing /**
"matches everything inside" the directory it follows — it does not license a
match for paths that never contained the preceding segment.

go-git Version

main @ ffa616a (v6). The affected code
(globMatch in plumbing/format/gitignore/pattern.go) is also present in v5.

Steps to Reproduce

Matcher-level minimal working example:

package main

import (
	"fmt"
	"strings"

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

func main() {
	m := gitignore.NewMatcher([]gitignore.Pattern{
		gitignore.ParsePattern("**/node_modules/**", nil),
	})

	for _, tc := range []struct {
		path  string
		isDir bool
	}{
		{"src", true},
		{"src/pkg", true},
		{"src/pkg/app.go", false},
		{"node_modules", true},
		{"node_modules/react/index.js", false},
	} {
		got := m.Match(strings.Split(tc.path, "/"), tc.isDir)
		fmt.Printf("%-28s isDir=%-5v ignored=%v\n", tc.path, tc.isDir, got)
	}
}

Actual output:

src                          isDir=true  ignored=true    <-- wrong
src/pkg                      isDir=true  ignored=true    <-- wrong
src/pkg/app.go               isDir=false ignored=false
node_modules                 isDir=true  ignored=true
node_modules/react/index.js  isDir=false ignored=true

Expected output, matching git check-ignore (git 2.47.3) over the same
.gitignore:

src/                         ignored=false
src/pkg/                     ignored=false
src/pkg/app.go               ignored=false
node_modules/                ignored=true
node_modules/react/index.js  ignored=true

End-to-end effect on Worktree.Status

mkdir repro && cd repro && git init -q
printf '**/node_modules/**\n' > .gitignore
mkdir -p src/pkg && echo 'package pkg' > src/pkg/app.go && echo hi > README.md
git add .gitignore && git commit -qm init

Reference git status --porcelain:

?? README.md
?? src/

Worktree.Status() on the same repository:

?? README.md

src/pkg/app.go is missing entirely — src was reported as an ignored
directory, so the walk never descended into it.

Additional Information

Root cause is in globMatch
(plumbing/format/gitignore/pattern.go), which treats its matched variable as
a flag that only ever moves to true:

  • A **/<segment> traversal that runs out of path sets matched = false and
    falls through. The next loop iteration is the trailing **, and its branch is
    evaluated before the if len(path) == 0 { return false } guard, so it sets
    matched back to true for any directory candidate.
  • The trailing ** branch only ever raises matched, so when its own condition
    is not satisfied it silently inherits whatever the previous segment left
    behind.

A randomised differential run against git check-ignore (296 patterns ×
66 paths) shows five pattern families over-matching on current main:
**/*.txt/**, **/a/**, **/baz/**/**, ?/**/?/**/ and a/**/*/**.

I have a fix with regression tests cross-checked against the git binary via
the existing ConformanceSuite oracle, and will open a PR referencing this
issue.

Activity

  1. added a commit that references this issue on Jul 27, 2026
    3189ee9
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