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
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions CHANGES_1.in.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 3 additions & 8 deletions pathspec/patterns/gitignore/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
42 changes: 42 additions & 0 deletions tests/test_10_gitignore_negated_root.py
Original file line number Diff line number Diff line change
@@ -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'))