From 2c3d905980be3db98700fdad97b3f9193ddf31b7 Mon Sep 17 00:00:00 2001 From: Midas <280795179+KaizenShogun@users.noreply.github.com> Date: Thu, 10 Sep 2026 06:23:11 +0200 Subject: [PATCH] Don't fast-reject a dir-only pattern by the queried path's last segment lastSeg is the last segment of the queried path, which only decides the match when the pattern is compared against the whole path. A dir-only pattern can match an ancestor, so *.egg-info/ was dropped before matchPattern could match mypkg.egg-info/ for mypkg.egg-info/PKG-INFO. Fixes #23. --- gitignore.go | 4 ++-- gitignore_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gitignore.go b/gitignore.go index a86dd60..6bb0864 100644 --- a/gitignore.go +++ b/gitignore.go @@ -356,7 +356,7 @@ func (m *Matcher) match(relPath string, isDir bool) bool { for i := len(m.patterns) - 1; i >= 0; i-- { p := &m.patterns[i] - if p.literalSuffix != "" && !strings.HasSuffix(lastSeg, p.literalSuffix) { + if p.literalSuffix != "" && !p.dirOnly && !strings.HasSuffix(lastSeg, p.literalSuffix) { continue } if !matchPattern(p, pathSegs, isDir) { @@ -373,7 +373,7 @@ func (m *Matcher) matchDetail(relPath string, isDir bool) MatchResult { for i := len(m.patterns) - 1; i >= 0; i-- { p := &m.patterns[i] - if p.literalSuffix != "" && !strings.HasSuffix(lastSeg, p.literalSuffix) { + if p.literalSuffix != "" && !p.dirOnly && !strings.HasSuffix(lastSeg, p.literalSuffix) { continue } if !matchPattern(p, pathSegs, isDir) { diff --git a/gitignore_test.go b/gitignore_test.go index 04faeb9..5b6cdca 100644 --- a/gitignore_test.go +++ b/gitignore_test.go @@ -892,6 +892,16 @@ func TestMatchVsGitCheckIgnore(t *testing.T) { {"other/deps", false}, }, }, + { + name: "dir-only pattern with a wildcard, contents of the matched directory", + patterns: "*.egg-info/\n", + paths: []checkPath{ + {"mypkg.egg-info", true}, + {"mypkg.egg-info/PKG-INFO", false}, + {"mypkg.egg-info/sub/x.txt", false}, + {"notes.egg-info-backup", false}, + }, + }, { name: "mixed patterns", patterns: "*.log\n!important.log\nbuild/\n/dist\nfoo/**/bar\n",