File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -68,7 +68,14 @@ def pattern_to_regex(
6868 raise TypeError (f"pattern:{ pattern !r} is not a unicode or byte string." )
6969
7070 original_pattern = pattern
71- pattern = pattern .strip ()
71+
72+ if pattern .endswith ('\\ ' ):
73+ # EDGE CASE: Spaces can be escaped with backslash.
74+ # If a pattern that ends with backslash followed by a space,
75+ # only strip from left.
76+ pattern = pattern .lstrip ()
77+ else :
78+ pattern = pattern .strip ()
7279
7380 if pattern .startswith ('#' ):
7481 # A pattern starting with a hash ('#') serves as a comment
Original file line number Diff line number Diff line change 1515 PathSpec )
1616from pathspec .util import (
1717 iter_tree_entries )
18+ from pathspec .patterns .gitwildmatch import GitWildMatchPatternError
1819from tests .util import (
1920 make_dirs ,
2021 make_files ,
@@ -119,6 +120,32 @@ def test_01_current_dir_paths(self):
119120 './src/test2/c/c.txt' ,
120121 })
121122
123+ def test_01_empty_path (self ):
124+ """
125+ Tests that patterns that end with an escaped space will be treated properly.
126+ """
127+ spec = PathSpec .from_lines ('gitwildmatch' , [
128+ '\\ ' ,
129+ 'abc\\ '
130+ ])
131+ test_files = [
132+ ' ' ,
133+ ' ' ,
134+ 'abc ' ,
135+ 'somefile' ,
136+ ]
137+ results = list (filter (spec .match_file , test_files ))
138+ self .assertEqual (results , [
139+ ' ' ,
140+ 'abc '
141+ ])
142+
143+ # An escape with double spaces is invalid.
144+ # Disallow it. Better to be safe than sorry.
145+ self .assertRaises (GitWildMatchPatternError , lambda : PathSpec .from_lines ('gitwildmatch' , [
146+ '\\ '
147+ ]))
148+
122149 def test_01_match_files (self ):
123150 """
124151 Tests that matching files one at a time yields the same results as
You can’t perform that action at this time.
0 commit comments