Skip to content

Commit

Permalink
Negate with caret symbol as with the exclamation mark
Browse files Browse the repository at this point in the history
  • Loading branch information
tomruk committed Apr 24, 2023
1 parent 8534317 commit 6b58e23
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
12 changes: 2 additions & 10 deletions pathspec/patterns/gitwildmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,10 @@ def _translate_segment_glob(pattern: str) -> str:
j += 1
expr = '['

if pattern[i] == '!':
# Braket expression needs to be negated.
if pattern[i] == '!' or pattern[i] == '^':
# Bracket expression needs to be negated.
expr += '^'
i += 1
elif pattern[i] == '^':
# POSIX declares that the regex bracket expression negation
# "[^...]" is undefined in a glob pattern. Python's
# `fnmatch.translate()` escapes the caret ('^') as a
# literal. To maintain consistency with undefined behavior,
# I am escaping the '^' as well.
expr += '\\^'
i += 1

# Build regex bracket expression. Escape slashes so they are
# treated as literal slashes by regex as defined by POSIX.
Expand Down
26 changes: 26 additions & 0 deletions tests/test_gitwildmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,29 @@ def test_12_asterisk_4_descendant(self):
self.assertEqual(results, {
'anydir/file.txt',
})

def test_13_negate_with_caret(self):
"""
Test negation using the caret symbol (^)
"""
pattern = GitWildMatchPattern("a[^gy]c")
results = set(filter(pattern.match_file, [
"agc",
"ayc",
"abc",
"adc",
]))
self.assertEqual(results, {"abc", "adc"})

def test_13_negate_with_exclamation_mark(self):
"""
Test negation using the exclamation mark (!)
"""
pattern = GitWildMatchPattern("a[!gy]c")
results = set(filter(pattern.match_file, [
"agc",
"ayc",
"abc",
"adc",
]))
self.assertEqual(results, {"abc", "adc"})

0 comments on commit 6b58e23

Please sign in to comment.