Skip to content

Treat a reversed bracket range as a literal in gitignore patterns - #193

Open
shashank11yadav wants to merge 1 commit into
cpburnz:masterfrom
shashank11yadav:fix/reversed-range-gitignore-pattern
Open

shashank11yadav wants to merge 1 commit into
cpburnz:masterfrom
shashank11yadav:fix/reversed-range-gitignore-pattern

Conversation

@shashank11yadav

Copy link
Copy Markdown

Fixes #192

A gitignore pattern with a reversed bracket range, such as [z-a], raised a raw re.error in the default 'literal' mode. GitIgnoreBasicPattern uses that mode by default, so PathSpec.from_lines('gitignore', ['[z-a]']) failed to build.

The bracket expression was only checked with re.compile when errors='raise'. The check now runs in every mode. In 'raise' mode the same GitIgnorePatternError is raised as before. In the other modes the whole bracket expression is treated as literal text, the same fallback already used for an invalid POSIX class.

Test changes:

  • tests/test_03_gitignore_basic.py, test_c_2_invalid: this changes an existing maintainer assertion. The old block asserted that '[z-a]' and 'a[z-a]' raise re.error ("The re module fails to compile these"). The PR moves these two patterns into the literal list above it, with their expected regexes, and removes that assertRaises block. It also removes the re_PatternError import, which is no longer used. The reason is that the crash is the bug this PR fixes.
  • tests/test_08_gitignore_bracket_slash.py: new test_reversed_range checks GitIgnoreBasicPattern and GitIgnoreSpecPattern in 'literal', 'null' and 'raise' modes, plus an end-to-end PathSpec check.

Behaviour in literal mode: [z-a] matches a file named [z-a] and does not match z. Git's handling of reversed ranges is not part of these tests, and this PR does not change it, so the difference from Git for this line remains.

Testing:

  • The full suite, run as python3 -m unittest discover -t . -s tests/ from the clone root, passes: 323 tests run, 957 skipped, OK.
  • The new test and the updated test_c_2_invalid both error without the fix (re.PatternError: bad character range z-a) and pass with it. This was a local run. CI was not run.
  • Type checks (mypy, pyright) were not run.

A pattern such as "[z-a]" crashed with a raw re.error when errors was
"literal", which is the default for GitIgnoreBasicPattern. The bracket
expression was only checked in "raise" mode. Check it in every mode and,
outside "raise", treat the whole bracket expression as a literal, as is
already done for an invalid POSIX class.

This branch has not been deployed

No deployments
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.

PathSpec.from_lines('gitignore', ['[z-a]']) raises re.error, Git accepts the pattern

1 participant