diff --git a/pathspec/patterns/gitignore/base.py b/pathspec/patterns/gitignore/base.py index 70cdc00..e6fc77e 100644 --- a/pathspec/patterns/gitignore/base.py +++ b/pathspec/patterns/gitignore/base.py @@ -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() diff --git a/tests/test_08_gitignore_bracket_slash.py b/tests/test_08_gitignore_bracket_slash.py index 5a74d98..e73a67b 100644 --- a/tests/test_08_gitignore_bracket_slash.py +++ b/tests/test_08_gitignore_bracket_slash.py @@ -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)