| 1 |
// Copyright 2025 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use itertools::Itertools as _; |
| 16 |
use jj_lib::object_id::ObjectId as _; |
| 17 |
use jj_lib::op_store::OperationId; |
| 18 |
|
| 19 |
use crate::cli_util::CommandHelper; |
| 20 |
use crate::command_error::CommandError; |
| 21 |
use crate::command_error::internal_error; |
| 22 |
use crate::command_error::user_error; |
| 23 |
use crate::commands::operation::DEFAULT_REVERT_WHAT; |
| 24 |
use crate::commands::operation::view_with_desired_portions_restored; |
| 25 |
use crate::commands::undo::UNDO_OP_DESC_PREFIX; |
| 26 |
use crate::ui::Ui; |
| 27 |
|
| 28 |
/// Redo the most recently undone operation |
| 29 |
/// |
| 30 |
/// This is the natural counterpart of `jj undo`. Repeated invocations of `jj |
| 31 |
/// undo` and `jj redo` act similarly to Undo/Redo commands in a text editor. |
| 32 |
/// |
| 33 |
/// Use `jj op log` to visualize the log of past operations, including a |
| 34 |
/// detailed description of any past undo/redo operations. See also `jj op |
| 35 |
/// restore` to explicitly restore an older operation by its id (available in |
| 36 |
/// the operation log). |
| 37 |
#[derive(clap::Args, Clone, Debug)] |
| 38 |
pub struct RedoArgs {} |
| 39 |
|
| 40 |
const REDO_OP_DESC_PREFIX: &str = "redo: restore to operation "; |
| 41 |
|
| 42 |
pub async fn cmd_redo( |
| 43 |
ui: &mut Ui, |
| 44 |
command: &CommandHelper, |
| 45 |
_: &RedoArgs, |
| 46 |
) -> Result<(), CommandError> { |
| 47 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 48 |
|
| 49 |
let mut target_op = workspace_command.repo().operation().clone(); |
| 50 |
|
| 51 |
// Growing the "redo-stack" works very similar to the |
| 52 |
// [undo-stack](./undo.rs). `jj redo` and `jj undo` track their stacks |
| 53 |
// separately. |
| 54 |
// |
| 55 |
// - If the operation to redo is a regular one (neither an undo- or |
| 56 |
// redo-operation): Fail, because there is nothing to redo. |
| 57 |
// - If the operation to redo is an undo-operation, try to redo it (by restoring |
| 58 |
// its parent operation). |
| 59 |
// - If the operation to redo is a redo-operation itself, redo the operation the |
| 60 |
// early redo-operation restored to. |
| 61 |
// - If the operation to restore to is a redo-operation itself, restore directly |
| 62 |
// to the original operation. This avoids creating a linked list of |
| 63 |
// redo-operations, which subsequently may have to be walked with an |
| 64 |
// inefficient loop. |
| 65 |
// |
| 66 |
// This described behavior leads to "jumping over" old redo-stacks if the |
| 67 |
// current one grows into it. Consider the following op-log example, where |
| 68 |
// redo-stacks are shown on the left and undo-stacks on the right: |
| 69 |
// |
| 70 |
// +------- "redo: restore C" * I |
| 71 |
// | | |
| 72 |
// | +----- "redo: restore D" * H |
| 73 |
// | | | |
| 74 |
// | | * G "undo: restore A" -------+ |
| 75 |
// | | | | |
| 76 |
// | | +- "redo: restore D" * F | |
| 77 |
// | | | | | |
| 78 |
// | | | * E "undo: restore A" ---+ | |
| 79 |
// | | | | | | |
| 80 |
// | +-> +----------------> * D "undo: restore B" -+ | | |
| 81 |
// | | | | | |
| 82 |
// +----------------------> * C | | | |
| 83 |
// | | | | |
| 84 |
// * B <----------------+ | | |
| 85 |
// | | | |
| 86 |
// * A <------------------+ <-+ |
| 87 |
// |
| 88 |
// The first interesting operation here is H: |
| 89 |
// - Attempt to redo G. |
| 90 |
// - G is an undo-operation, so attempt to restore its parent F. |
| 91 |
// - F is a redo-operation. Restore its original operation D, instead of F. |
| 92 |
// |
| 93 |
// The operation I is also noteworthy: |
| 94 |
// - Attempt to redo H. |
| 95 |
// - H is a redo-operation restoring to D, so attempt to redo D. |
| 96 |
// - D is an undo-operation. Redo it by restoring its parent C. |
| 97 |
// |
| 98 |
if let Some(target_op_hex) = target_op |
| 99 |
.metadata() |
| 100 |
.description |
| 101 |
.strip_prefix(REDO_OP_DESC_PREFIX) |
| 102 |
{ |
| 103 |
let target_op_id = OperationId::try_from_hex(target_op_hex).ok_or_else(|| { |
| 104 |
internal_error("Failed to parse ID of target operation in redo-stack") |
| 105 |
})?; |
| 106 |
target_op = workspace_command |
| 107 |
.repo() |
| 108 |
.loader() |
| 109 |
.load_operation(&target_op_id) |
| 110 |
.await?; |
| 111 |
} |
| 112 |
|
| 113 |
if !target_op |
| 114 |
.metadata() |
| 115 |
.description |
| 116 |
.starts_with(UNDO_OP_DESC_PREFIX) |
| 117 |
{ |
| 118 |
// cannot redo a non-undo-operation |
| 119 |
return Err(user_error("Nothing to redo")); |
| 120 |
} |
| 121 |
|
| 122 |
let mut target_op_parent = target_op |
| 123 |
.parents() |
| 124 |
.await? |
| 125 |
.into_iter() |
| 126 |
.exactly_one() |
| 127 |
.map_err(|_| internal_error("Undo operation should have a single parent"))?; |
| 128 |
|
| 129 |
// Avoid the creation of a linked list by restoring to the original |
| 130 |
// operation directly, if we're about to restore a redo-operation. If |
| 131 |
// we didn't do this, repeated calls of `jj undo; jj redo` would create |
| 132 |
// an ever-growing linked list of redo-operations that restore each |
| 133 |
// other. Calling `jj redo` one more time would have to redo a potential |
| 134 |
// undo-operation at the very beginning of the linked list, which would |
| 135 |
// require walking the entire thing unnecessarily. |
| 136 |
if let Some(target_op_parent_hex) = target_op_parent |
| 137 |
.metadata() |
| 138 |
.description |
| 139 |
.strip_prefix(REDO_OP_DESC_PREFIX) |
| 140 |
{ |
| 141 |
let target_op_parent_id = |
| 142 |
OperationId::try_from_hex(target_op_parent_hex).ok_or_else(|| { |
| 143 |
internal_error("Failed to parse ID of target operation's parent in redo-stack") |
| 144 |
})?; |
| 145 |
target_op_parent = workspace_command |
| 146 |
.repo() |
| 147 |
.loader() |
| 148 |
.load_operation(&target_op_parent_id) |
| 149 |
.await?; |
| 150 |
} |
| 151 |
|
| 152 |
let mut tx = workspace_command.start_transaction(); |
| 153 |
let new_view = view_with_desired_portions_restored( |
| 154 |
target_op_parent.view().await?.store_view(), |
| 155 |
tx.base_repo().view().store_view(), |
| 156 |
&DEFAULT_REVERT_WHAT, |
| 157 |
); |
| 158 |
tx.repo_mut().set_view(new_view); |
| 159 |
if let Some(mut formatter) = ui.status_formatter() { |
| 160 |
write!(formatter, "Restored to operation: ")?; |
| 161 |
let template = tx.base_workspace_helper().operation_summary_template(); |
| 162 |
template.format(&target_op_parent, formatter.as_mut())?; |
| 163 |
writeln!(formatter)?; |
| 164 |
} |
| 165 |
tx.finish( |
| 166 |
ui, |
| 167 |
format!("{REDO_OP_DESC_PREFIX}{}", target_op_parent.id().hex()), |
| 168 |
) |
| 169 |
.await?; |
| 170 |
|
| 171 |
Ok(()) |
| 172 |
} |