From e3055e5aafef9ea4300aaa0adc1500a5e0768fa8 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 14 Jul 2024 19:39:44 +0900 Subject: [PATCH] diff: do not emit unified diff header on absent/empty transitions ---/+++ lines are part of unified diff hunks, not Git diff header. --- cli/src/diff_util.rs | 26 ++++++++++++++++++-------- cli/tests/test_diff_command.rs | 6 ------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/cli/src/diff_util.rs b/cli/src/diff_util.rs index 5ab89d385..a61d7a495 100644 --- a/cli/src/diff_util.rs +++ b/cli/src/diff_util.rs @@ -1037,14 +1037,10 @@ pub fn show_git_diff( (None, Some(right_mode)) => { writeln!(formatter, "new file mode {right_mode}")?; writeln!(formatter, "index {left_hash}..{right_hash}")?; - writeln!(formatter, "--- /dev/null")?; - writeln!(formatter, "+++ b/{path_string}")?; } (Some(left_mode), None) => { writeln!(formatter, "deleted file mode {left_mode}")?; writeln!(formatter, "index {left_hash}..{right_hash}")?; - writeln!(formatter, "--- a/{path_string}")?; - writeln!(formatter, "+++ /dev/null")?; } (Some(left_mode), Some(right_mode)) => { if left_mode != right_mode { @@ -1056,15 +1052,29 @@ pub fn show_git_diff( } else if left_hash != right_hash { writeln!(formatter, "index {left_hash}..{right_hash} {left_mode}")?; } - if left_part.content != right_part.content { - writeln!(formatter, "--- a/{path_string}")?; - writeln!(formatter, "+++ b/{path_string}")?; - } } (None, None) => panic!("either left or right part should be present"), } Ok(()) })?; + + if left_part.content == right_part.content { + continue; // no content hunks + } + + let left_path = match left_part.mode { + Some(_) => format!("a/{path_string}"), + None => "/dev/null".to_owned(), + }; + let right_path = match right_part.mode { + Some(_) => format!("b/{path_string}"), + None => "/dev/null".to_owned(), + }; + formatter.with_label("file_header", |formatter| { + writeln!(formatter, "--- {left_path}")?; + writeln!(formatter, "+++ {right_path}")?; + Ok(()) + })?; show_unified_diff_hunks( formatter, &left_part.content, diff --git a/cli/tests/test_diff_command.rs b/cli/tests/test_diff_command.rs index ac9d03bf3..acc6208dd 100644 --- a/cli/tests/test_diff_command.rs +++ b/cli/tests/test_diff_command.rs @@ -335,8 +335,6 @@ fn test_diff_file_mode() { diff --git a/file1 b/file1 new file mode 100755 index 0000000000..e69de29bb2 - --- /dev/null - +++ b/file1 diff --git a/file2 b/file2 new file mode 100755 index 0000000000..d00491fd7e @@ -354,8 +352,6 @@ fn test_diff_file_mode() { diff --git a/file4 b/file4 new file mode 100644 index 0000000000..e69de29bb2 - --- /dev/null - +++ b/file4 "###); let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-r@-", "--git"]); insta::assert_snapshot!(stdout, @r###" @@ -409,8 +405,6 @@ fn test_diff_file_mode() { diff --git a/file4 b/file4 deleted file mode 100755 index e69de29bb2..0000000000 - --- a/file4 - +++ /dev/null "###); }