From fda3563a121900cee383c56f2224008406ab3b7f Mon Sep 17 00:00:00 2001 From: Matt Kulukundis Date: Sun, 30 Jun 2024 15:02:33 -0400 Subject: [PATCH] diff: add headers to file-by-file mode --- cli/src/diff_util.rs | 50 +++++++++++++++++++++++----------- cli/tests/test_diff_command.rs | 20 ++++++++++++++ 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/cli/src/diff_util.rs b/cli/src/diff_util.rs index 7dc54c0318..efd3304757 100644 --- a/cli/src/diff_util.rs +++ b/cli/src/diff_util.rs @@ -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), _) => { @@ -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 { diff --git a/cli/tests/test_diff_command.rs b/cli/tests/test_diff_command.rs index 216abc395d..d208c1a13a 100644 --- a/cli/tests/test_diff_command.rs +++ b/cli/tests/test_diff_command.rs @@ -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 -- @@ -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 -- @@ -1169,24 +1176,32 @@ fn test_diff_external_file_by_file_tool() { test_env.jj_cmd_success(&repo_path, &["log", "-p", config]), @r###" @ rlvkpnrz test.user@example.com 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 test.user@example.com 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 │ -- @@ -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 --