From 8e727de2ab3589d1cd5d9530c0809d9793165edd Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 12 Sep 2024 16:20:57 +0200 Subject: [PATCH] undo: Report what operation has been undone in `jj op undo` --- CHANGELOG.md | 2 ++ cli/src/commands/operation/undo.rs | 22 ++++++++++++++++++++++ cli/src/config/templates.toml | 7 +++++++ cli/tests/test_duplicate_command.rs | 8 ++++++-- cli/tests/test_evolog_command.rs | 4 ++-- cli/tests/test_git_colocated.rs | 5 +++-- cli/tests/test_git_fetch.rs | 4 +++- cli/tests/test_git_import_export.rs | 4 +++- cli/tests/test_log_command.rs | 4 ++-- cli/tests/test_operations.rs | 20 ++++++++++---------- cli/tests/test_show_command.rs | 4 ++-- cli/tests/test_templater.rs | 4 ++-- 12 files changed, 64 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ef1952375..f753571657 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). allows to set a name for the remote instead of using the default `origin`. +* `jj op undo` now reports information on the operation that has been undone. + ### Fixed bugs * Fixed panic when parsing invalid conflict markers of a particular form. diff --git a/cli/src/commands/operation/undo.rs b/cli/src/commands/operation/undo.rs index 22bba29e47..d4e7900803 100644 --- a/cli/src/commands/operation/undo.rs +++ b/cli/src/commands/operation/undo.rs @@ -21,6 +21,7 @@ use super::DEFAULT_UNDO_WHAT; use crate::cli_util::CommandHelper; use crate::command_error::user_error; use crate::command_error::CommandError; +use crate::operation_templater::OperationTemplateLanguage; use crate::ui::Ui; /// Create a new operation that undoes an earlier operation @@ -70,5 +71,26 @@ pub fn cmd_op_undo( tx.repo_mut().set_view(new_view); tx.finish(ui, format!("undo operation {}", bad_op.id().hex()))?; + if let Some(mut formatter) = ui.status_formatter() { + write!(formatter, "Undid operation ")?; + let workspace_env = workspace_command.env(); + let language = OperationTemplateLanguage::new( + bad_op.op_store().root_operation_id(), + Some(bad_op.id()), + workspace_env.operation_template_extensions(), + ); + let text = command + .settings() + .config() + .get_string("templates.op_summary")?; + let template = workspace_env.parse_template( + &language, + &text, + OperationTemplateLanguage::wrap_operation, + )?; + template.format(&bad_op, &mut *formatter)?; + writeln!(formatter)?; + } + Ok(()) } diff --git a/cli/src/config/templates.toml b/cli/src/config/templates.toml index 6fd6b7c268..77c6995ea8 100644 --- a/cli/src/config/templates.toml +++ b/cli/src/config/templates.toml @@ -41,6 +41,13 @@ tag_list = ''' label("tag", name) ++ format_ref_targets(self) ++ "\n" ''' +op_summary = ''' +if(root, + separate(" ", self.id().short(), label("root", "root()")), + separate(" ", self.id().short(), format_time_range(self.time()), self.description().first_line()) +) +''' + [template-aliases] builtin_log_oneline = ''' if(root, diff --git a/cli/tests/test_duplicate_command.rs b/cli/tests/test_duplicate_command.rs index c9f0b23db5..62c57bd26d 100644 --- a/cli/tests/test_duplicate_command.rs +++ b/cli/tests/test_duplicate_command.rs @@ -75,7 +75,9 @@ fn test_duplicate() { let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["undo"]); insta::assert_snapshot!(stdout, @""); - insta::assert_snapshot!(stderr, @""); + insta::assert_snapshot!(stderr, @r#" + Undid operation c4b0b2a977fe 2001-02-03 04:05:17.000 +07:00 - 2001-02-03 04:05:17.000 +07:00 duplicate 1 commit(s) + "#); let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["duplicate" /* duplicates `c` */]); insta::assert_snapshot!(stdout, @""); insta::assert_snapshot!(stderr, @r###" @@ -277,7 +279,9 @@ fn test_undo_after_duplicate() { let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["undo"]); insta::assert_snapshot!(stdout, @""); - insta::assert_snapshot!(stderr, @""); + insta::assert_snapshot!(stderr, @r#" + Undid operation 5a2bcfcdd78b 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00 duplicate 1 commit(s) + "#); insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###" @ 2443ea76b0b1 a ◆ 000000000000 diff --git a/cli/tests/test_evolog_command.rs b/cli/tests/test_evolog_command.rs index f61b468692..4fc2b949e6 100644 --- a/cli/tests/test_evolog_command.rs +++ b/cli/tests/test_evolog_command.rs @@ -327,7 +327,7 @@ fn test_evolog_with_no_template() { let repo_path = test_env.env_root().join("repo"); let stderr = test_env.jj_cmd_cli_error(&repo_path, &["evolog", "-T"]); - insta::assert_snapshot!(stderr, @r###" + insta::assert_snapshot!(stderr, @r#" error: a value is required for '--template