| 1 |
// Copyright 2020 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 |
|
| 17 |
use clap_complete::ArgValueCandidates; |
| 18 |
use clap_complete::ArgValueCompleter; |
| 19 |
use indoc::formatdoc; |
| 20 |
use itertools::Itertools as _; |
| 21 |
use jj_lib::commit::conflict_label_for_commits; |
| 22 |
use jj_lib::merge::Diff; |
| 23 |
use jj_lib::object_id::ObjectId as _; |
| 24 |
use tracing::instrument; |
| 25 |
|
| 26 |
use crate::cli_util::CommandHelper; |
| 27 |
use crate::cli_util::RevisionArg; |
| 28 |
use crate::cli_util::print_unmatched_explicit_paths; |
| 29 |
use crate::command_error::CommandError; |
| 30 |
use crate::command_error::user_error; |
| 31 |
use crate::complete; |
| 32 |
use crate::ui::Ui; |
| 33 |
|
| 34 |
/// Restore paths from another revision |
| 35 |
/// |
| 36 |
/// That means that the paths get the same content in the destination (`--into`) |
| 37 |
/// as they had in the source (`--from`). This is typically used for undoing |
| 38 |
/// changes to some paths in the working copy (`jj restore <paths>`). |
| 39 |
/// |
| 40 |
/// If only one of `--from` or `--into` is specified, the other one defaults to |
| 41 |
/// the working copy. |
| 42 |
/// |
| 43 |
/// When neither `--from` nor `--into` is specified, the command restores into |
| 44 |
/// the working copy from its parent(s). `jj restore` without arguments is |
| 45 |
/// similar to `jj abandon`, except that it leaves an empty revision with its |
| 46 |
/// description and other metadata preserved. |
| 47 |
/// |
| 48 |
/// See `jj diffedit` if you'd like to restore portions of files rather than |
| 49 |
/// entire files. |
| 50 |
#[derive(clap::Args, Clone, Debug)] |
| 51 |
pub(crate) struct RestoreArgs { |
| 52 |
/// Restore only these paths (instead of all paths) |
| 53 |
#[arg(value_name = "FILESETS", value_hint = clap::ValueHint::AnyPath)] |
| 54 |
#[arg(add = ArgValueCompleter::new(complete::modified_changes_in_or_range_files))] |
| 55 |
paths: Vec<String>, |
| 56 |
|
| 57 |
/// Revision to restore from (source) |
| 58 |
#[arg(long, short, value_name = "REVSET")] |
| 59 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 60 |
from: Option<RevisionArg>, |
| 61 |
|
| 62 |
/// Revision to restore into (destination) |
| 63 |
#[arg(long, short = 't', visible_alias = "to", value_name = "REVSET")] |
| 64 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))] |
| 65 |
into: Option<RevisionArg>, |
| 66 |
|
| 67 |
/// Undo the changes in a revision as compared to the merge of its parents. |
| 68 |
/// |
| 69 |
/// This undoes the changes that can be seen with `jj diff -r REVSET`. If |
| 70 |
/// `REVSET` only has a single parent, this option is equivalent to `jj |
| 71 |
/// restore --into REVSET --from REVSET-`. |
| 72 |
/// |
| 73 |
/// The default behavior of `jj restore` is equivalent to `jj restore |
| 74 |
/// --changes-in @`. |
| 75 |
#[arg(long, short, value_name = "REVSET", conflicts_with_all = ["into", "from"])] |
| 76 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 77 |
changes_in: Option<RevisionArg>, |
| 78 |
|
| 79 |
/// Prints an error. DO NOT USE. |
| 80 |
/// |
| 81 |
/// If we followed the pattern of `jj diff` and `jj diffedit`, we would use |
| 82 |
/// `--revision` instead of `--changes-in` However, that would make it |
| 83 |
/// likely that someone unfamiliar with this pattern would use `-r` when |
| 84 |
/// they wanted `--from`. This would make a different revision empty, and |
| 85 |
/// the user might not even realize something went wrong. |
| 86 |
#[arg(long, short, hide = true)] |
| 87 |
revision: Option<RevisionArg>, |
| 88 |
|
| 89 |
/// Interactively choose which parts to restore |
| 90 |
#[arg(long, short)] |
| 91 |
interactive: bool, |
| 92 |
|
| 93 |
/// Specify diff editor to be used (implies --interactive) |
| 94 |
#[arg(long, value_name = "NAME")] |
| 95 |
#[arg(add = ArgValueCandidates::new(complete::diff_editors))] |
| 96 |
tool: Option<String>, |
| 97 |
|
| 98 |
/// Preserve the content (not the diff) when rebasing descendants |
| 99 |
#[arg(long)] |
| 100 |
restore_descendants: bool, |
| 101 |
} |
| 102 |
|
| 103 |
#[instrument(skip_all)] |
| 104 |
pub(crate) async fn cmd_restore( |
| 105 |
ui: &mut Ui, |
| 106 |
command: &CommandHelper, |
| 107 |
args: &RestoreArgs, |
| 108 |
) -> Result<(), CommandError> { |
| 109 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 110 |
let (from_commits, from_tree, to_commit); |
| 111 |
if args.revision.is_some() { |
| 112 |
return Err( |
| 113 |
user_error("`jj restore` does not have a `--revision`/`-r` option.") |
| 114 |
.hinted("To modify the current revision, use `--from`.") |
| 115 |
.hinted( |
| 116 |
"To undo changes in a revision compared to its parents, use `--changes-in`.", |
| 117 |
), |
| 118 |
); |
| 119 |
} |
| 120 |
if args.from.is_some() || args.into.is_some() { |
| 121 |
to_commit = workspace_command |
| 122 |
.resolve_single_rev(ui, args.into.as_ref().unwrap_or(&RevisionArg::AT)) |
| 123 |
.await?; |
| 124 |
let from_commit = workspace_command |
| 125 |
.resolve_single_rev(ui, args.from.as_ref().unwrap_or(&RevisionArg::AT)) |
| 126 |
.await?; |
| 127 |
from_tree = from_commit.tree(); |
| 128 |
from_commits = vec![from_commit]; |
| 129 |
} else { |
| 130 |
to_commit = workspace_command |
| 131 |
.resolve_single_rev(ui, args.changes_in.as_ref().unwrap_or(&RevisionArg::AT)) |
| 132 |
.await?; |
| 133 |
from_tree = to_commit |
| 134 |
.parent_tree(workspace_command.repo().as_ref()) |
| 135 |
.await?; |
| 136 |
from_commits = to_commit.parents().await?; |
| 137 |
} |
| 138 |
workspace_command.check_rewritable([to_commit.id()]).await?; |
| 139 |
|
| 140 |
let fileset_expression = workspace_command.parse_file_patterns(ui, &args.paths)?; |
| 141 |
let matcher = fileset_expression.to_matcher(); |
| 142 |
let diff_selector = |
| 143 |
workspace_command.diff_selector(ui, args.tool.as_deref(), args.interactive)?; |
| 144 |
let to_tree = to_commit.tree(); |
| 145 |
let format_instructions = || { |
| 146 |
formatdoc! {" |
| 147 |
You are restoring changes from: {from_commits} |
| 148 |
to commit: {to_commit} |
| 149 |
|
| 150 |
The diff initially shows all changes restored. Adjust the right side until it |
| 151 |
shows the contents you want for the destination commit. |
| 152 |
", |
| 153 |
from_commits = from_commits |
| 154 |
.iter() |
| 155 |
.map(|commit| workspace_command.format_commit_summary(commit)) |
| 156 |
// "You are restoring changes from: " |
| 157 |
.join("\n "), |
| 158 |
to_commit = workspace_command.format_commit_summary(&to_commit), |
| 159 |
} |
| 160 |
}; |
| 161 |
let new_tree = diff_selector |
| 162 |
.select( |
| 163 |
ui, |
| 164 |
Diff::new(&to_tree, &from_tree), |
| 165 |
Diff::new( |
| 166 |
to_commit.conflict_label(), |
| 167 |
conflict_label_for_commits(&from_commits), |
| 168 |
), |
| 169 |
&matcher, |
| 170 |
format_instructions, |
| 171 |
) |
| 172 |
.await?; |
| 173 |
|
| 174 |
print_unmatched_explicit_paths( |
| 175 |
ui, |
| 176 |
&workspace_command, |
| 177 |
&fileset_expression, |
| 178 |
[&to_tree, &from_tree], |
| 179 |
) |
| 180 |
.await?; |
| 181 |
|
| 182 |
if new_tree.tree_ids() == to_commit.tree_ids() { |
| 183 |
writeln!(ui.status(), "Nothing changed.")?; |
| 184 |
} else { |
| 185 |
let mut tx = workspace_command.start_transaction(); |
| 186 |
tx.repo_mut() |
| 187 |
.rewrite_commit(&to_commit) |
| 188 |
.set_tree(new_tree) |
| 189 |
.write() |
| 190 |
.await?; |
| 191 |
// rebase_descendants early; otherwise the new commit would always have |
| 192 |
// a conflicted change id at this point. |
| 193 |
let (num_rebased, extra_msg) = if args.restore_descendants { |
| 194 |
( |
| 195 |
tx.repo_mut().reparent_descendants().await?, |
| 196 |
" (while preserving their content)", |
| 197 |
) |
| 198 |
} else { |
| 199 |
(tx.repo_mut().rebase_descendants().await?, "") |
| 200 |
}; |
| 201 |
if let Some(mut formatter) = ui.status_formatter() |
| 202 |
&& num_rebased > 0 |
| 203 |
{ |
| 204 |
writeln!( |
| 205 |
formatter, |
| 206 |
"Rebased {num_rebased} descendant commits{extra_msg}." |
| 207 |
)?; |
| 208 |
} |
| 209 |
tx.finish(ui, format!("restore into commit {}", to_commit.id().hex())) |
| 210 |
.await?; |
| 211 |
} |
| 212 |
Ok(()) |
| 213 |
} |