summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPawel Dominiak <dominiak.pawel@gmail.com>2024-03-12 12:53:12 +0100
committerGitHub <noreply@github.com>2024-03-12 07:53:12 -0400
commit9b775ecd36bf6b399186219a0b9345ccd403d6ef (patch)
tree257dd07a354e1812821dcb728eeb155c74056bd2
parentf49fd3b012067e34c101d7dfc6cc3bbac1fe5ccc (diff)
Preserve "Binary files" line in color_only mode (#1649)
Closes #320
-rw-r--r--src/handlers/diff_header_misc.rs3
-rw-r--r--src/handlers/mod.rs2
-rw-r--r--src/tests/test_example_diffs.rs29
3 files changed, 33 insertions, 1 deletions
diff --git a/src/handlers/diff_header_misc.rs b/src/handlers/diff_header_misc.rs
index c5e4026b..c9404d6c 100644
--- a/src/handlers/diff_header_misc.rs
+++ b/src/handlers/diff_header_misc.rs
@@ -16,7 +16,8 @@ impl<'a> StateMachine<'a> {
return Ok(false);
}
- if self.test_diff_is_binary() {
+ // Preserve the "Binary files" line when diff lines should be kept unchanged.
+ if !self.config.color_only && self.test_diff_is_binary() {
// Print the "Binary files" line verbatim, if there was no "diff" line, or it
// listed different files but was not followed by header minus and plus lines.
// This can happen in output of standalone diff or git diff --no-index.
diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs
index 048da99d..fcbabefb 100644
--- a/src/handlers/mod.rs
+++ b/src/handlers/mod.rs
@@ -31,6 +31,8 @@ impl<'a> StateMachine<'a> {
// 2. Git diff emits lines describing submodule state such as "Submodule x/y/z contains
// untracked content"
//
+ // 3. When comparing binary files, diff can emit "Binary files ... differ" line.
+ //
// See https://github.com/dandavison/delta/issues/60#issuecomment-557485242 for a
// proposal for more robust parsing logic.
diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs
index 68a88658..e71d9885 100644
--- a/src/tests/test_example_diffs.rs
+++ b/src/tests/test_example_diffs.rs
@@ -1780,6 +1780,15 @@ src/align.rs:71: impl<'a> Alignment<'a> { │
assert_eq!(output, input);
}
+ #[test]
+ fn test_git_diff_binary_is_unchanged_under_color_only() {
+ let config = integration_test_utils::make_config_from_args(&["--color-only"]);
+ let input = BINARY_FILES_DIFFER_BETWEEN_OTHER;
+ let output = integration_test_utils::run_delta(input, &config);
+ let output = strip_ansi_codes(&output);
+ assert_eq!(output, input);
+ }
+
// See https://github.com/dandavison/delta/issues/371#issuecomment-720173435
#[test]
fn test_keep_plus_minus_markers_under_inspect_raw_lines() {
@@ -2356,6 +2365,26 @@ index 00de669..d47cd84 100644
Binary files a/qux and b/qux differ
";
+ const BINARY_FILES_DIFFER_BETWEEN_OTHER: &str = "\
+diff --git a/foo b/foo
+index 7b57bd29ea8a..4d3b8c11a4a2 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-abc
++def
+diff --git a/qux b/qux
+index 00de669..d47cd84 100644
+Binary files a/qux and b/qux differ
+diff --git a/bar b/bar
+index 7b57bd29ea8a..4d3b8c11a4a2 100644
+--- a/bar
++++ b/bar
+@@ -1 +1 @@
+-123
++456
+";
+
const DIFF_NO_INDEX_BINARY_FILES_DIFFER: &str = "\
diff --git foo bar sub dir/foo bar baz
index 329fbf5..481817c 100644