diff --git a/CHANGES_1.in.rst b/CHANGES_1.in.rst index f0c6bf4..5d752bb 100644 --- a/CHANGES_1.in.rst +++ b/CHANGES_1.in.rst @@ -61,6 +61,7 @@ Bug fixes: - `Pull #182`_: Preserve subclass state when shallow-copying regular expression patterns, including subclasses without an include parameter. - `Pull #184`_: Preserve overridden `RegexPattern.match_file()` implementations when selecting a backend. - `Pull #188`_: Treat a trailing `**\` as an invalid gitignore pattern. +- A directory pattern ending in a run of three or more asterisks (e.g., ``d/***/``) now requires a descendant directory like ``d/**/``, so it no longer matches ``d/`` or its direct files. - `Issue #192`_ / `Pull #193`_: Pattern `[z-a]` raises re.error, Git accepts the pattern. .. _`Issue #116`: https://github.com/cpburnz/python-pathspec/issues/116 diff --git a/pathspec/patterns/gitignore/spec.py b/pathspec/patterns/gitignore/spec.py index 25d9ded..83ad1bc 100644 --- a/pathspec/patterns/gitignore/spec.py +++ b/pathspec/patterns/gitignore/spec.py @@ -293,12 +293,14 @@ def pattern_to_regex( # A terminal directory globstar with a concrete prefix must consume a # descendant segment before the directory marker. Keep this semantic bit - # separate from normalized segments, which collapse adjacent ``**``. + # separate from normalized segments, which collapse adjacent ``**``. A run + # of three or more asterisks is a globstar too (e.g., ``d/***/``). + star_segs = _collapse_star_runs(orig_segs) requires_dir_descendant = ( is_dir_pattern - and len(orig_segs) > 2 - and orig_segs[-2] == '**' - and any(seg not in ('', '**') for seg in orig_segs[:-2]) + and len(star_segs) > 2 + and star_segs[-2] == '**' + and any(seg not in ('', '**') for seg in star_segs[:-2]) ) # Normalize pattern to make processing easier. diff --git a/tests/test_06_gitignore.py b/tests/test_06_gitignore.py index a927801..82c1b88 100644 --- a/tests/test_06_gitignore.py +++ b/tests/test_06_gitignore.py @@ -1131,6 +1131,28 @@ def test_c(self): {"top.txt", "d/"}, {"d/top.txt", "d/child/", "a/b/deep.txt"}, ), + # A run of three or more asterisks is a globstar, so these behave like + # the "**" patterns above. + ( + "d/***/", + {"d/", "d/top.txt"}, + {"d/child/", "d/child/file.txt", "d/child/grandchild/deep.txt"}, + ), + ( + "/d/****/", + {"d/", "d/top.txt"}, + {"d/child/", "d/child/file.txt"}, + ), + ( + "**/d/***/", + {"d/top.txt", "x/d/", "x/d/top.txt"}, + {"d/child/file.txt", "x/d/child/file.txt"}, + ), + ( + "a/**/***/", + {"a/", "a/top.txt"}, + {"a/child/", "a/child/file.txt", "a/b/top.txt"}, + ), ] for pattern, unignored, ignored in cases: for sub_test in self.parameterize_from_lines([pattern]):