Skip to content

Commit

Permalink
diff: add headers to file-by-file mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fowles committed Jul 4, 2024
1 parent c535b1c commit 481cf82
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
50 changes: 34 additions & 16 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,15 @@ pub fn show_file_by_file_diff(
let left_wc_dir = temp_dir.path().join("left");
let right_wc_dir = temp_dir.path().join("right");
let mut diff_stream = materialized_diff_stream(repo.store(), tree_diff);
let mut items: Vec<(
String,
RepoPathBuf,
(MaterializedTreeValue, MaterializedTreeValue),
)> = Vec::new();
async {
while let Some((path, diff)) = diff_stream.next().await {
let ui_path = path_converter.format_file_path(&path);
let (left_value, right_value) = diff?;

match (&left_value, &right_value) {
(_, MaterializedTreeValue::AccessDenied(source))
| (MaterializedTreeValue::AccessDenied(source), _) => {
Expand All @@ -699,25 +703,39 @@ pub fn show_file_by_file_diff(
writeln!(formatter, " {source}")?;
continue;
}
_ => {}
_ => {
items.push((ui_path, path, (left_value, right_value)));
}
}
let left_path = create_file(&path, &left_wc_dir, left_value)?;
let right_path = create_file(&path, &right_wc_dir, right_value)?;

invoke_external_diff(
ui,
formatter.raw(),
tool,
&maplit::hashmap! {
"left" => left_path.to_str().expect("temp_dir should be valid utf-8"),
"right" => right_path.to_str().expect("temp_dir should be valid utf-8"),
},
)
.map_err(DiffRenderError::DiffGenerate)?;
}
Ok::<(), DiffRenderError>(())
}
.block_on()
.block_on()?;
let num_items = items.len();
for (idx, (ui_path, path, (left_value, right_value))) in items.into_iter().enumerate() {
if idx > 0 {
writeln!(formatter.labeled("header"))?;
}
writeln!(
formatter.labeled("header"),
"JJ (#{} of {}): comparing {ui_path}",
idx + 1,
num_items,
)?;
let left_path = create_file(&path, &left_wc_dir, left_value)?;
let right_path = create_file(&path, &right_wc_dir, right_value)?;
invoke_external_diff(
ui,
formatter.raw(),
tool,
&maplit::hashmap! {
"left" => left_path.to_str().expect("temp_dir should be valid utf-8"),
"right" => right_path.to_str().expect("temp_dir should be valid utf-8"),
},
)
.map_err(DiffRenderError::DiffGenerate)?;
}
Ok(())
}

struct GitDiffPart {
Expand Down
20 changes: 20 additions & 0 deletions cli/tests/test_diff_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,14 +1142,20 @@ fn test_diff_external_file_by_file_tool() {
// diff without file patterns
insta::assert_snapshot!(
test_env.jj_cmd_success(&repo_path, &["diff", config]), @r###"
JJ (#1 of 3): comparing file1
==
file1
--
file1
JJ (#2 of 3): comparing file2
==
file2
--
file2
JJ (#3 of 3): comparing file3
==
file3
--
Expand All @@ -1159,6 +1165,7 @@ fn test_diff_external_file_by_file_tool() {
// diff with file patterns
insta::assert_snapshot!(
test_env.jj_cmd_success(&repo_path, &["diff", config, "file1"]), @r###"
JJ (#1 of 1): comparing file1
==
file1
--
Expand All @@ -1169,24 +1176,32 @@ fn test_diff_external_file_by_file_tool() {
test_env.jj_cmd_success(&repo_path, &["log", "-p", config]), @r###"
@ rlvkpnrz [email protected] 2001-02-03 08:05:09 39d9055d
│ (no description set)
│ JJ (#1 of 3): comparing file1
│ ==
│ file1
│ --
│ file1
│ JJ (#2 of 3): comparing file2
│ ==
│ file2
│ --
│ file2
│ JJ (#3 of 3): comparing file3
│ ==
│ file3
│ --
│ file3
◉ qpvuntsm [email protected] 2001-02-03 08:05:08 0ad4ef22
│ (no description set)
│ JJ (#1 of 2): comparing file1
│ ==
│ file1
│ --
│ file1
│ JJ (#2 of 2): comparing file2
│ ==
│ file2
│ --
Expand All @@ -1203,14 +1218,19 @@ fn test_diff_external_file_by_file_tool() {
(no description set)
JJ (#1 of 3): comparing file1
==
file1
--
file1
JJ (#2 of 3): comparing file2
==
file2
--
file2
JJ (#3 of 3): comparing file3
==
file3
--
Expand Down

0 comments on commit 481cf82

Please sign in to comment.