Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negate with caret symbol as with the exclamation mark #78

Merged
merged 3 commits into from
Apr 24, 2023
Merged
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
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"})