Skip to content

Implement Git-compatible wildmatch for gitignore patterns - #1940

Merged
pjbgf merged 4 commits into
go-git:mainfrom
AriehSchneier:fix-gitignore
Apr 30, 2026
Merged

pjbgf merged 4 commits into
go-git:mainfrom
AriehSchneier:fix-gitignore

Conversation

@AriehSchneier

Copy link
Copy Markdown
Contributor

This PR implements a Git-compatible wildmatch function to fix gitignore pattern matching compatibility issues.

Summary

Replaces filepath.Match with a custom wildmatch implementation that provides full Git compatibility for gitignore pattern matching.

Changes

Commit 1: Add comprehensive gitignore compatibility test suite

  • Complete test suite based on Git's official tests (t3070-wildmatch.sh and t0008-ignores.sh)
  • 84 tests covering all edge cases and compatibility scenarios
  • Tests for POSIX character classes, bracket expressions, double star patterns, etc.

Commit 2: Implement Git-compatible wildmatch for gitignore patterns

  • Custom wildmatch() function with star backtracking
  • Full POSIX character class support ([:digit:], [:upper:], [:alpha:], [:xdigit:], etc.)
  • Proper bracket expression negation with [!...] and [^...]
  • Escaped characters in patterns and bracket expressions
  • Complex bracket ranges and edge cases (e.g., [\]], [[-\]], [--A])
  • Malformed character class detection
  • Patterns with ** embedded in segments (e.g., foo**/bar, /bar)

Test Results

84/84 tests passing (100%)

  • TestGitCanonicalSuite ✅
  • TestWildmatchCanonicalSuite ✅
  • TestCompleteWildmatchSuite ✅
  • TestPatternSuite ✅
  • TestMatcherSuite ✅

Implementation

Based directly on Git's wildmatch.c implementation from the Git source code, ensuring full compatibility with Git's gitignore pattern matching behavior.

Fixes #877


🤖 Generated with Claude Code

@AriehSchneier
AriehSchneier force-pushed the fix-gitignore branch 3 times, most recently from a5a20a2 to fb9676c Compare March 31, 2026 03:50
@AriehSchneier
AriehSchneier requested a review from pjbgf March 31, 2026 08:48
@AriehSchneier
AriehSchneier force-pushed the fix-gitignore branch 3 times, most recently from 6f2b90c to d584230 Compare April 4, 2026 13:23
@AriehSchneier
AriehSchneier force-pushed the fix-gitignore branch 5 times, most recently from 6ac0f75 to 52e4307 Compare April 14, 2026 22:49
@AriehSchneier
AriehSchneier force-pushed the fix-gitignore branch 4 times, most recently from 7b54df0 to c9620cc Compare April 30, 2026 00:52
@pjbgf

pjbgf commented Apr 30, 2026

Copy link
Copy Markdown
Member

@AriehSchneier please note that commits assisted by AI must have a Assisted-By: trailer as opposed to Co-Authored-By: as per AI Policy. Please update that and rebase the changes.

I have some additional changes to add to this PR which I will at some point today.

This commit adds a complete test suite for evaluating go-git's gitignore
implementation against Git's canonical behavior, based on Git's official
test suite (t3070-wildmatch.sh and t0008-ignores.sh).

Test Files:
- complete_wildmatch_test.go: Complete wildmatch pattern tests
- git_canonical_test.go: Canonical test suite from Git source
- wildmatch_canonical_test.go: Wildmatch-specific canonical tests

The test suite identifies several compatibility issues with Git's behavior:
- Bracket expression negation ([!...] vs [^...])
- Double star pattern matching (data/**, foo**/bar)
- Advanced bracket expressions (a[]]b, a[]-]b)
- POSIX character classes ([[:alpha:]], [[:digit:]])
- Multi-level path counting (*/*/*)

This test suite provides a baseline for future compatibility improvements
without implementing the fixes themselves.

Assisted-by: Claude Sonnet 4.5 <[email protected]>
Signed-off-by: Arieh Schneier <[email protected]>
This commit replaces filepath.Match with a custom wildmatch implementation
that provides full Git compatibility for gitignore pattern matching.

Key improvements:
- POSIX character classes: [:digit:], [:upper:], [:alpha:], [:xdigit:], etc.
- Proper bracket expression negation with [!...] and [^...]
- Escaped characters in patterns and bracket expressions
- Complex bracket ranges and edge cases (e.g., [\\]], [[-\\]], [--A])
- Malformed character class detection (returns false for invalid classes)
- Patterns with ** embedded in segments (e.g., foo**/bar, **/bar**)

Implementation details:
- wildmatch(): Core pattern matching with star backtracking
- matchBracket(): Bracket expression matching following Git's logic
- matchCharClass(): POSIX character class validation with (matched, valid) tuple
- findBracketEnd(): Correctly handles malformed [:class:] patterns as literals

Based on Git's wildmatch.c implementation from the Git source code.

Test results: 84/84 tests passing (100%)
- TestGitCanonicalSuite: All canonical Git behavior tests passing
- TestWildmatchCanonicalSuite: All wildmatch-specific tests passing
- TestCompleteWildmatchSuite: Complete Git test matrix passing
- TestPatternSuite: All pattern matching tests passing
- TestMatcherSuite: All matcher integration tests passing

Assisted-by: Claude Sonnet 4.5 <[email protected]>
Signed-off-by: Arieh Schneier <[email protected]>
Fix two divergences from Git's wildmatch:
- Trailing '\' no longer matches itself; the escape branch falls
  through to backtracking per wildmatch.c's escape/default path.
- POSIX classes are ASCII-only, matching sane-ctype.h: high-bit bytes
  never classify.

Share the [:name:] header parser between findBracketEnd and matchBracket
as parsePosixClass.

Consolidate complete_wildmatch_test.go, git_canonical_test.go, and
wildmatch_canonical_test.go into conformance_test.go. Without -short,
each expectation is cross-validated against `git check-ignore` on a
materialized scratch repo (file ops confined via *os.Root). Add
FuzzMatch over ParsePattern+Match.

Assisted-by: Claude Opus 4.7 <[email protected]>
Signed-off-by: Paulo Gomes <[email protected]>
Go-git currently doesn't support core.ignorecase=true. To ensure the tests still pass correctly,
verify that check would pass if core.ignorecase=false. This should be removed once that support
is added to go-git.

Signed-off-by: Paulo Gomes <[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.

@AriehSchneier thanks for working on this, a much needed addition to go-git. 🙇 🙇 🙇

I made a few additional changes while I was checking for upstream conformance:

  • Consolidated the different upstream-test cases on a single file.
  • Trailing \ fix in wildmatch a pattern ending in \ falls through to backtracking and fails to match, matching wildmatch.c's escape.
  • POSIX classes are now ASCII-only. matchCharClass replaces unicode.Is* with byte-range checks, mirroring sane-ctype.h so high-bit bytes never satisfy [[:alpha:]]/[[:digit:]]/etc.
  • Shared parsePosixClass helper. The [:name:] header parser used to be duplicated in findBracketEnd and matchBracket, now this is a helper function.
  • When !testing.Short() several test cases will go on and also verify if the matching expectations aligns with upstream Git.
  • Added checks for Git v2.11.0 (due to compatibility checks) to ensure we accommodate for cases whereby upstream behaviour changed over time.
  • Added checks to enforce that MacOS and Windows behaviour aligns with Git with core.ignorecase=false, at least until we add support for core.ignorecase=true.
  • Added FuzzMatch to fuzz test this logic going forwards.

@pjbgf
pjbgf merged commit c6873e1 into go-git:main Apr 30, 2026
17 checks passed
@AriehSchneier
AriehSchneier deleted the fix-gitignore branch April 30, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: gitignore behavior mismatch to the git in specific scenarios

2 participants