Skip to content

Commit

Permalink
Fix the empty result issue in add -p (#664)
Browse files Browse the repository at this point in the history
* Strip a neglected CR character in ingest_line

* Add test_orphan_carriage_return_is_stripped

* Eliminate redundant assignment in if-let-Some
  • Loading branch information
norisio authored Jul 22, 2021
1 parent e566469 commit 63fdebb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ 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(b'\r') = self.line.bytes().nth_back(0) {
self.line.truncate(self.line.len() - 1);
}
}

/// Should a handle_* function be called on this element?
Expand Down
24 changes: 24 additions & 0 deletions src/tests/test_example_diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&[
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 63fdebb

Please sign in to comment.