From 9302492483b06ca1613aa8b11cf08cef93930b12 Mon Sep 17 00:00:00 2001 From: cpburnz <2126043+cpburnz@users.noreply.github.com> Date: Sun, 23 Apr 2023 15:25:43 -0400 Subject: [PATCH 1/2] Fix issue 77 --- CHANGES.rst | 11 ++++++++++- pathspec/_meta.py | 3 ++- pathspec/patterns/gitwildmatch.py | 9 +++++---- tests/test_gitwildmatch.py | 25 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f37c491..825dac8 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,16 @@ Change History ============== +0.11.2 (TBD) +------------ + +Bug fixes: + +- `Issue #77`_: On bracket expression negation. + +.. _`Issue #77`: https://github.com/cpburnz/python-pathspec/issues/77 + + 0.11.1 (2023-03-14) ------------------- @@ -19,7 +29,6 @@ Improvements: .. _`Pull #75`: https://github.com/cpburnz/python-pathspec/pull/75 - 0.11.0 (2023-01-24) ------------------- diff --git a/pathspec/_meta.py b/pathspec/_meta.py index 80942a5..3f417db 100644 --- a/pathspec/_meta.py +++ b/pathspec/_meta.py @@ -50,6 +50,7 @@ "Avasam ", "yschroeder ", "axesider ", + "tomruk ", ] __license__ = "MPL 2.0" -__version__ = "0.11.1" +__version__ = "0.11.2.dev1" diff --git a/pathspec/patterns/gitwildmatch.py b/pathspec/patterns/gitwildmatch.py index 94d3115..8593872 100644 --- a/pathspec/patterns/gitwildmatch.py +++ b/pathspec/patterns/gitwildmatch.py @@ -306,16 +306,17 @@ def _translate_segment_glob(pattern: str) -> str: expr = '[' if pattern[i] == '!': - # Braket expression needs to be negated. + # 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. To maintain consistency with undefined behavior, - # I am escaping the '^' as well. - expr += '\\^' + # 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 diff --git a/tests/test_gitwildmatch.py b/tests/test_gitwildmatch.py index 7dccaee..6a51683 100644 --- a/tests/test_gitwildmatch.py +++ b/tests/test_gitwildmatch.py @@ -773,3 +773,28 @@ def test_12_asterisk_4_descendant(self): self.assertEqual(results, { 'anydir/file.txt', }) + + def test_13_issue_77_1_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_issue_77_2_results(self): + """ + Test that regex bracket expression negation works. + """ + pattern = GitWildMatchPattern('a[^b]c') + results = set(filter(pattern.match_file, [ + 'abc', + 'azc', + ])) + self.assertEqual(results, { + 'azc', + }) From b9a014e560af033591d8cfe7734deb929cc52f67 Mon Sep 17 00:00:00 2001 From: cpburnz <2126043+cpburnz@users.noreply.github.com> Date: Sun, 23 Apr 2023 16:19:57 -0400 Subject: [PATCH 2/2] Update CHANGES --- CHANGES.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 825dac8..f5a473b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,8 +8,11 @@ Change History 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 @@ -25,6 +28,7 @@ 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