Skip to content

Commit

Permalink
Merge branch 'master' into caret-symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
cpburnz authored Apr 24, 2023
2 parents 57fbd3e + b9a014e commit 518db79
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
15 changes: 14 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ Change History
==============


0.11.2 (TBD)
------------

Bug fixes:

- `Pull #76`_: Add edge case: patterns that end with an escaped space
- `Issue #77`_: On bracket expression negation.


.. _`Pull #76`: https://github.com/cpburnz/python-pathspec/pull/76
.. _`Issue #77`: https://github.com/cpburnz/python-pathspec/issues/77


0.11.1 (2023-03-14)
-------------------

Expand All @@ -15,11 +28,11 @@ Improvements:
- `Pull #75`_: Fix partially unknown PathLike type.
- Convert `os.PathLike` to a string properly using `os.fspath`.


.. _`Issue #74`: https://github.com/cpburnz/python-pathspec/issues/74
.. _`Pull #75`: https://github.com/cpburnz/python-pathspec/pull/75



0.11.0 (2023-01-24)
-------------------

Expand Down
3 changes: 2 additions & 1 deletion pathspec/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"Avasam <https://github.com/Avasam>",
"yschroeder <https://github.com/yschroeder>",
"axesider <https://github.com/axesider>",
"tomruk <https://github.com/tomruk>",
]
__license__ = "MPL 2.0"
__version__ = "0.11.1"
__version__ = "0.11.2.dev1"
14 changes: 13 additions & 1 deletion 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 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 All @@ -312,10 +315,19 @@ def _translate_segment_glob(pattern: str) -> str:
j += 1
expr = '['

if pattern[i] == '!' or pattern[i] == '^':
if 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. Git supports the using a caret for negation.
# Maintain consistency with Git because that is the expected
# behavior.
expr += '^'
i += 1

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

def test_13_issue_77_regex(self):
"""
Test the resulting regex for regex bracket expression negation.
"""
regex, include = GitWildMatchPattern.pattern_to_regex('a[^b]c')
self.assertTrue(include)

equiv_regex, include = GitWildMatchPattern.pattern_to_regex('a[!b]c')
self.assertTrue(include)

self.assertEqual(regex, equiv_regex)

def test_13_negate_with_caret(self):
"""
Test negation using the caret symbol (^)
Expand Down

0 comments on commit 518db79

Please sign in to comment.