From 237a2299ea834905118ab82aa6076397874d560b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=92=95=E5=92=95=E5=92=95?= <42111195+lexingtonhibiki@users.noreply.github.com> Date: Tue, 3 Nov 2026 00:00:35 +0800 Subject: [PATCH 1/3] Document negated root gitignore no-op fix --- CHANGES.rst | 3 +++ CHANGES_1.in.rst | 2 ++ 2 files changed, 5 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 671e0a6..d1d94df 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 9da2b23..7e73876 100644 --- a/CHANGES_1.in.rst +++ b/CHANGES_1.in.rst @@ -21,6 +21,8 @@ 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`. From 3d4fe56f43fd7548f7f412c46168fa67b52faaf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=92=95=E5=92=95=E5=92=95?= <42111195+lexingtonhibiki@users.noreply.github.com> Date: Tue, 3 Nov 2026 00:03:00 +0800 Subject: [PATCH 2/3] Treat negated root-only gitignore patterns as no-ops --- pathspec/patterns/gitignore/spec.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pathspec/patterns/gitignore/spec.py b/pathspec/patterns/gitignore/spec.py index 0bc41d2..c4125f7 100644 --- a/pathspec/patterns/gitignore/spec.py +++ b/pathspec/patterns/gitignore/spec.py @@ -246,11 +246,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 @@ -261,9 +256,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. From 09e2f26ca8a34b9f85465231f98abd0534df6794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=92=95=E5=92=95=E5=92=95?= <42111195+lexingtonhibiki@users.noreply.github.com> Date: Tue, 3 Nov 2026 00:04:07 +0800 Subject: [PATCH 3/3] Test negated root patterns across gitignore backends --- tests/test_10_gitignore_negated_root.py | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/test_10_gitignore_negated_root.py 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'))