From 5b84e192fc633a7b2f55aeac760993539dc1a350 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 12 Jan 2022 09:37:58 -0800 Subject: [PATCH] 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). --- lib/src/gitignore.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/src/gitignore.rs b/lib/src/gitignore.rs index 1fa906462..dc90ce6b5 100644 --- a/lib/src/gitignore.rs +++ b/lib/src/gitignore.rs @@ -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]