Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pathspec/patterns/gitignore/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,10 @@ def _translate_segment_glob(

if not is_expr_negated:
body = body.replace('/', '')
if body.startswith('^'):
if body.startswith(('^', ':', '.', '=')):
# Hyperscan reads a leading ':', '.' or '=' as the start of a POSIX
# class ("[:a:]") or collating element ("[.a.]", "[=a=]") and
# rejects the regex. Escape it.
body = '\\' + body
if not body:
raise _NeverMatchError()
Expand Down
14 changes: 14 additions & 0 deletions tests/test_08_gitignore_bracket_slash.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,17 @@ def test_negated_rule(self):
spec = GitIgnoreSpec.from_lines(['*', '!' + line], backend=backend)
self.assertFalse(spec.match_file('a.b'))
self.assertTrue(spec.match_file('a/b'))

def test_posix_like_body(self):
# Git has no "[:a:]" outside of a bracket expression, so "[:a:]" is just a
# set of ':' and 'a'. Native backends must not see a POSIX class here.
for backend in ('simple', 're2', 'hyperscan'):
with self.subTest(backend=backend):
require_backend(backend)
for line, char in (('[:alpha:]', 'p'), ('[.a.]', '.'), ('[=a=]', '=')):
with self.subTest(line=line):
spec = GitIgnoreSpec.from_lines([line, 'x' + line], backend=backend)
for path in ('a', char, 'xa', 'x' + char, 'dir/a'):
self.assertTrue(spec.match_file(path), path)
for path in ('b', 'xb', line):
self.assertFalse(spec.match_file(path), path)
Loading