Skip to content
Open
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
81 changes: 34 additions & 47 deletions pathspec/patterns/gitignore/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/test_02_gitignore_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
Expand All @@ -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']),
Expand Down