From dcf24cdece0ed87aa597b3beed497c631fab1b9a Mon Sep 17 00:00:00 2001 From: wissaaamm <172216098+wissaaamm@users.noreply.github.com> Date: Sat, 5 Dec 2026 14:52:10 +0200 Subject: [PATCH] Recognize literal POSIX prefixes in gitignore bracket sets --- pathspec/patterns/gitignore/base.py | 81 ++++++++++++----------------- tests/test_02_gitignore_base.py | 3 ++ 2 files changed, 37 insertions(+), 47 deletions(-) diff --git a/pathspec/patterns/gitignore/base.py b/pathspec/patterns/gitignore/base.py index a312c70..dc818e0 100644 --- a/pathspec/patterns/gitignore/base.py +++ b/pathspec/patterns/gitignore/base.py @@ -94,6 +94,38 @@ def _has_ending_escape(pattern: str) -> bool: return (len(pattern) - len(pattern.rstrip('\\'))) % 2 == 1 +def _find_bracket_end(pattern: str, start: int) -> int: + """ + Find the closing bracket of the expression at *start*, or return the end + of *pattern* if the expression is unclosed. + """ + end = len(pattern) + index = start + 1 + if index < end and pattern[index] in '!^': + index += 1 + if index < end and pattern[index] == ']': + index += 1 + + posix_close = -1 + while index < end and pattern[index] != ']': + if pattern[index] == '\\': + index += 2 + continue + if pattern.startswith('[:', index): + if posix_close < index + 2: + posix_close = pattern.find(']', index + 2) + if posix_close == -1: + return end + # Git treats '[' and ':' as ordinary set members if the first + # closing bracket does not complete a POSIX class token. + if posix_close > index + 2 and pattern[posix_close - 1] == ':': + index = posix_close + 1 + continue + index += 1 + + return min(index, end) + + def _split_pattern(pattern: str) -> list[str]: """ Split the pattern by path separator (i.e., ``/``). A slash appearing in a @@ -117,25 +149,7 @@ def _split_pattern(pattern: str) -> list[str]: continue if pattern[i] == '[': - j = i + 1 - if j < len(pattern) and pattern[j] in '!^': - j += 1 - - if j < len(pattern) and pattern[j] == ']': - j += 1 - - while j < len(pattern) and pattern[j] != ']': - if pattern[j] == '\\': - j += 2 - elif pattern[j:j+2] == '[:': - close = pattern.find(':]', j + 2) - if close == -1: - j = len(pattern) - break - j = close + 2 - else: - j += 1 - + j = _find_bracket_end(pattern, i) if j < len(pattern): i = j + 1 continue @@ -416,34 +430,7 @@ def _translate_segment_glob( # - "[]-]" matches ']' and '-'. # - "[!]a-]" matches any character except ']', 'a' and '-'. bracket_start = i - 1 - j = i - - # Pass bracket expression negation. - if j < end and (pattern[j] == '!' or pattern[j] == '^'): - j += 1 - - # Pass first closing bracket if it is at the beginning of the - # expression. - if j < end and pattern[j] == ']': - j += 1 - - # Find closing bracket. Stop once we reach the end or find it. - while j < end and pattern[j] != ']': - if pattern[j] == '\\': - # Skip over an escaped character so an escaped closing bracket - # ("\]") does not end the bracket expression. - j += 2 - elif pattern[j:j+2] == '[:': - # Skip over a POSIX character class token ("[:name:]") so its - # internal closing bracket is not mistaken for the end of the whole - # bracket expression. - close = pattern.find(':]', j + 2) - if close == -1: - j = end - break - j = close + 2 - else: - j += 1 + j = _find_bracket_end(pattern, bracket_start) if j < end: # Found end of bracket expression. Increment j to be one past the diff --git a/tests/test_02_gitignore_base.py b/tests/test_02_gitignore_base.py index 1def04e..5803593 100644 --- a/tests/test_02_gitignore_base.py +++ b/tests/test_02_gitignore_base.py @@ -37,6 +37,8 @@ BRACKET_LITERAL_CASES: list[tuple[str, list[str], list[str]]] = [ ('[[]draft].md', ['[draft].md'], ['draft.md', 'adraft].md']), + ('[[:]draft].md', ['[draft].md', ':draft].md'], ['draft.md', 'xdraft].md']), + ('[[:x].md', ['[.md', ':.md', 'x.md'], ['a.md', '[[:x].md']), ('[a&&b].pdf', ['a.pdf', '&.pdf', 'b.pdf'], ['c.pdf', 'ab.pdf']), ('[a||b].pdf', ['a.pdf', '|.pdf', 'b.pdf'], ['c.pdf', 'ab.pdf']), ('[a~~b].pdf', ['a.pdf', '~.pdf', 'b.pdf'], ['c.pdf', 'ab.pdf']), @@ -58,6 +60,7 @@ BRACKET_SEP_CASES: list[tuple[str, list[str], list[str]]] = [ ('[a/b].txt', ['a.txt', 'b.txt'], ['c.txt', 'dir/a.txt', 'a/b.txt']), + ('[[:x/].txt', ['[.txt', ':.txt', 'x.txt'], ['dir/x.txt', 'a.txt']), ('[a/b].txt/', ['a.txt/', 'a.txt/file'], ['a.txt', 'dir/a.txt/file']), ('dir/[a/b].txt', ['dir/a.txt', 'dir/b.txt'], ['a.txt', 'deep/dir/a.txt']), ('/[a/b].txt', ['a.txt', 'b.txt'], ['dir/a.txt', 'a/b.txt']),