Skip to content

Keep a terminal newline as part of a gitignore filename - #152

Open
kokotatan wants to merge 2 commits into
cpburnz:masterfrom
kokotatan:fix-terminal-newline-filename-matches
Open

kokotatan wants to merge 2 commits into
cpburnz:masterfrom
kokotatan:fix-terminal-newline-filename-matches

Conversation

@kokotatan

Copy link
Copy Markdown

Summary

A gitignore rule such as foo currently matches a path named foo\n in the simple and Hyperscan backends, although Git keeps the newline as part of the filename. Python's $ anchor may match before a final newline; Hyperscan has the same behavior for $ and \Z.

Generate Python's strict \Z end anchor, and translate it to the equivalent \z spelling before compiling RE2/Hyperscan expressions. Keep escaped literal backslash-Z filenames intact and preserve the directory-marker splitting logic. RE2's existing split $ already has strict end-of-text semantics. The same translation keeps a custom RegexPattern using a Python \Z anchor consistent across backends.

The change is distinct from merged #139 (globstars containing newlines in parent directories) and open #150 (trimming whitespace from pattern lines). This fixes matching of filenames that themselves end with a newline.

Validation

  • New public regression tests fail on the original upstream code: 40 failing subtests, no test errors. Tests cover both pattern classes, string/byte compiled patterns, all three backends, negation, and escaped literal backslashes.
  • Full unittest suite: 227 tests passed, 14 platform skips, with RE2 and Hyperscan installed alongside the simple backend on CPython 3.12.12.
  • Independent git check-ignore --stdin -z oracle: 480 comparisons across ten pattern configurations and three backends. Mismatches decrease from 46 to 8; all 38 corrected cases concern terminal-newline handling, and there are zero new mismatches. The eight remaining simple-backend ancestor-negation mismatches are identical on untouched upstream and remain outside this change.
  • mypy pathspec tests/check_usage.py and pyright pathspec tests/check_usage.py report the same 5 and 8 diagnostics, respectively, on this patch and an untouched upstream checkout. These concern missing RE2 typing and existing _typing.py/usage-check diagnostics; no new diagnostic is introduced.
  • Sphinx documentation builds with warnings as errors; git diff --check passes.

AI assistance: OpenAI Codex prepared and validated the patch and tests. Claude Opus independently reviewed the relevant source and backend contracts before submission. No human review is claimed.

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.

1 participant