File tree 2 files changed +34
-1
lines changed
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 25
25
_BYTES_ENCODING = 'latin1'
26
26
27
27
28
+ class GitWildMatchPatternError (ValueError ):
29
+ """
30
+ The :class:`GitWildMatchPatternError` indicates an invalid git wild match
31
+ pattern.
32
+ """
33
+ pass
34
+
35
+
28
36
class GitWildMatchPattern (RegexPattern ):
29
37
"""
30
38
The :class:`GitWildMatchPattern` class represents a compiled Git
@@ -56,6 +64,7 @@ def pattern_to_regex(cls, pattern):
56
64
else :
57
65
raise TypeError ("pattern:{!r} is not a unicode or byte string." .format (pattern ))
58
66
67
+ original_pattern = pattern
59
68
pattern = pattern .strip ()
60
69
61
70
if pattern .startswith ('#' ):
@@ -137,6 +146,12 @@ def pattern_to_regex(cls, pattern):
137
146
# according to `git check-ignore` (v2.4.1).
138
147
pass
139
148
149
+ if not pattern_segs :
150
+ # After resolving the edge cases, we end up with no
151
+ # pattern at all. This must be because the pattern is
152
+ # invalid.
153
+ raise GitWildMatchPatternError ("Invalid git pattern: %r" % (original_pattern ,))
154
+
140
155
if not pattern_segs [- 1 ] and len (pattern_segs ) > 1 :
141
156
# A pattern ending with a slash ('/') will match all
142
157
# descendant paths if it is a directory but not if it is a
Original file line number Diff line number Diff line change 10
10
11
11
import pathspec .patterns .gitwildmatch
12
12
import pathspec .util
13
- from pathspec .patterns .gitwildmatch import GitWildMatchPattern
13
+ from pathspec .patterns .gitwildmatch import GitWildMatchPattern , GitWildMatchPatternError
14
14
15
15
if sys .version_info [0 ] >= 3 :
16
16
unichr = chr
@@ -528,3 +528,21 @@ def test_08_escape(self):
528
528
escaped = r"file\!with\*weird\#naming_\[1\].t\?t"
529
529
result = GitWildMatchPattern .escape (fname )
530
530
self .assertEqual (result , escaped )
531
+
532
+ def test_09_single_escape_fail (self ):
533
+ """
534
+ Test an escape on a line by itself.
535
+ """
536
+ self ._check_invalid_pattern ("\\ " )
537
+
538
+ def test_09_single_exclamation_mark_fail (self ):
539
+ """
540
+ Test an escape on a line by itself.
541
+ """
542
+ self ._check_invalid_pattern ("!" )
543
+
544
+ def _check_invalid_pattern (self , git_ignore_pattern ):
545
+ expected_message_pattern = re .escape (repr (git_ignore_pattern ))
546
+ with self .assertRaisesRegexp (GitWildMatchPatternError , expected_message_pattern ):
547
+ GitWildMatchPattern (git_ignore_pattern )
548
+
You can’t perform that action at this time.
0 commit comments