Skip to content

Commit cebf13f

Browse files
authored
Merge pull request #78 from tomruk/caret-symbol
Negate with caret symbol as with the exclamation mark
2 parents b9a014e + 518db79 commit cebf13f

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

pathspec/patterns/gitwildmatch.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,16 @@ def _translate_segment_glob(pattern: str) -> str:
290290
# - "[]-]" matches ']' and '-'.
291291
# - "[!]a-]" matches any character except ']', 'a' and '-'.
292292
j = i
293-
# Pass brack expression negation.
294-
if j < end and pattern[j] == '!':
293+
294+
# Pass bracket expression negation.
295+
if j < end and (pattern[j] == '!' or pattern[j] == '^'):
295296
j += 1
297+
296298
# Pass first closing bracket if it is at the beginning of the
297299
# expression.
298300
if j < end and pattern[j] == ']':
299301
j += 1
302+
300303
# Find closing bracket. Stop once we reach the end or find it.
301304
while j < end and pattern[j] != ']':
302305
j += 1

tests/test_gitwildmatch.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def test_12_asterisk_4_descendant(self):
774774
'anydir/file.txt',
775775
})
776776

777-
def test_13_issue_77_1_regex(self):
777+
def test_13_issue_77_regex(self):
778778
"""
779779
Test the resulting regex for regex bracket expression negation.
780780
"""
@@ -786,15 +786,28 @@ def test_13_issue_77_1_regex(self):
786786

787787
self.assertEqual(regex, equiv_regex)
788788

789-
def test_13_issue_77_2_results(self):
789+
def test_13_negate_with_caret(self):
790790
"""
791-
Test that regex bracket expression negation works.
791+
Test negation using the caret symbol (^)
792792
"""
793-
pattern = GitWildMatchPattern('a[^b]c')
793+
pattern = GitWildMatchPattern("a[^gy]c")
794794
results = set(filter(pattern.match_file, [
795-
'abc',
796-
'azc',
795+
"agc",
796+
"ayc",
797+
"abc",
798+
"adc",
797799
]))
798-
self.assertEqual(results, {
799-
'azc',
800-
})
800+
self.assertEqual(results, {"abc", "adc"})
801+
802+
def test_13_negate_with_exclamation_mark(self):
803+
"""
804+
Test negation using the exclamation mark (!)
805+
"""
806+
pattern = GitWildMatchPattern("a[!gy]c")
807+
results = set(filter(pattern.match_file, [
808+
"agc",
809+
"ayc",
810+
"abc",
811+
"adc",
812+
]))
813+
self.assertEqual(results, {"abc", "adc"})

0 commit comments

Comments
 (0)