Skip to content

go-git ignores files inside un-ignored directory with wildcard pattern #2112

Open

Description

@aktau

Bug Description

When using a .gitignore file with a wildcard ignore * followed by a directory-only inclusion like !dir/, go-git fails to ignore files inside dir/, whereas standard Git ignores them.

In plumbing/format/gitignore/pattern.go, the simpleNameMatch function iterates over all components of a path. If any component matches the pattern, it returns true (matched), unless it's the last component and it's checking dirOnly.

For a path ["dir", "file.txt"] and pattern !dir/:

  1. Component dir matches dir.
  2. It is not the last component (i == 0, len(path)-1 == 1).
  3. The function returns true (matched).
  4. This causes the file to be included, overriding the * pattern.

Effectively, !dir/ acts as !dir/** in go-git.

(I did use AI to generate a bug report with identifiers that avoid the non-public parts of my project, I edited it myself and ran the reproducer myself.)

go-git Version

488798e

Steps to Reproduce

To reproduce:

Standard Git:

mkdir git-repro
cd git-repro
git init
echo '*' > .gitignore
echo '!my-dir/' >> .gitignore
mkdir -p my-dir/sub
echo 'test' > my-dir/sub/file.txt
git status

This would say: nothing to commit (the file inside my-dir is ignored).

git based on go-git:

Run these commands to set up the environment and run the reproducer:

mkdir gogit-repro
cd gogit-repro
# Save the Go code below as repro.go
go mod init repro
go get github.com/go-git/go-git/v5
go run repro.go
// repro.go
package main

import (
	"fmt"
	"os"

	"github.com/go-git/go-git/v5"
)

func main() {
	// Setup environment
	os.MkdirAll("my-dir/sub", 0755)
	os.WriteFile(".gitignore", []byte("*\n!my-dir/\n"), 0644)
	os.WriteFile("my-dir/sub/file.txt", []byte("test"), 0644)

	r, err := git.PlainInit(".", false)
	if err != nil {
		panic(err)
	}
	w, err := r.Worktree()
	if err != nil {
		panic(err)
	}

	status, err := w.Status()
	if err != nil {
		panic(err)
	}

	fmt.Println("Status:")
	for path, s := range status {
		fmt.Printf("  %s: %c\n", path, s.Worktree)
	}
}

Actual Output:

Status:
  my-dir/sub/file.txt: ?

(? indicates Untracked, meaning it was not ignored).

Additional Information

No response

Activity

  1. added
    bug
    on May 12, 2026
  2. thesahibnanda-max commented on Aug 11, 2026

    @thesahibnanda-max

    Opened #2310 to fix this — simpleNameMatch was letting a dirOnly re-include pattern (!dir/) matching an ancestor path component stand in as a match for the full descendant path, so files nested under the re-included directory were wrongly reported as untracked instead of ignored. The PR description has "Fixes #2112" so this will auto-close once it's merged.

  3. added a commit that references this issue on Aug 11, 2026
    9d8d8d3
  4. thesahibnanda-max commented on Aug 11, 2026

    @thesahibnanda-max

    Update: the fix now lives in #2311 (replaces #2310, which had a commit-message/DCO CI failure — same change, just re-committed correctly). #2311 has the full write-up of the root cause and fix; short version: simpleNameMatch was letting a dirOnly re-include pattern (!dir/) that matched an ancestor path component stand in as a match for the entire descendant path, so files nested under the re-included directory were wrongly reported as untracked instead of ignored. "Fixes #2112" is in the PR description, so it'll auto-close this issue once merged.

  5. KaizenShogun commented on Sep 18, 2026

    @KaizenShogun

    I keep an outside conformance bench against git check-ignore, and this bug is the whole of what it finds in go-git's matcher: on today's main (0f3a0a2c), all 12 divergences over 8,953 queries are this one rule. Two open PRs offer to fix it — #2195 and #2311 — so I measured both against their own merge bases. Behaviour, not a code review; oracle is git 2.55.0.

    A second code path, which neither PR's write-up mentions. The analysis here is about simpleNameMatch and a dirOnly re-include. globMatch cascades the same way, with an anchored multi-segment negation and no dirOnly anywhere:

    $ git init -q r && cd r
    $ printf 'a/**/*\n!a/b\n' > .gitignore
    $ mkdir -p a/b && touch a/b/c
    $ git check-ignore -q a/b/c && echo ignored
    ignored                       # .gitignore:1:a/**/*

    Matcher.Match([]string{"a", "b", "c"}, false) returns false on main, and Status() lists a/b/c as untracked. !a/b re-includes the directory; git then re-tests what is inside it, and a/**/* still ignores that.

    The pattern pairs are real: supabase/supabase ships volumes/functions/** + !volumes/functions/deno.json* and **/*/generated/**/* + !**/*/generated/.gitkeep, ollama/ollama ships .vscode/* + !.vscode/extensions.json. It bites when the re-included name is a directory on disk (when it is a file, go-git agrees) — which is also why the four rows left in #2386's Status() column after it fixes #2369 are this shape.

    The two PRs. One Go helper (ReadPatterns + Matcher.Match, and Worktree.Status() as a second entry point), built once per column through a replace to a worktree of that commit, so only the library changes between rows. Corpus: 33 repositories' whole .gitignore trees materialised on disk, 8,953 matcher queries and 6,714 answered through Status(). Sets, not counts.

    column matcher Status()
    main 0f3a0a2c 12 5
    #2311 base 3cb46ce7 12 5
    #2311 refs/pull/2311/merge 28 — 0 fixed, 16 new 23 — 0 fixed, 18 new
    #2195 base d6cbbfaa 12 5
    #2195 refs/pull/2195/merge 86 — 8 fixed, 82 new 83 — 2 fixed, 80 new

    The three bases are 147 commits apart and give identical rows, same paths; that is what makes the deltas readable. (#2195's merge ref is computed against a base that predates Scope, so its column describes the patch on that base.)

    What they buy. #2195 fixes the globMatch half — the repro above and the supabase rows both come right. #2311 fixes this issue's case verbatim — * + !my-dir/, my-dir/sub/file.txt ignored, checked by hand against both columns. No row of my corpus moves in its favour, which says the corpus doesn't sample that combination, not that the patch does nothing.

    What they cost, and it is the same family both times — a directory excluded by one rule and re-included by a later one, where nothing else in the file matches the contents:

    $ printf 'core\n!core/\n' > .gitignore              # python/cpython ships this, with a comment saying why
    # a/core/keep    git: not ignored    main: not ignored    +2311: ignored    +2195: ignored
    
    $ printf '.vscode\n!packages/playground/.vscode\n' > .gitignore    # facebook/react, compiler/
    # packages/playground/.vscode/settings.json
    #                git: not ignored    main: not ignored    +2311: ignored through Status()    +2195: ignored

    All 82 new rows under #2195 go that one way — git does not ignore, go-git does — across ten repositories: !.env.example (langchain, gradio), !.* and !deps/**/.* (node), !.vscode/settings.json (angular), !core/ (cpython), !.yarn/patches (next.js, tailwind), plus scikit-learn, vscode and react. #2311's 16 and 18 are the same shape, confined to cpython, react and vscode.

    Both patches encode "a negation counts only if it matches the whole path". git's rule reads differently from here: the negation re-includes the directory, and everything inside is then tested again against every pattern. In this issue's case the contents stay ignored because * matches sub and file.txt on their own, not because !my-dir/ stopped counting; with core + !core/ nothing is left to ignore a/core/keep, which is why main is right there and both patches are not.

    Bench, adapter and corpus: https://github.com/KaizenShogun/gitignore-conformance — every row above is a gic.py run with --entry patterns or --entry status.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions