Skip to content

bug: gitignore behavior mismatch to the git in specific scenarios #877

Closed

Description

@ImSingee

If the root .gitignore has abc, and the abc/.ignore has !def

Then, the ignore status for abc/def between git and go-git mismatch:

  • In go-git: the abc/def is not ignored
  • In git: the abc/def is ignored

Test Folder: ignore-check.zip

Test Code:

import (
	"testing"
	
	"github.com/go-git/go-billy/v5/osfs"
	"github.com/go-git/go-git/v5/plumbing/format/gitignore"
	"github.com/stretchr/testify/assert"
)

func TestGitIgnore(t *testing.T) {
	fs := osfs.New("/tmp/ignore-check")

	ps, err := gitignore.ReadPatterns(fs, nil)
	assert.NoError(t, err)

	assert.Equal(t, 2, len(ps))

	matcher := gitignore.NewMatcher(ps)

	assert.True(t, matcher.Match([]string{"abc"}, true), "abc should be ignored")
	assert.True(t, matcher.Match([]string{"abc/def"}, true), "abc/def should be ignored") # this test failed
	assert.False(t, matcher.Match([]string{"abc/ghi"}, true), "abc/ghi should not be ignored")
	assert.False(t, matcher.Match([]string{"def"}, true), "def (root) should not be ignored")
}

Activity

  1. changed the title [-]bug: gitignore may be mismatched to the git behavior in specific scenarios[/-] [+]bug: gitignore behavior mismatch to the git in specific scenarios[/+] on Oct 13, 2023
  2. added
    bug
    help wanted
    on Oct 28, 2023
  3. j4k4 commented on Dec 27, 2023

    @j4k4

    I'm having the same issue, which is quite annoying to deal with.
    I've created a sample repository which helps to reproduce the issue, too: https://github.com/j4k4/go-git-status-bug
    Looks like it has to do something with the domain, as dropping the following line makes it behave correctly again:

    diff --git a/plumbing/format/gitignore/pattern.go b/plumbing/format/gitignore/pattern.go
    index 450b3cd..098cb50 100644
    --- a/plumbing/format/gitignore/pattern.go
    +++ b/plumbing/format/gitignore/pattern.go
    @@ -39,8 +39,6 @@ type pattern struct {
     
     // ParsePattern parses a gitignore pattern string into the Pattern structure.
     func ParsePattern(p string, domain []string) Pattern {
    -       // storing domain, copy it to ensure it isn't changed externally
    -       domain = append([]string(nil), domain...)
            res := pattern{domain: domain}
     
            if strings.HasPrefix(p, inclusionPrefix) {
    
  4. github-actions commented on Dec 20, 2024

    @github-actions

    To help us keep things tidy and focus on the active tasks, we've introduced a stale bot to spot issues/PRs that haven't had any activity in a while.

    This particular issue hasn't had any updates or activity in the past 90 days, so it's been labeled as 'stale'. If it remains inactive for the next 30 days, it'll be automatically closed.

    We understand everyone's busy, but if this issue is still important to you, please feel free to add a comment or make an update to keep it active.

    Thanks for your understanding and cooperation!

  5. added
    stale
    on Dec 20, 2024
  6. added
    no-autoclose
    and removed
    stale
    on Jan 22, 2025
  7. reopened this on Jan 22, 2025
  8. added
    good first issue
    on Mar 24, 2026
  9. pjbgf commented on Mar 24, 2026

    @pjbgf
    Member
  10. added a commit that references this issue on Mar 25, 2026
    d9dfe72
  11. AriehSchneier commented on Mar 26, 2026

    @AriehSchneier
    Contributor

    A test suite and details of where we are currently lacking:
    AriehSchneier@6aa9efc

  12. AriehSchneier commented on Mar 30, 2026

    @AriehSchneier
    Contributor

    @pjbgf Do we want to update our implementation, or do we want to use https://github.com/git-pkgs/gitignore?

    If we want to use https://github.com/git-pkgs/gitignore I have a branch that appears to work and match gits implementation (required us to bump go.mod to 1.25 which it looks like is about to happen anyway, I can rebase my changes once that happens):
    AriehSchneier@3edf6ec

  13. KaizenShogun commented on Sep 9, 2026

    @KaizenShogun

    @AriehSchneier your March question — fix go-git's matcher, or delegate to git-pkgs/gitignore — is
    measurable, so here are numbers rather than an opinion.

    Setup. One Go helper (gitignore.ReadPatterns + Matcher.Match, and Worktree.Status() as a
    second entry point), built four times from the same source, with only the go-git tree changing.
    Corpus: the rule files of 33 large repos as written, 4,463 nested-.gitignore queries, with
    git check-ignore --no-index (git 2.55.0) as the oracle. Bench:
    https://github.com/KaizenShogun/gitignore-conformance.

    The control is your own 6aa9efc, the parent of the delegating commit, so the comparison isolates
    the engine swap instead of five months of drift.

    build Matcher.Match (4,463) Status() (2,224)
    main (tarball taken 2026-09-09) 2 1
    your 6aa9efc — own matcher 4 1
    your 3edf6ec — delegating, git-pkgs/gitignore v1.1.1 5 4
    the same, with both library bugs patched 2 1

    Compared as sets, not counts. Delegating as it stands trades two failures for three: it fixes
    grafana's testdata/**output/, and it breaks deps/ (a file named after the directory that
    excluded it) in three places, which is also the whole of the Status() regression. Those three are
    git-pkgs/gitignore#22. There is a second one I filed today, git-pkgs/gitignore#23: a dir-only
    pattern with a wildcard, *.egg-info/, matches the directory but nothing inside it — that one the
    bench never asked about, and it hits 9 real rule lines across 8 of those repos.

    With both patched, delegating lands exactly where main already is: 2 and 1.

    The part that matters for this issue. The repro in the opening comment — root abc, nested
    abc/.gitignore with !def — passes on all four builds. It doesn't reproduce any more, whichever
    engine you pick. What does still fail on all four is the sibling shape from #694, both patterns in
    one file:

    test/
    !test/keep
    

    git keeps test/keep ignored (once a directory is excluded it never looks inside again), and all
    four builds re-include it. It is also the last thing standing in both columns above — the two
    remaining divergences are .vscode + !.vscode/extensions.json from ollama, the same shape. So
    the engine choice is orthogonal to it: neither library gets it right today.

  14. KaizenShogun commented on Sep 11, 2026

    @KaizenShogun

    A correction to my own comment above, with the numbers that force it.

    I measured those four builds on a corpus that never asked what was inside a directory. Adding that
    one variant took it from 4,463 questions to 8,953, and it moves every row here — including the
    conclusion I stated, that delegating "lands exactly where main already is".

    Same setup as before: git 2.55.0 as the oracle, one Go helper built once per tree, nothing else
    changing between rows.

    build Matcher.Match Status() old, at 4,463
    main (2026-09-09) 12 5 2 / 1
    6aa9efc — own matcher 18 15 4 / 1
    3edf6ec — delegating, v1.1.1 + git-pkgs/gitignore#22 24 23 2 / 1
    the same, library at HEAD 12 11 —

    (Status() answers 6,714 of the 8,953; it declines directories, so I don't make a row up for them.)

    Row three is the one I reported in March terms, and on the bigger corpus it reads 24 against main's
    12 — the tie was an artefact of the questions, not a property of the engines. But that row's library
    is v1.1.1 plus one patch, and it has gone stale: git-pkgs/gitignore#22 and #24 are both merged now,
    and the library measured on its own sits at 12 over the same questions. That's the last row, and
    it's the only one that answers the question as it stands in 2026.

    Delegating to HEAD scores 12 under Matcher.Match — and compared as sets, not counts, they are the
    same twelve main already fails. Nothing fixed, nothing broken. A real tie this time.

    Status() is where delegating still costs something: 11 against 5, one case bought and seven lost.
    All seven are contents of a directory whose name reads like a file — .vscode/extensions.json/… in
    ollama, docker/volumes/functions/main/index.ts/… in supabase. Which of the two open library issues
    they trace to (git-pkgs/gitignore#25 or #26) I haven't pinned down; I'm reporting the number, not the
    cause.

    So at the matcher the engine choice is a wash today, and whatever decides it isn't conformance. The
    Status() gap is the part worth watching, and it lives upstream rather than here.

    Bench, corpus and instructions to reproduce any row: https://github.com/KaizenShogun/gitignore-conformance

  15. KaizenShogun commented on Sep 11, 2026

    @KaizenShogun

    Closing the loose end from my comment this morning: I said delegating costs 7 cases in Status() and that I had not attributed them. Now I have, and the attribution changes what the 7 mean.

    Same delegating build, only the library tree swapped — twice, to two patched copies of git-pkgs/gitignore HEAD:

    library under the delegator Status() divergences of the 7
    HEAD as shipped 11 —
    HEAD, no implicit ** on negations 67 fixes 6, leaves 1
    HEAD, no implicit ** at all (the ceiling) 1818 fixes 4, leaves 3

    So six of the seven are git-pkgs/gitignore#26 — a negation inherits the implicit ** that appendTrailingDoubleStar appends, so !volumes/functions/main/index.ts re-includes everything under that path too. The seventh is a different bug, #25: app/ui/app/.vscode/extensions.json, where .vscode is excluded and git refuses to let !.vscode/extensions.json bring it back. Both are open with repros.

    Two things worth flagging, since neither is what I expected:

    The ceiling row is not a superset of the patch row. Dropping the implicit ** everywhere fixes four of the seven but leaves the two ollama ones that the negation-only patch fixes — because once exclusions lose their **, .vscode stops covering its own contents and those paths diverge again for a new reason. The mechanism is load-bearing for exclusions; it is only wrong on negations.

    And the negation-only patch costs 66 new breakages in Status() on this corpus (67 total against 11). It is the right diagnosis and the wrong patch — the library uses that same implicit ** to emulate the walk down ancestors it does not have, so removing it from negations takes the walk away with it. I said as much in #26; this is the same result seen through go-git.

    None of this moves the matcher numbers from this morning: delegating and main still return the same 12. The 7 are Status()-only, and they have two ticket numbers rather than an unknown cause.

  16. KaizenShogun commented on Sep 13, 2026

    @KaizenShogun

    A correction to my own comment of 11 Sep in this thread. I said delegating to git-pkgs/gitignore cost 7 cases in Status(), and traced most of them to two bugs I'd reported in that library. Those bugs are fixed and tagged (v1.3.0, today). The cost is no longer 7, and not in the direction I'd have guessed: rebuilt on the fixed library, the delegating branch gets worse.

    Same corpus and oracle as before — 8,953 nested-.gitignore queries over 43 repos, git check-ignore --no-index, git 2.55.0 — one Go helper built three times, only the trees under it changing. The branch pins v1.1.1; in both delegating rows I swap the library tree and change nothing else. main and the older delegating build were re-measured today as controls, and both reproduce their old numbers with the same rows:

    build Matcher.Match Status()
    go-git main, own matcher 12 / 8,953 5 / 6,714
    3edf6ec delegating, library at the commit before the fix (#22 and #24 in) 12 11
    the same 3edf6ec, library swapped for v1.3.0 84 81
    git-pkgs/gitignore v1.3.0 on its own, used as a whole-tree matcher 0 —

    The 82 new ones in the first column all point the same way — git says not ignored, go-git says ignored — across 10 repos and 14 pattern shapes. That's the direction that hides files from Status().

    It isn't the library. Two rules, with settings.json an actual directory:

    .gitignore:  .vscode/*
                 !.vscode/settings.json
    

    git check-ignore --no-index says .vscode/settings.json/keep.txt is not ignored, and .vscode/other.json is. ReadPatterns + NewMatcher(ps).Match([]string{".vscode", "settings.json", "keep.txt"}, false) returns false on 3edf6ec as branched and true on 3edf6ec + v1.3.0. The library asked directly — NewFromDirectory + MatchPath — agrees with git.

    The mechanism is in the bridge, pattern.go: pattern.Match builds a fresh extgitignore matcher per pattern per query and feeds it a single line. Since v1.3.0 that matcher walks the parent directories of the path it is handed, so .vscode/* on its own now answers "ignored" for anything under .vscode/settings.json; the negation, alone in its own matcher, no longer matches that descendant at all, because the implicit trailing ** that used to make it cascade was removed in the same release. go-git's Matcher is then choosing between one Exclude and nothing. Before v1.3.0 the implicit ** made the negation cascade too, and the two errors cancelled.

    So what's worth delegating is the library's whole-tree Matcher, which owns the parent walk and last-match-wins across every rule file; a per-pattern bridge delegates the pattern and keeps the matching, and that is where the 84 come from. main looks to be going its own way — Scope in 9a978dd is the same fix made in-house — so this may well be moot. I'm posting it because the numbers I left in this thread say otherwise, and anyone rebuilding that branch today lands on the wrong side of them.

    Bench and corpus: https://github.com/KaizenShogun/gitignore-conformance

  17. KaizenShogun commented on Sep 13, 2026

    @KaizenShogun

    One number in my comment this morning is wrong, and it's the one describing the bench rather than any result: the 8,953 queries come from 33 repositories, not 43. 43 is the level-1 corpus in the same repo — one root .gitignore each, 9,852 queries — and I pasted its census onto a level-2 number. My earlier comments in this thread say 33, which is the right one.

    Nothing else changes: main 12, the branch as pinned 12, the branch on v1.3.0 84, and the two-rule repro at the end stands on its own without the corpus.

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

    buggood first issuehelp wantedno-autoclose

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions