diff --git a/CHANGES.rst b/CHANGES.rst index c366638..5187e89 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -24,6 +24,9 @@ New features: Bug fixes: +- Treat a negated root-only gitignore pattern (``!/``) as a no-op instead of re-including directories. + +- Match escaped internal path separators in gitignore patterns without treating them as trailing backslashes. - Hyperscan backend no longer fails on multibyte UTF-8 characters. - `Pull #123`_: Ignore invalid gitignore bracket ranges for `GitIgnoreSpec`. - `Pull #128`_: Support POSIX character classes (e.g. `[[:alpha:]]`) in gitignore bracket expressions. diff --git a/CHANGES_1.in.rst b/CHANGES_1.in.rst index 684ee08..8b61e3f 100644 --- a/CHANGES_1.in.rst +++ b/CHANGES_1.in.rst @@ -21,6 +21,9 @@ New features: Bug fixes: +- Treat a negated root-only gitignore pattern (``!/``) as a no-op instead of re-including directories. + +- Match escaped internal path separators in gitignore patterns without treating them as trailing backslashes. - Hyperscan backend no longer fails on multibyte UTF-8 characters. - `Pull #123`_: Ignore invalid gitignore bracket ranges for `GitIgnoreSpec`. - `Pull #128`_: Support POSIX character classes (e.g. `[[:alpha:]]`) in gitignore bracket expressions. diff --git a/pathspec/patterns/gitignore/spec.py b/pathspec/patterns/gitignore/spec.py index 4d7aef2..ee07122 100644 --- a/pathspec/patterns/gitignore/spec.py +++ b/pathspec/patterns/gitignore/spec.py @@ -250,11 +250,6 @@ def pattern_to_regex( # a literal hash (i.e., '\#'). return (None, None) - elif pattern_str == '/': - # EDGE CASE: According to `git check-ignore` (v2.4.1), a single '/' does - # not match any file. - return (None, None) - if pattern_str.startswith('!'): # A pattern starting with an exclamation mark ('!') negates the pattern # (exclude instead of include). Escape the exclamation mark with a @@ -265,9 +260,9 @@ def pattern_to_regex( else: include = True - if not pattern_str: - # A blank pattern is a null-operation (neither includes nor excludes - # files). + if not pattern_str or pattern_str == '/': + # Blank and root-only patterns are null-operations, including after + # removing the negation prefix (e.g., '!/'). return (None, None) # Split pattern into segments. diff --git a/tests/test_10_gitignore_negated_root.py b/tests/test_10_gitignore_negated_root.py new file mode 100644 index 0000000..d0201bb --- /dev/null +++ b/tests/test_10_gitignore_negated_root.py @@ -0,0 +1,42 @@ +"""A root-only pattern remains a no-op after its negation prefix is removed.""" + +from unittest import TestCase + +from pathspec.patterns.gitignore.spec import GitIgnoreSpecPattern + +from .test_06_gitignore import GitIgnoreSpecMixin + + +class NegatedRootTest(GitIgnoreSpecMixin, TestCase): + + def test_root_pattern_is_noop(self): + for pattern in ('/', '!/', '!/ ', '!/\r\n'): + for value in (pattern, pattern.encode('ascii')): + for errors in (None, 'null', 'literal', 'raise'): + with self.subTest(pattern=value, errors=errors): + self.assertEqual( + GitIgnoreSpecPattern.pattern_to_regex(value, errors=errors), + (None, None), + ) + + def test_negated_root_does_not_reinclude_ancestor(self): + # Git check-ignore and git add --dry-run both keep the child ignored: + # '!/' does not reopen 'a/', so the final negation cannot reach it. + for root_pattern in ('!/', '!/ ', '!/\r\n', '/', '!//', '', '# comment'): + lines = ['*', root_pattern, '!a/keep.txt'] + for sub_test in self.parameterize_from_lines(lines): + with sub_test() as spec: + for path in ('a/', 'a/keep.txt', 'a/b/keep.txt'): + result = spec.check_file(path) + self.assertEqual((result.include, result.index), (True, 0)) + + def test_actual_directory_negation_still_reincludes(self): + for sub_test in self.parameterize_from_lines(['*', '!a/', '!a/keep.txt']): + with sub_test() as spec: + self.assertFalse(spec.match_file('a/keep.txt')) + + def test_escaped_exclamation_is_literal(self): + for sub_test in self.parameterize_from_lines([r'\!/']): + with sub_test() as spec: + self.assertTrue(spec.match_file('!/keep.txt')) + self.assertFalse(spec.match_file('a/keep.txt'))