Skip to content

Commit

Permalink
Preserve "Binary files" line in color_only mode (#1649)
Browse files Browse the repository at this point in the history
Closes #320
  • Loading branch information
imbrish authored Mar 12, 2024
1 parent f49fd3b commit 9b775ec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/handlers/diff_header_misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
29 changes: 29 additions & 0 deletions src/tests/test_example_diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -2354,6 +2363,26 @@ rename to bar
diff --git a/qux b/qux
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 = "\
Expand Down

0 comments on commit 9b775ec

Please sign in to comment.