python-pathspec
python-pathspec copied to clipboard
Leading & trailing whitespace
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.
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
I'm closing this because I think I answered your question.