Skip to content

Commit

Permalink
Merge pull request #78 from tomruk/caret-symbol
Browse files Browse the repository at this point in the history
Negate with caret symbol as with the exclamation mark
  • Loading branch information
cpburnz committed Apr 24, 2023
2 parents b9a014e + 518db79 commit cebf13f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
7 changes: 5 additions & 2 deletions pathspec/patterns/gitwildmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,16 @@ def _translate_segment_glob(pattern: str) -> str:
# - "[]-]" matches ']' and '-'.
# - "[!]a-]" matches any character except ']', 'a' and '-'.
j = i
# Pass brack expression negation.
if j < end and pattern[j] == '!':

# 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] != ']':
j += 1
Expand Down
31 changes: 22 additions & 9 deletions tests/test_gitwildmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def test_12_asterisk_4_descendant(self):
'anydir/file.txt',
})

def test_13_issue_77_1_regex(self):
def test_13_issue_77_regex(self):
"""
Test the resulting regex for regex bracket expression negation.
"""
Expand All @@ -786,15 +786,28 @@ def test_13_issue_77_1_regex(self):

self.assertEqual(regex, equiv_regex)

def test_13_issue_77_2_results(self):
def test_13_negate_with_caret(self):
"""
Test that regex bracket expression negation works.
Test negation using the caret symbol (^)
"""
pattern = GitWildMatchPattern('a[^b]c')
pattern = GitWildMatchPattern("a[^gy]c")
results = set(filter(pattern.match_file, [
'abc',
'azc',
"agc",
"ayc",
"abc",
"adc",
]))
self.assertEqual(results, {
'azc',
})
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 cebf13f

Please sign in to comment.