python-pathspec icon indicating copy to clipboard operation
python-pathspec copied to clipboard

Leading & trailing whitespace

Open CarterFendley opened this issue 1 year ago • 1 comments

Hello, I am using this library and noticing some odd results around white space. When the white space is leading or trailing, I do not get any matches.

# Leading whitespace does not match
>>> pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, ['   whitespace.txt']).match_file('   whitespace.txt')
False

# Trailing whitespace does not match
>>> pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, ['whitespace.txt   ']).match_file('whitespace.txt   ')
False

It seems to have no problem with whitespace internal to a string.

>>> pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, ['white   space.txt']).match_file('white   space.txt')
True

Can you help me understand what is going on here? Both whitespace.txt and whitespace.txt are valid file names so I would like to figure out why I can't match them.

CarterFendley avatar Jun 16 '24 02:06 CarterFendley

You have to escape leading and trailing spaces in patterns in order to match them in file names.

# Leading whitespace.
>>> pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, ['\\ \\ \\ whitespace.txt']).match_file('   whitespace.txt')
True

# Trailing whitespace.
>>> pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, ['whitespace.txt\\ \\ \\ ']).match_file('whitespace.txt   ')
True

cpburnz avatar Jun 27 '24 00:06 cpburnz

I'm closing this because I think I answered your question.

cpburnz avatar Apr 02 '25 01:04 cpburnz