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
1 change: 1 addition & 0 deletions CHANGES_1.in.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ New features:

Bug fixes:

- A gitignore pattern segment made of three or more asterisks (e.g., `***/x`) is now treated as a double-asterisk, matching Git.
- 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
16 changes: 16 additions & 0 deletions pathspec/patterns/gitignore/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ def _strip_trailing_ws(pattern: str) -> str:
return pattern[:last_ws]


def _collapse_star_runs(segments: list[str]) -> list[str]:
"""
Replace each pattern segment that consists only of three or more asterisks
(e.g., ``***``) with a double-asterisk (``**``). Git's wildmatch treats a run
of asterisks as a single globstar when it spans a whole path segment.

*segments* (:class:`list` of :class:`str`) are the pattern segments.

Returns the modified segments (:class:`list` of :class:`str`).
"""
return [
'**' if len(seg) > 2 and seg.count('*') == len(seg) else seg
for seg in segments
]


def _sub_bracket_token(match: re.Match[str]) -> str:
"""
Translate a single token matched by :data:`_BRACKET_TOKEN_REGEX`. A token can
Expand Down
3 changes: 2 additions & 1 deletion pathspec/patterns/gitignore/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
_PosixClassError,
_RangeNotationError,
_TrailingBackslashError,
_collapse_star_runs,
_strip_trailing_ws)


Expand Down Expand Up @@ -221,7 +222,7 @@ def pattern_to_regex(
return (None, None)

# Split pattern into segments.
orig_segs = pattern_str.split('/')
orig_segs = _collapse_star_runs(pattern_str.split('/'))

# Check whether the pattern is specifically a directory pattern before
# normalization.
Expand Down
3 changes: 2 additions & 1 deletion pathspec/patterns/gitignore/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
_PosixClassError,
_RangeNotationError,
_TrailingBackslashError,
_collapse_star_runs,
_strip_trailing_ws)

_DIR_MARK = 'ps_d'
Expand Down Expand Up @@ -265,7 +266,7 @@ def pattern_to_regex(
return (None, None)

# Split pattern into segments.
orig_segs = pattern_str.split('/')
orig_segs = _collapse_star_runs(pattern_str.split('/'))

# Check whether the pattern is specifically a directory pattern before
# normalization.
Expand Down
54 changes: 54 additions & 0 deletions tests/test_10_gitignore_star_runs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Test runs of three or more asterisks that span a whole gitignore path segment.
"""

import unittest

from pathspec import GitIgnoreSpec
from pathspec.patterns.gitignore.basic import GitIgnoreBasicPattern
from pathspec.patterns.gitignore.spec import GitIgnoreSpecPattern

# Each case is (pattern, paths that Git matches, paths that Git does not match).
# Git's wildmatch treats a run of asterisks that spans a whole path segment as
# a single globstar (``**``).
CASES = (
('***/x', ('x', 'a/x', 'a/b/x'), ('xx', 'a/xx')),
('****/x', ('x', 'a/x', 'a/b/x'), ('xx',)),
('a/***', ('a/b', 'a/b/c'), ('a', 'b/a', 'b/a/c')),
('a/***/b', ('a/b', 'a/x/b', 'a/x/y/b'), ('a/x', 'b/x/b')),
('***', ('a', 'a/b', 'a/b/c'), ()),
# A run of asterisks inside a segment is still a single asterisk.
('a***b', ('ab', 'axxb'), ('a/b', 'a/x/b')),
)


class StarRunTest(unittest.TestCase):
"""
The :class:`StarRunTest` class tests asterisk runs against Git's behavior.
"""

def test_pattern_classes(self):
"""
Tests that both gitignore pattern classes treat a segment of asterisks
like a double-asterisk.
"""
for factory in (GitIgnoreBasicPattern, GitIgnoreSpecPattern):
for line, matches, misses in CASES:
with self.subTest(factory=factory.__name__, line=line):
pattern = factory(line)
for path in matches:
self.assertTrue(pattern.match_file(path), path)
for path in misses:
self.assertFalse(pattern.match_file(path), path)

def test_gitignore_spec(self):
"""
Tests that :class:`.GitIgnoreSpec` matches the same paths as Git.
"""
for line, matches, misses in CASES:
with self.subTest(line=line):
spec = GitIgnoreSpec.from_lines([line])
for path in matches:
self.assertTrue(spec.match_file(path), path)
for path in misses:
self.assertFalse(spec.match_file(path), path)
Loading