From 3c1fd02fd9c92cc567724f5051b3c73d2f75464f Mon Sep 17 00:00:00 2001 From: Naoya Sakabe Date: Thu, 22 Jul 2021 17:58:10 +0900 Subject: [PATCH 1/3] Strip a neglected CR character in ingest_line --- src/delta.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/delta.rs b/src/delta.rs index e8f3938d6..d296d5589 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -184,6 +184,15 @@ impl<'a> StateMachine<'a> { .to_string() }; self.line = ansi::strip_ansi_codes(&self.raw_line); + + // Strip the neglected CR. + // (CR-LF is unfortunately split by git because it adds ansi escapes between them. + // Thus byte_lines library can't remove the CR properly.) + if let Some(c) = self.line.bytes().nth_back(0) { + if c == b'\r' { + self.line.truncate(self.line.len() - 1); + } + } } /// Should a handle_* function be called on this element? From 230d52988ee3a21bda30ad0add57b1d1473289f7 Mon Sep 17 00:00:00 2001 From: Naoya Sakabe Date: Thu, 22 Jul 2021 23:57:04 +0900 Subject: [PATCH 2/3] Add test_orphan_carriage_return_is_stripped --- src/tests/test_example_diffs.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index 1a1d99914..c605715e9 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -296,6 +296,16 @@ commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e ); } + #[test] + fn test_orphan_carriage_return_is_stripped() { + let config = integration_test_utils::make_config_from_args(&[]); + let output = integration_test_utils::run_delta( + GIT_DIFF_SINGLE_HUNK_WITH_SEQUENCE_OF_CR_ESCAPE_SEQUENCES_LF, + &config, + ); + assert!(output.bytes().all(|b: u8| b != b'\r')); + } + #[test] fn test_commit_decoration_style_omit() { _do_test_commit_style_no_decoration(&[ @@ -1708,6 +1718,20 @@ Date: Thu May 14 11:13:17 2020 -0400 #[allow(dead_code)] "; + const GIT_DIFF_SINGLE_HUNK_WITH_SEQUENCE_OF_CR_ESCAPE_SEQUENCES_LF: &str = "\ +diff --git a/src/main.rs b/src/main.rs +index f346a8c..e443b63 100644 +--- a/src/main.rs ++++ b/src/main.rs +@@ -1,5 +1,4 @@ +-deleted line\r +-\r + fn main() {\r + println!(\"existing line\");\r ++ println!(\"added line\");\r + }\r +"; + const DIFF_IN_DIFF: &str = "\ diff --git a/0001-Init.patch b/0001-Init.patch deleted file mode 100644 From 5a36d50f0fa019c35e8a6e4084025cb05abfc585 Mon Sep 17 00:00:00 2001 From: Naoya Sakabe Date: Fri, 23 Jul 2021 04:28:16 +0900 Subject: [PATCH 3/3] Eliminate redundant assignment in if-let-Some --- src/delta.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/delta.rs b/src/delta.rs index d296d5589..bc691748e 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -188,10 +188,8 @@ impl<'a> StateMachine<'a> { // Strip the neglected CR. // (CR-LF is unfortunately split by git because it adds ansi escapes between them. // Thus byte_lines library can't remove the CR properly.) - if let Some(c) = self.line.bytes().nth_back(0) { - if c == b'\r' { - self.line.truncate(self.line.len() - 1); - } + if let Some(b'\r') = self.line.bytes().nth_back(0) { + self.line.truncate(self.line.len() - 1); } }