Skip to content

Commit 2a0e876

Browse files
committed
undo: show hint when workspace is removed by operation
When an operation like `jj undo` or `jj op revert` removes the current workspace, show a helpful hint explaining what happened and how to recover.
1 parent 711484d commit 2a0e876

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

cli/src/cli_util.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,9 +2168,18 @@ See https://docs.jj-vcs.dev/latest/working-copy/#stale-working-copy \
21682168
if self.may_update_working_copy {
21692169
if let Some(new_commit) = &maybe_new_wc_commit {
21702170
self.update_working_copy(ui, maybe_old_wc_commit.as_ref(), new_commit)?;
2171-
} else {
2172-
// It seems the workspace was deleted, so we shouldn't try to
2173-
// update it.
2171+
} else if maybe_old_wc_commit.is_some() {
2172+
// Workspace was deleted by this operation
2173+
writeln!(
2174+
ui.hint_default(),
2175+
"Workspace '{name}' was removed by this operation.",
2176+
name = self.workspace_name().as_symbol()
2177+
)?;
2178+
writeln!(
2179+
ui.hint_no_heading(),
2180+
"Use `jj workspace add` to create a new one, or `jj op revert` to restore \
2181+
the previous state."
2182+
)?;
21742183
}
21752184
}
21762185

cli/tests/test_undo_redo_commands.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ fn test_undo_root_operation() {
2020
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2121
let work_dir = test_env.work_dir("repo");
2222

23+
// Undoing the first operation after init restores to root operation,
24+
// which removes the workspace. Show a helpful hint.
2325
let output = work_dir.run_jj(["undo"]);
2426
insta::assert_snapshot!(output, @r"
2527
------- stderr -------
2628
Restored to operation: 000000000000 root()
29+
Hint: Workspace 'default' was removed by this operation.
30+
Use `jj workspace add` to create a new one, or `jj op revert` to restore the previous state.
2731
[EOF]
2832
");
2933

@@ -152,6 +156,8 @@ fn test_undo_with_rev_arg_falls_back_to_revert() {
152156
Warning: `jj undo <operation>` is deprecated; use `jj op revert <operation>` instead
153157
Reverted operation: 8f47435a3990 (2001-02-03 08:05:07) add workspace 'default'
154158
Rebased 1 descendant commits
159+
Hint: Workspace 'default' was removed by this operation.
160+
Use `jj workspace add` to create a new one, or `jj op revert` to restore the previous state.
155161
[EOF]
156162
");
157163

cli/tests/test_workspaces.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,12 @@ fn test_workspaces_forget() {
992992
.run_jj(["workspace", "add", "../secondary"])
993993
.success();
994994
let output = main_dir.run_jj(["workspace", "forget"]);
995-
insta::assert_snapshot!(output, @"");
995+
insta::assert_snapshot!(output, @r"
996+
------- stderr -------
997+
Hint: Workspace 'default' was removed by this operation.
998+
Use `jj workspace add` to create a new one, or `jj op revert` to restore the previous state.
999+
[EOF]
1000+
");
9961001

9971002
// When listing workspaces, only the secondary workspace shows up
9981003
let output = main_dir.run_jj(["workspace", "list"]);

0 commit comments

Comments
 (0)