Skip to content

Commit 6b58e23

Browse files
committed
Negate with caret symbol as with the exclamation mark
1 parent 8534317 commit 6b58e23

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

pathspec/patterns/gitwildmatch.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,10 @@ def _translate_segment_glob(pattern: str) -> str:
312312
j += 1
313313
expr = '['
314314

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

328320
# Build regex bracket expression. Escape slashes so they are
329321
# treated as literal slashes by regex as defined by POSIX.

tests/test_gitwildmatch.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,29 @@ def test_12_asterisk_4_descendant(self):
773773
self.assertEqual(results, {
774774
'anydir/file.txt',
775775
})
776+
777+
def test_13_negate_with_caret(self):
778+
"""
779+
Test negation using the caret symbol (^)
780+
"""
781+
pattern = GitWildMatchPattern("a[^gy]c")
782+
results = set(filter(pattern.match_file, [
783+
"agc",
784+
"ayc",
785+
"abc",
786+
"adc",
787+
]))
788+
self.assertEqual(results, {"abc", "adc"})
789+
790+
def test_13_negate_with_exclamation_mark(self):
791+
"""
792+
Test negation using the exclamation mark (!)
793+
"""
794+
pattern = GitWildMatchPattern("a[!gy]c")
795+
results = set(filter(pattern.match_file, [
796+
"agc",
797+
"ayc",
798+
"abc",
799+
"adc",
800+
]))
801+
self.assertEqual(results, {"abc", "adc"})

0 commit comments

Comments
 (0)