Skip to content

Commit 8534317

Browse files
committed
Add edge case: patterns that end with an escaped space
1 parent 6fd6bee commit 8534317

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

pathspec/patterns/gitwildmatch.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff 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

tests/test_pathspec.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
PathSpec)
1616
from pathspec.util import (
1717
iter_tree_entries)
18+
from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
1819
from 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

0 commit comments

Comments
 (0)