From 42161257caf31e625de94a97291dfe600e072e32 Mon Sep 17 00:00:00 2001 From: Jonathan Frere Date: Tue, 4 Feb 2025 14:29:48 +0100 Subject: [PATCH] cli: op undo: show a warning when undoing an undo operation One common issue that users run into with `jj undo` is that they expect it to behave like an "undo stack", in which every consecutive `jj undo` execution moves the current state further back in history. This is (currently) an incorrect intuition - running multiple `jj undo` commands back-to-back will only result in the previous non-undo change being reverted, then un-reverted, then reverted, and so on. This change adds a hint when the user runs `jj undo` multiple times consecutively, suggesting that they may be looking for `jj op restore`, which allows them to revert the whole state of the repository back to a previous snapshot. --- CHANGELOG.md | 3 +++ cli/src/commands/operation/undo.rs | 14 +++++++++++ cli/tests/test_undo.rs | 38 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb10921de2..fc45958b25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### New features +* `jj undo` now shows a hint when undoing an undo operation that the user may + be looking for `jj op restore` instead. + * Template functions `truncate_start()` and `truncate_end()` gained an optional `ellipsis` parameter; passing this prepends or appends the ellipsis to the content if it is truncated to fit the maximum width. diff --git a/cli/src/commands/operation/undo.rs b/cli/src/commands/operation/undo.rs index 9e5520e79c..781cc3eee8 100644 --- a/cli/src/commands/operation/undo.rs +++ b/cli/src/commands/operation/undo.rs @@ -70,6 +70,7 @@ pub fn cmd_op_undo( &args.what, ); tx.repo_mut().set_view(new_view); + let was_undo_op = *tx.repo().view() == parent_op.view()?; if let Some(mut formatter) = ui.status_formatter() { write!(formatter, "Undid operation: ")?; let template = tx.base_workspace_helper().operation_summary_template(); @@ -78,5 +79,18 @@ pub fn cmd_op_undo( } tx.finish(ui, format!("undo operation {}", bad_op.id().hex()))?; + if args.operation == "@" && was_undo_op { + writeln!( + ui.hint_default(), + "This action reverted an 'undo' operation. The repository is now in the same state as \ + it was before the original 'undo'." + )?; + writeln!( + ui.hint_default(), + "If your goal is to undo multiple operations, consider using `jj op log` to see past \ + states, and `jj op restore` to restore one of these states." + )?; + } + Ok(()) } diff --git a/cli/tests/test_undo.rs b/cli/tests/test_undo.rs index 1d1d1666e6..2211877c01 100644 --- a/cli/tests/test_undo.rs +++ b/cli/tests/test_undo.rs @@ -385,6 +385,44 @@ fn test_bookmark_track_untrack_undo() { "###); } +#[test] +fn test_shows_a_warning_when_undoing_an_undo_operation_as_bare_jj_undo() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); + let repo_path = test_env.env_root().join("repo"); + + test_env.jj_cmd_ok(&repo_path, &["new"]); + test_env.jj_cmd_ok(&repo_path, &["undo"]); + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["undo"]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r" + Undid operation: 2d5b73a97567 (2001-02-03 08:05:09) undo operation 289cb69a8458456474a77cc432e8009b99f039cdcaf19ba4526753e97d70fee3fd0f410ff2b7c1d10cf0c2501702e7a85d58f9d813cdca567c377431ec4d2b97 + Working copy now at: rlvkpnrz 65b6b74e (empty) (no description set) + Parent commit : qpvuntsm 230dd059 (empty) (no description set) + Hint: This action reverted an 'undo' operation. The repository is now in the same state as it was before the original 'undo'. + Hint: If your goal is to undo multiple operations, consider using `jj op log` to see past states, and `jj op restore` to restore one of these states. + "); +} + +#[test] +fn test_shows_no_warning_when_undoing_a_specific_undo_change() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); + let repo_path = test_env.env_root().join("repo"); + + test_env.jj_cmd_ok(&repo_path, &["new"]); + test_env.jj_cmd_ok(&repo_path, &["undo"]); + let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log"]); + let op_id_hex = stdout[3..15].to_string(); + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["undo", &op_id_hex]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r" + Undid operation: 2d5b73a97567 (2001-02-03 08:05:09) undo operation 289cb69a8458456474a77cc432e8009b99f039cdcaf19ba4526753e97d70fee3fd0f410ff2b7c1d10cf0c2501702e7a85d58f9d813cdca567c377431ec4d2b97 + Working copy now at: rlvkpnrz 65b6b74e (empty) (no description set) + Parent commit : qpvuntsm 230dd059 (empty) (no description set) + "); +} + fn get_bookmark_output(test_env: &TestEnvironment, repo_path: &Path) -> String { // --quiet to suppress deleted bookmarks hint test_env.jj_cmd_success(repo_path, &["bookmark", "list", "--all-remotes", "--quiet"])