From 084983d4a82bdf8553be04290886737ba190ec2f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 13 Sep 2024 13:25:58 +0200 Subject: [PATCH] op-diff: Use op_summary template --- cli/src/commands/operation/diff.rs | 60 ++++++++++++------------ cli/tests/test_operations.rs | 73 +++++++++++++++--------------- 2 files changed, 65 insertions(+), 68 deletions(-) diff --git a/cli/src/commands/operation/diff.rs b/cli/src/commands/operation/diff.rs index 53dbff724db..7fe2c4533df 100644 --- a/cli/src/commands/operation/diff.rs +++ b/cli/src/commands/operation/diff.rs @@ -36,7 +36,6 @@ use jj_lib::revset; use jj_lib::revset::RevsetIteratorExt as _; use crate::cli_util::short_change_hash; -use crate::cli_util::short_operation_hash; use crate::cli_util::CommandHelper; use crate::cli_util::LogContentFormat; use crate::command_error::CommandError; @@ -48,6 +47,7 @@ use crate::formatter::Formatter; use crate::graphlog::get_graphlog; use crate::graphlog::Edge; use crate::graphlog::GraphStyle; +use crate::operation_templater::OperationTemplateLanguage; use crate::templater::TemplateRenderer; use crate::ui::Ui; @@ -124,42 +124,40 @@ pub fn cmd_op_diff( workspace_env.parse_template(&language, &text, CommitTemplateLanguage::wrap_commit)? }; + let from_op_summary_template = { + let workspace_env = workspace_command.env(); + let language = OperationTemplateLanguage::new( + from_op.op_store().root_operation_id(), + Some(from_op.id()), + workspace_env.operation_template_extensions(), + ); + let text = command + .settings() + .config() + .get_string("templates.op_summary")?; + workspace_env.parse_template(&language, &text, OperationTemplateLanguage::wrap_operation)? + }; + let to_op_summary_template = { + let workspace_env = workspace_command.env(); + let language = OperationTemplateLanguage::new( + to_op.op_store().root_operation_id(), + Some(to_op.id()), + workspace_env.operation_template_extensions(), + ); + let text = command + .settings() + .config() + .get_string("templates.op_summary")?; + workspace_env.parse_template(&language, &text, OperationTemplateLanguage::wrap_operation)? + }; ui.request_pager(); let mut formatter = ui.stdout_formatter(); formatter.with_label("op_log", |formatter| { write!(formatter, "From operation ")?; - write!( - formatter.labeled("id"), - "{}", - short_operation_hash(from_op.id()), - )?; - write!(formatter, ": ")?; - write!( - formatter.labeled("description"), - "{}", - if from_op.id() == from_op.op_store().root_operation_id() { - "root()" - } else { - &from_op.metadata().description - } - )?; + from_op_summary_template.format(&from_op, &mut *formatter)?; writeln!(formatter)?; write!(formatter, " To operation ")?; - write!( - formatter.labeled("id"), - "{}", - short_operation_hash(to_op.id()), - )?; - write!(formatter, ": ")?; - write!( - formatter.labeled("description"), - "{}", - if to_op.id() == to_op.op_store().root_operation_id() { - "root()" - } else { - &to_op.metadata().description - } - )?; + to_op_summary_template.format(&to_op, &mut *formatter)?; writeln!(formatter) })?; diff --git a/cli/tests/test_operations.rs b/cli/tests/test_operations.rs index de06bf19291..3d3c0742386 100644 --- a/cli/tests/test_operations.rs +++ b/cli/tests/test_operations.rs @@ -865,11 +865,10 @@ fn test_op_diff() { &repo_path, &["op", "diff", "--from", "0000000", "--to", "0000000"], ); - insta::assert_snapshot!(&stdout, @r###" - From operation 000000000000: root() - To operation 000000000000: root() - - "###); + insta::assert_snapshot!(&stdout, @r#" + From operation 000000000000 root() + To operation 000000000000 root() + "#); let stdout = test_env.jj_cmd_success(&repo_path, &["op", "diff", "--from", "@", "--to", "@"]); insta::assert_snapshot!(&stdout, @r#" From operation ea112f6a02be 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 check out git remote's default branch @@ -880,9 +879,9 @@ fn test_op_diff() { // `jj op diff --op @` should behave identically to `jj op diff --from // @- --to @` (if `@` is not a merge commit). let stdout = test_env.jj_cmd_success(&repo_path, &["op", "diff", "--from", "@-", "--to", "@"]); - insta::assert_snapshot!(&stdout, @r###" - From operation cba9d7096849: fetch from git remote into empty repo - To operation ea112f6a02be: check out git remote's default branch + insta::assert_snapshot!(&stdout, @r#" + From operation cba9d7096849 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 fetch from git remote into empty repo + To operation ea112f6a02be 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 check out git remote's default branch Changed commits: ○ Change sqpuoqvxutmz @@ -899,7 +898,7 @@ fn test_op_diff() { bookmark-1@origin: + tracked ulyvmwyz 1d843d1f bookmark-1 | Commit 1 - untracked ulyvmwyz 1d843d1f bookmark-1 | Commit 1 - "###); + "#); let stdout_without_from_to = test_env.jj_cmd_success(&repo_path, &["op", "diff"]); assert_eq!(stdout, stdout_without_from_to); @@ -1277,9 +1276,9 @@ fn test_op_diff_patch() { Parent commit : qpvuntsm 6b1027d2 (no description set) "###); let stdout = test_env.jj_cmd_success(&repo_path, &["op", "diff", "--op", "@-", "-p", "--git"]); - insta::assert_snapshot!(&stdout, @r###" - From operation b51416386f26: add workspace 'default' - To operation 6188e9d1f7da: snapshot working copy + insta::assert_snapshot!(&stdout, @r#" + From operation b51416386f26 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 add workspace 'default' + To operation 6188e9d1f7da 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 snapshot working copy Changed commits: ○ Change qpvuntsmwlqt @@ -1292,16 +1291,16 @@ fn test_op_diff_patch() { +++ b/file @@ -1,0 +1,1 @@ +a - "###); + "#); let stdout = test_env.jj_cmd_success(&repo_path, &["op", "diff", "--op", "@", "-p", "--git"]); - insta::assert_snapshot!(&stdout, @r###" - From operation 6188e9d1f7da: snapshot working copy - To operation 8f6a879bef11: new empty commit + insta::assert_snapshot!(&stdout, @r#" + From operation 6188e9d1f7da 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 snapshot working copy + To operation 8f6a879bef11 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 new empty commit Changed commits: ○ Change rlvkpnrzqnoo + rlvkpnrz 56950632 (empty) (no description set) - "###); + "#); // Squash the working copy commit. std::fs::write(repo_path.join("file"), "b\n").unwrap(); @@ -1349,16 +1348,16 @@ fn test_op_diff_patch() { Parent commit : qpvuntsm 2ac85fd1 (no description set) "###); let stdout = test_env.jj_cmd_success(&repo_path, &["op", "diff", "-p", "--git"]); - insta::assert_snapshot!(&stdout, @r###" - From operation c53f5f1afbc6: squash commits into 6b1027d2770cd0a39c468e525e52bf8c47e1464a - To operation e13dc1c7a3b3: abandon commit 9f4fb57fba25a7b47ce5980a5d9a4766778331e8 + insta::assert_snapshot!(&stdout, @r#" + From operation c53f5f1afbc6 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00 squash commits into 6b1027d2770cd0a39c468e525e52bf8c47e1464a + To operation e13dc1c7a3b3 2001-02-03 04:05:13.000 +07:00 - 2001-02-03 04:05:13.000 +07:00 abandon commit 9f4fb57fba25a7b47ce5980a5d9a4766778331e8 Changed commits: ○ Change yqosqzytrlsw + yqosqzyt 33f321c4 (empty) (no description set) ○ Change mzvwutvlkqwt - mzvwutvl hidden 9f4fb57f (empty) (no description set) - "###); + "#); } #[test] @@ -1442,9 +1441,9 @@ fn test_op_diff_sibling() { "--summary", ], ); - insta::assert_snapshot!(&stdout, @r###" - From operation b8584dcdb220: new empty commit - To operation 654f3215449e: describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22 + insta::assert_snapshot!(&stdout, @r#" + From operation b8584dcdb220 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00 new empty commit + To operation 654f3215449e 2001-02-03 04:05:12.000 +07:00 - 2001-02-03 04:05:12.000 +07:00 describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22 Changed commits: ○ Change qpvuntsmwlqt @@ -1457,7 +1456,7 @@ fn test_op_diff_sibling() { ○ Change zsuskulnrvyr - zsuskuln hidden 8afecaef A.2 A file2 - "###); + "#); let stdout = test_env.jj_cmd_success( &repo_path, &[ @@ -1472,9 +1471,9 @@ fn test_op_diff_sibling() { "--summary", ], ); - insta::assert_snapshot!(&stdout, @r###" - From operation 654f3215449e: describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22 - To operation b8584dcdb220: new empty commit + insta::assert_snapshot!(&stdout, @r#" + From operation 654f3215449e 2001-02-03 04:05:12.000 +07:00 - 2001-02-03 04:05:12.000 +07:00 describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22 + To operation b8584dcdb220 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00 new empty commit Changed commits: ○ Change mzvwutvlkqwt @@ -1487,7 +1486,7 @@ fn test_op_diff_sibling() { A file2 ○ Change qpvuntsmwlqt - qpvuntsm hidden 02ef2bc4 (empty) B - "###); + "#); } #[test] @@ -1516,9 +1515,9 @@ fn test_op_diff_word_wrap() { test_env.jj_cmd_ok(&repo_path, &["debug", "snapshot"]); // ui.log-word-wrap option works, and diff stat respects content width - insta::assert_snapshot!(render(&["op", "diff", "--from=@---", "--stat"], 40, true), @r###" - From operation b51416386f26: add workspace 'default' - To operation d12081b11443: snapshot working copy + insta::assert_snapshot!(render(&["op", "diff", "--from=@---", "--stat"], 40, true), @r#" + From operation b51416386f26 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 add workspace 'default' + To operation d12081b11443 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 snapshot working copy Changed commits: ○ Change sqpuoqvxutmz @@ -1565,14 +1564,14 @@ fn test_op_diff_word_wrap() { + untracked tqyxmszt 3e785984 bookmark-3@origin | Commit 3 - untracked (absent) - "###); + "#); // Graph width should be subtracted from the term width let config = r#"templates.commit_summary='"0 1 2 3 4 5 6 7 8 9"'"#; insta::assert_snapshot!( - render(&["op", "diff", "--from=@---", "--config-toml", config], 10, true), @r###" - From operation b51416386f26: add workspace 'default' - To operation d12081b11443: snapshot working copy + render(&["op", "diff", "--from=@---", "--config-toml", config], 10, true), @r#" + From operation b51416386f26 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 add workspace 'default' + To operation d12081b11443 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 snapshot working copy Changed commits: @@ -1637,7 +1636,7 @@ fn test_op_diff_word_wrap() { - untracked (absent) - "###); + "#); } #[test]