gitignore: ignore a single CR at EOL

I ran into a tool that produced a `.gitignore` file with CRLF line
endings. I had not considered that case when implementing support for
`.gitignore`, so we interpreted the CR as part of the string, which of
course made the files not match.

This patch fixes the bug by ignoring a single CR at EOL. That seems to
be what Git does (I didn't see any information about it in the
documentation).
This commit is contained in:
Martin von Zweigbergk 2022-01-12 09:37:58 -08:00
parent 1e07b95e7b
commit 5b84e192fc

View File

@ -32,6 +32,7 @@ impl GitIgnoreLine {
let mut non_space_seen = false;
let mut prev_was_space = false;
let mut in_escape = false;
let input = input.strip_suffix('\r').unwrap_or(input);
for (i, c) in input.char_indices() {
if !prev_was_space && non_space_seen {
trimmed_len = i;
@ -341,6 +342,13 @@ mod tests {
// It's unclear how this should be interpreted, but we count spaces before
// escaped spaces
assert!(matches_file(b"a b \\ \n", "a b "));
// A single CR at EOL is ignored
assert!(matches_file(b"a\r\n", "a"));
assert!(!matches_file(b"a\r\n", "a\r"));
assert!(matches_file(b"a\r\r\n", "a\r"));
assert!(!matches_file(b"a\r\r\n", "a\r\r"));
assert!(matches_file(b"\ra\n", "\ra"));
assert!(!matches_file(b"\ra\n", "a"));
}
#[test]