Skip to content

Commit

Permalink
diff: do not emit unified diff header on absent/empty transitions
Browse files Browse the repository at this point in the history
---/+++ lines are part of unified diff hunks, not Git diff header.
  • Loading branch information
yuja committed Jul 14, 2024
1 parent 5223299 commit 5e2a9c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
26 changes: 18 additions & 8 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
6 changes: 0 additions & 6 deletions cli/tests/test_diff_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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###"
Expand Down Expand Up @@ -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
"###);
}

Expand Down

0 comments on commit 5e2a9c2

Please sign in to comment.