| 1 |
// Copyright 2020-2023 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 std::io::Write as _; |
| 16 |
use std::iter; |
| 17 |
use std::slice; |
| 18 |
|
| 19 |
use clap_complete::ArgValueCandidates; |
| 20 |
use itertools::Itertools as _; |
| 21 |
use jj_lib::op_walk; |
| 22 |
|
| 23 |
use crate::cli_util::CommandHelper; |
| 24 |
use crate::cli_util::short_operation_hash; |
| 25 |
use crate::command_error::CommandError; |
| 26 |
use crate::command_error::cli_error; |
| 27 |
use crate::command_error::user_error; |
| 28 |
use crate::complete; |
| 29 |
use crate::ui::Ui; |
| 30 |
|
| 31 |
/// Abandon operation history |
| 32 |
/// |
| 33 |
/// To discard old operation history, use `jj op abandon ..<operation ID>`. It |
| 34 |
/// will abandon the specified operation and all its ancestors. The descendants |
| 35 |
/// will be reparented onto the root operation. |
| 36 |
/// |
| 37 |
/// To discard recent operations, use `jj op restore <operation ID>` followed |
| 38 |
/// by `jj op abandon <operation ID>..@-`. |
| 39 |
/// |
| 40 |
/// Previous versions of a change (or predecessors) are also discarded if they |
| 41 |
/// become unreachable from the operation history. The abandoned operations, |
| 42 |
/// commits, and other unreachable objects can later be garbage collected by |
| 43 |
/// using `jj util gc` command. |
| 44 |
#[derive(clap::Args, Clone, Debug)] |
| 45 |
pub struct OperationAbandonArgs { |
| 46 |
/// The operation or operation range to abandon |
| 47 |
#[arg(add = ArgValueCandidates::new(complete::operations))] |
| 48 |
operation: String, |
| 49 |
} |
| 50 |
|
| 51 |
pub async fn cmd_op_abandon( |
| 52 |
ui: &mut Ui, |
| 53 |
command: &CommandHelper, |
| 54 |
args: &OperationAbandonArgs, |
| 55 |
) -> Result<(), CommandError> { |
| 56 |
// Don't load the repo so that this command can be used to recover from |
| 57 |
// corrupted repo state. |
| 58 |
let mut workspace = command.load_workspace()?; |
| 59 |
let repo_loader = workspace.repo_loader(); |
| 60 |
let op_store = repo_loader.op_store(); |
| 61 |
let op_heads_store = repo_loader.op_heads_store(); |
| 62 |
// It doesn't make sense to create divergent operations that will be merged |
| 63 |
// with the current head. |
| 64 |
if command.global_args().at_operation.is_some() { |
| 65 |
return Err(cli_error("--at-op is not respected")); |
| 66 |
} |
| 67 |
let current_head_ops = op_walk::get_current_head_ops(op_store, op_heads_store.as_ref()).await?; |
| 68 |
let resolve_op = |op_str| op_walk::resolve_op_at(op_store, ¤t_head_ops, op_str); |
| 69 |
let (abandon_root_op, abandon_head_ops) = |
| 70 |
if let Some((root_op_str, head_op_str)) = args.operation.split_once("..") { |
| 71 |
let root_op = if root_op_str.is_empty() { |
| 72 |
repo_loader.root_operation().await |
| 73 |
} else { |
| 74 |
resolve_op(root_op_str).await? |
| 75 |
}; |
| 76 |
let head_ops = if head_op_str.is_empty() { |
| 77 |
current_head_ops.clone() |
| 78 |
} else { |
| 79 |
vec![resolve_op(head_op_str).await?] |
| 80 |
}; |
| 81 |
(root_op, head_ops) |
| 82 |
} else { |
| 83 |
let op = resolve_op(&args.operation).await?; |
| 84 |
let parent_ops = op.parents().await?; |
| 85 |
let parent_op = match parent_ops.len() { |
| 86 |
0 => return Err(user_error("Cannot abandon the root operation")), |
| 87 |
1 => parent_ops.into_iter().next().unwrap(), |
| 88 |
_ => return Err(user_error("Cannot abandon a merge operation")), |
| 89 |
}; |
| 90 |
(parent_op, vec![op]) |
| 91 |
}; |
| 92 |
|
| 93 |
if let Some(op) = abandon_head_ops |
| 94 |
.iter() |
| 95 |
.find(|op| current_head_ops.contains(op)) |
| 96 |
{ |
| 97 |
let mut err = user_error(format!( |
| 98 |
"Cannot abandon the current operation {}", |
| 99 |
short_operation_hash(op.id()) |
| 100 |
)); |
| 101 |
if current_head_ops.len() == 1 { |
| 102 |
err.add_hint("Run `jj undo` to revert the current operation, then use `jj op abandon`"); |
| 103 |
} |
| 104 |
return Err(err); |
| 105 |
} |
| 106 |
|
| 107 |
// Reparent descendants, count the number of abandoned operations. |
| 108 |
let stats = op_walk::reparent_range( |
| 109 |
op_store.as_ref(), |
| 110 |
&abandon_head_ops, |
| 111 |
¤t_head_ops, |
| 112 |
&abandon_root_op, |
| 113 |
) |
| 114 |
.await?; |
| 115 |
assert_eq!( |
| 116 |
current_head_ops.len(), |
| 117 |
stats.new_head_ids.len(), |
| 118 |
"all current_head_ops should be reparented as they aren't included in abandon_head_ops" |
| 119 |
); |
| 120 |
let reparented_head_ops = || iter::zip(¤t_head_ops, &stats.new_head_ids); |
| 121 |
if reparented_head_ops().all(|(old, new_id)| old.id() == new_id) { |
| 122 |
writeln!(ui.status(), "Nothing changed.")?; |
| 123 |
return Ok(()); |
| 124 |
} |
| 125 |
writeln!( |
| 126 |
ui.status(), |
| 127 |
"Abandoned {} operations and reparented {} descendant operations.", |
| 128 |
stats.unreachable_count, |
| 129 |
stats.rewritten_count, |
| 130 |
)?; |
| 131 |
for (old, new_id) in reparented_head_ops().filter(|&(old, new_id)| old.id() != new_id) { |
| 132 |
op_heads_store |
| 133 |
.update_op_heads(slice::from_ref(old.id()), new_id) |
| 134 |
.await?; |
| 135 |
} |
| 136 |
// Remap the operation id of the current workspace. If there were any |
| 137 |
// divergent operations, user will need to re-abandon their ancestors. |
| 138 |
if !command.global_args().ignore_working_copy { |
| 139 |
let mut locked_ws = workspace.start_working_copy_mutation().await?; |
| 140 |
let old_op_id = locked_ws.locked_wc().old_operation_id(); |
| 141 |
if let Some((_, new_id)) = reparented_head_ops().find(|(old, _)| old.id() == old_op_id) { |
| 142 |
locked_ws.finish(new_id.clone()).await?; |
| 143 |
} else { |
| 144 |
writeln!( |
| 145 |
ui.warning_default(), |
| 146 |
"The working copy operation {} is not updated because it differs from the repo {}.", |
| 147 |
short_operation_hash(old_op_id), |
| 148 |
current_head_ops |
| 149 |
.iter() |
| 150 |
.map(|op| short_operation_hash(op.id())) |
| 151 |
.join(", "), |
| 152 |
)?; |
| 153 |
} |
| 154 |
} |
| 155 |
Ok(()) |
| 156 |
} |