diff: do not emit unified diff header on absent/empty transitions

---/+++ lines are part of unified diff hunks, not Git diff header.
This commit is contained in:
Yuya Nishihara 2024-07-14 19:39:44 +09:00
parent 53f7acbc42
commit e3055e5aaf
2 changed files with 18 additions and 14 deletions

View file

@ -1037,14 +1037,10 @@ pub fn show_git_diff(
(None, Some(right_mode)) => { (None, Some(right_mode)) => {
writeln!(formatter, "new file mode {right_mode}")?; writeln!(formatter, "new file mode {right_mode}")?;
writeln!(formatter, "index {left_hash}..{right_hash}")?; writeln!(formatter, "index {left_hash}..{right_hash}")?;
writeln!(formatter, "--- /dev/null")?;
writeln!(formatter, "+++ b/{path_string}")?;
} }
(Some(left_mode), None) => { (Some(left_mode), None) => {
writeln!(formatter, "deleted file mode {left_mode}")?; writeln!(formatter, "deleted file mode {left_mode}")?;
writeln!(formatter, "index {left_hash}..{right_hash}")?; writeln!(formatter, "index {left_hash}..{right_hash}")?;
writeln!(formatter, "--- a/{path_string}")?;
writeln!(formatter, "+++ /dev/null")?;
} }
(Some(left_mode), Some(right_mode)) => { (Some(left_mode), Some(right_mode)) => {
if left_mode != right_mode { if left_mode != right_mode {
@ -1056,15 +1052,29 @@ pub fn show_git_diff(
} else if left_hash != right_hash { } else if left_hash != right_hash {
writeln!(formatter, "index {left_hash}..{right_hash} {left_mode}")?; 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"), (None, None) => panic!("either left or right part should be present"),
} }
Ok(()) 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( show_unified_diff_hunks(
formatter, formatter,
&left_part.content, &left_part.content,

View file

@ -335,8 +335,6 @@ fn test_diff_file_mode() {
diff --git a/file1 b/file1 diff --git a/file1 b/file1
new file mode 100755 new file mode 100755
index 0000000000..e69de29bb2 index 0000000000..e69de29bb2
--- /dev/null
+++ b/file1
diff --git a/file2 b/file2 diff --git a/file2 b/file2
new file mode 100755 new file mode 100755
index 0000000000..d00491fd7e index 0000000000..d00491fd7e
@ -354,8 +352,6 @@ fn test_diff_file_mode() {
diff --git a/file4 b/file4 diff --git a/file4 b/file4
new file mode 100644 new file mode 100644
index 0000000000..e69de29bb2 index 0000000000..e69de29bb2
--- /dev/null
+++ b/file4
"###); "###);
let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-r@-", "--git"]); let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-r@-", "--git"]);
insta::assert_snapshot!(stdout, @r###" insta::assert_snapshot!(stdout, @r###"
@ -409,8 +405,6 @@ fn test_diff_file_mode() {
diff --git a/file4 b/file4 diff --git a/file4 b/file4
deleted file mode 100755 deleted file mode 100755
index e69de29bb2..0000000000 index e69de29bb2..0000000000
--- a/file4
+++ /dev/null
"###); "###);
} }