Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pathspec/patterns/gitignore/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def escape(s: AnyStr) -> AnyStr:
# Reference: https://git-scm.com/docs/gitignore#_pattern_format
out_string = ''.join((f"\\{x}" if x in '\\[]!*#?' else x) for x in string)

# EDGE CASE: Git strips trailing spaces from a pattern unless they are
# escaped with a backslash. Escape them so an escaped filename that ends
# with a space still matches that file.
stripped = out_string.rstrip(' ')
trailing = len(out_string) - len(stripped)
if trailing:
out_string = stripped + '\\ ' * trailing

if return_type is bytes:
out_bytes = out_string.encode(_BYTES_ENCODING)
return out_bytes # type: ignore[return-value]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_02_gitignore_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_01_escape_bytes(self):
Test escaping binary strings.
"""
byte_to_escaped = {__b: b'\\' + __b for __b in (
__c.encode(_BYTES_ENCODING) for __c in '\\[]!*#?'
__c.encode(_BYTES_ENCODING) for __c in '\\[]!*#? '
)}
for char_ord in range(256):
char_byte = chr(char_ord).encode(_BYTES_ENCODING)
Expand All @@ -32,7 +32,7 @@ def test_01_escape_str(self):
"""
Test escaping unicode strings.
"""
char_to_escaped = {__c: f"\\{__c}" for __c in '\\[]!*#?'}
char_to_escaped = {__c: f"\\{__c}" for __c in '\\[]!*#? '}
for char_ord in range(128):
char = chr(char_ord)
escape_val = _GitIgnoreBasePattern.escape(char)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_04_gitignore_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,18 @@ def test_08_escape(self):
result = GitIgnoreSpecPattern.escape(fname)
self.assertEqual(result, escaped)

def test_08_escape_trailing_space(self):
"""
Test that escaping a filename with a trailing space keeps the space, so
the escaped pattern still matches the file. Git strips unescaped trailing
spaces from a pattern.
"""
for fname in ['foo ', 'trailing ', ' ']:
escaped = GitIgnoreSpecPattern.escape(fname)
self.assertTrue(escaped.endswith('\\ '), (fname, escaped))
pattern = GitIgnoreSpecPattern(escaped)
self.assertEqual(set(filter(pattern.match_file, [fname])), {fname}, (fname, escaped))

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