Skip to content

Implement Git-compatible wildmatch for gitignore patterns#1940

Open
AriehSchneier wants to merge 2 commits intogo-git:mainfrom
AriehSchneier:fix-gitignore
Open

Implement Git-compatible wildmatch for gitignore patterns#1940
AriehSchneier wants to merge 2 commits intogo-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 and others added 2 commits April 17, 2026 10:28
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.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com>
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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com>
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.

bug: gitignore behavior mismatch to the git in specific scenarios

1 participant