Skip to content

fix: silently discard unclosed bracket expressions#104

Open
ArmaanjeetSandhu wants to merge 1 commit intocpburnz:masterfrom
ArmaanjeetSandhu:fix/unclosed-bracket-patterns
Open

fix: silently discard unclosed bracket expressions#104
ArmaanjeetSandhu wants to merge 1 commit intocpburnz:masterfrom
ArmaanjeetSandhu:fix/unclosed-bracket-patterns

Conversation

@ArmaanjeetSandhu
Copy link

Summary

Fixes #93 by making pathspec match Git's behavior for invalid gitignore patterns with unclosed bracket expressions.

Problem

Git silently discards patterns like [ and [!] (unclosed brackets), treating them as null-operations. Files with those literal names are tracked.

pathspec previously treated these as literal characters, creating regex patterns that would incorrectly ignore files named [ or [!].

Solution

Added validation to detect unclosed bracket expressions and return (None, None) (null-operation) instead of creating invalid regex patterns.

Changes:

  • Added __has_unclosed_bracket() helper method to both GitIgnoreBasicPattern and GitIgnoreSpecPattern
  • Modified pattern_to_regex() to check for unclosed brackets before processing
  • Updated 4 tests (2 in each test file) that were documenting buggy behavior to now test correct behavior

Verification

Tested against git check-ignore to confirm identical behavior:

$ echo '[' > .gitignore
$ touch '['
$ git check-ignore '['
# (no output = file is tracked) ✓

With this fix, pathspec now produces the same result.

Test results:

  • All tests pass after updating the 4 tests that documented the bug
  • Pattern [ → files named [ are tracked ✓
  • Pattern [!] → files named [!] are tracked ✓
  • Valid patterns like [abc] still work correctly ✓

Related

This completes the fixes mentioned in issue #93. The other issues mentioned (foo**/bar and leading whitespace) were already fixed in v1.0.0.

Git silently discards gitignore patterns with unclosed bracket
expressions (e.g., '[' or '[!]'), treating them as null-operations.
Files with those literal names are tracked, not ignored.

pathspec previously treated unclosed brackets as literal characters,
creating regex patterns that would incorrectly match and ignore files
with those names.

This commit adds validation to detect unclosed bracket expressions
and return (None, None) to indicate a null-operation pattern, matching
Git's behavior exactly.
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.

Different gitignore pattern interpretations between python-pathspec and Git

1 participant