Skip to content

Commit

Permalink
Merge pull request #50 from SebastiaanZ/bug/44-index-error
Browse files Browse the repository at this point in the history
Address #44: raise informative exception for invalid git patterns
  • Loading branch information
cpburnz authored Jul 18, 2021
2 parents c00b332 + ed4481f commit 816aba4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions pathspec/patterns/gitwildmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
_BYTES_ENCODING = 'latin1'


class GitWildMatchPatternError(ValueError):
"""
The :class:`GitWildMatchPatternError` indicates an invalid git wild match
pattern.
"""
pass


class GitWildMatchPattern(RegexPattern):
"""
The :class:`GitWildMatchPattern` class represents a compiled Git
Expand Down Expand Up @@ -56,6 +64,7 @@ def pattern_to_regex(cls, pattern):
else:
raise TypeError("pattern:{!r} is not a unicode or byte string.".format(pattern))

original_pattern = pattern
pattern = pattern.strip()

if pattern.startswith('#'):
Expand Down Expand Up @@ -137,6 +146,12 @@ def pattern_to_regex(cls, pattern):
# according to `git check-ignore` (v2.4.1).
pass

if not pattern_segs:
# After resolving the edge cases, we end up with no
# pattern at all. This must be because the pattern is
# invalid.
raise GitWildMatchPatternError("Invalid git pattern: %r" % (original_pattern,))

if not pattern_segs[-1] and len(pattern_segs) > 1:
# A pattern ending with a slash ('/') will match all
# descendant paths if it is a directory but not if it is a
Expand Down
20 changes: 19 additions & 1 deletion pathspec/tests/test_gitwildmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pathspec.patterns.gitwildmatch
import pathspec.util
from pathspec.patterns.gitwildmatch import GitWildMatchPattern
from pathspec.patterns.gitwildmatch import GitWildMatchPattern, GitWildMatchPatternError

if sys.version_info[0] >= 3:
unichr = chr
Expand Down Expand Up @@ -528,3 +528,21 @@ def test_08_escape(self):
escaped = r"file\!with\*weird\#naming_\[1\].t\?t"
result = GitWildMatchPattern.escape(fname)
self.assertEqual(result, escaped)

def test_09_single_escape_fail(self):
"""
Test an escape on a line by itself.
"""
self._check_invalid_pattern("\\")

def test_09_single_exclamation_mark_fail(self):
"""
Test an escape on a line by itself.
"""
self._check_invalid_pattern("!")

def _check_invalid_pattern(self, git_ignore_pattern):
expected_message_pattern = re.escape(repr(git_ignore_pattern))
with self.assertRaisesRegexp(GitWildMatchPatternError, expected_message_pattern):
GitWildMatchPattern(git_ignore_pattern)

0 comments on commit 816aba4

Please sign in to comment.