| 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 clap_complete::ArgValueCandidates; |
| 16 |
use itertools::Itertools as _; |
| 17 |
use jj_lib::object_id::ObjectId as _; |
| 18 |
use jj_lib::operation::Operation; |
| 19 |
use jj_lib::repo::Repo as _; |
| 20 |
|
| 21 |
use super::DEFAULT_REVERT_WHAT; |
| 22 |
use super::RevertWhatToRestore; |
| 23 |
use super::view_with_desired_portions_restored; |
| 24 |
use crate::cli_util::CommandHelper; |
| 25 |
use crate::command_error::CommandError; |
| 26 |
use crate::command_error::user_error; |
| 27 |
use crate::complete; |
| 28 |
use crate::ui::Ui; |
| 29 |
|
| 30 |
/// Create a new operation that reverts an earlier operation |
| 31 |
/// |
| 32 |
/// This reverts an individual operation by applying the inverse of the |
| 33 |
/// operation. |
| 34 |
#[derive(clap::Args, Clone, Debug)] |
| 35 |
pub struct OperationRevertArgs { |
| 36 |
/// The operation to revert |
| 37 |
/// |
| 38 |
/// Use `jj op log` to find an operation to revert. |
| 39 |
#[arg(default_value = "@")] |
| 40 |
#[arg(add = ArgValueCandidates::new(complete::operations))] |
| 41 |
operation: String, |
| 42 |
|
| 43 |
/// What portions of the local state to restore (can be repeated) |
| 44 |
/// |
| 45 |
/// This option is EXPERIMENTAL. |
| 46 |
#[arg(long, value_enum, default_values_t = DEFAULT_REVERT_WHAT)] |
| 47 |
what: Vec<RevertWhatToRestore>, |
| 48 |
} |
| 49 |
|
| 50 |
fn tx_description(op: &Operation) -> String { |
| 51 |
format!("revert operation {}", op.id().hex()) |
| 52 |
} |
| 53 |
|
| 54 |
pub async fn cmd_op_revert( |
| 55 |
ui: &mut Ui, |
| 56 |
command: &CommandHelper, |
| 57 |
args: &OperationRevertArgs, |
| 58 |
) -> Result<(), CommandError> { |
| 59 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 60 |
let target_op = workspace_command.resolve_single_op(&args.operation)?; |
| 61 |
let target_op_parent = match target_op.parents().await?.into_iter().at_most_one() { |
| 62 |
Ok(Some(op)) => op, |
| 63 |
Ok(None) => return Err(user_error("Cannot revert root operation")), |
| 64 |
Err(_) => return Err(user_error("Cannot revert a merge operation")), |
| 65 |
}; |
| 66 |
|
| 67 |
let mut tx = workspace_command.start_transaction(); |
| 68 |
let repo_loader = tx.base_repo().loader(); |
| 69 |
let repo_at_target_op = &repo_loader.load_at(&target_op).await?; |
| 70 |
let repo_at_target_op_parent = &repo_loader.load_at(&target_op_parent).await?; |
| 71 |
tx.repo_mut() |
| 72 |
.merge(repo_at_target_op, repo_at_target_op_parent) |
| 73 |
.await?; |
| 74 |
let new_view = view_with_desired_portions_restored( |
| 75 |
tx.repo().view().store_view(), |
| 76 |
tx.base_repo().view().store_view(), |
| 77 |
&args.what, |
| 78 |
); |
| 79 |
tx.repo_mut().set_view(new_view); |
| 80 |
if let Some(mut formatter) = ui.status_formatter() { |
| 81 |
write!(formatter, "Reverted operation: ")?; |
| 82 |
let template = tx.base_workspace_helper().operation_summary_template(); |
| 83 |
template.format(&target_op, formatter.as_mut())?; |
| 84 |
writeln!(formatter)?; |
| 85 |
} |
| 86 |
tx.finish(ui, tx_description(&target_op)).await?; |
| 87 |
|
| 88 |
Ok(()) |
| 89 |
} |