Skip to content

plumbing: format/gitignore, fix trailing ** matching unrelated paths - #2279

Merged
pjbgf merged 1 commit into
go-git:mainfrom
glitch-ux:fix/gitignore-trailing-doublestar-overmatch
Jul 26, 2026
Merged

pjbgf merged 1 commit into
go-git:mainfrom
glitch-ux:fix/gitignore-trailing-doublestar-overmatch

Conversation

@glitch-ux

@glitch-ux glitch-ux commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2278

Why

A .gitignore containing **/node_modules/** makes go-git report every
directory in the repository as ignored — including directories with no
node_modules component in their path, and even when no node_modules exists.

That hides files rather than just mislabelling them: Worktree.Status stops
descending into a directory the matcher excludes, so untracked work below the
repository root never shows up. Status() looks clean when it isn't.

The cause is in globMatch, which treats matched as a flag that only ever
moves to true. When the earlier segments of a pattern rule a path out, the
trailing ** runs afterwards and flips it back on. A second symptom shares the
cause: a trailing ** reached with the path already consumed inherits the
previous segment's answer, so a/**/*/** matches a/f.txt.

gitignore(5) defines the trailing /**
form as matching everything inside the directory it follows — not paths that
never contained the preceding segment. Confirmed against git check-ignore
(git 2.47.3).

What changed

Eight lines in plumbing/format/gitignore/pattern.go: a ** that never finds
the segment following it now returns a definitive non-match, and the trailing
** assigns its own result instead of only raising the flag.

Verification

  • Regression tests added to ConformanceSuite, so every expectation is
    cross-checked against the real git binary. All fail on main, pass here.
  • Randomised differential run against git check-ignore (296 patterns × 66
    paths): five over-matching pattern families on main, zero after the fix,
    no new divergence.
  • golangci-lint (v2.11.4, make validate-lint): 0 issues. go test ./...
    shows the same failures before and after.

…ixes go-git#2278

globMatch treated its matched flag as write-once-true, so a trailing `**`
could report a match that the preceding segments had already ruled out.

A `**/<segment>` traversal that exhausted the path left matched false and
fell through to the trailing `**`, which set it back to true for any
directory candidate. `**/node_modules/**` therefore excluded every
directory in the repository, and because Worktree.Status stops descending
into an excluded directory, every untracked file below the root vanished
from the reported status. Separately, a trailing `**` reached with the
path already consumed kept the preceding segment's result, so `a/**/*/**`
matched `a/f.txt`.

A `**` that never finds the segment following it now returns a definitive
non-match, and the trailing `**` assigns its own result rather than only
raising it. gitignore(5) documents the trailing `/**` form as matching
everything inside the directory it follows; the new conformance cases are
cross-checked against `git check-ignore`.

Assisted-by: Claude Opus 5 <[email protected]>
Signed-off-by: glitch-ux <[email protected]>

@pjbgf pjbgf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glitch-ux thanks for working on this. 🙇

@pjbgf
pjbgf merged commit 8bff611 into go-git:main Jul 26, 2026
23 checks passed
@glitch-ux

Copy link
Copy Markdown
Contributor Author

@pjbgf My pleasure! :)

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: a trailing ** matches unrelated directories, hiding untracked files from Worktree.Status

2 participants