Debian Codegraph @

cli/src/commands/operation/restore.rs

raw
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 clap_complete::ArgValueCandidates;
16 use jj_lib::object_id::ObjectId as _;
17
18 use super::DEFAULT_REVERT_WHAT;
19 use super::RevertWhatToRestore;
20 use super::view_with_desired_portions_restored;
21 use crate::cli_util::CommandHelper;
22 use crate::command_error::CommandError;
23 use crate::complete;
24 use crate::ui::Ui;
25
26 /// Create a new operation that restores the repo to an earlier state
27 ///
28 /// This restores the repo to the state at the specified operation, effectively
29 /// undoing all later operations. It does so by creating a new operation.
30 #[derive(clap::Args, Clone, Debug)]
31 pub struct OperationRestoreArgs {
32 /// The operation to restore to
33 ///
34 /// Use `jj op log` to find an operation to restore to. Use e.g. `jj
35 /// --at-op=<operation ID> log` before restoring to an operation to see the
36 /// state of the repo at that operation.
37 #[arg(add = ArgValueCandidates::new(complete::operations))]
38 operation: String,
39
40 /// What portions of the local state to restore (can be repeated)
41 ///
42 /// This option is EXPERIMENTAL.
43 #[arg(long, value_enum, default_values_t = DEFAULT_REVERT_WHAT)]
44 what: Vec<RevertWhatToRestore>,
45 }
46
47 pub async fn cmd_op_restore(
48 ui: &mut Ui,
49 command: &CommandHelper,
50 args: &OperationRestoreArgs,
51 ) -> Result<(), CommandError> {
52 let mut workspace_command = command.workspace_helper(ui).await?;
53 let target_op = workspace_command.resolve_single_op(&args.operation)?;
54 let mut tx = workspace_command.start_transaction();
55 let new_view = view_with_desired_portions_restored(
56 target_op.view().await?.store_view(),
57 tx.base_repo().view().store_view(),
58 &args.what,
59 );
60 tx.repo_mut().set_view(new_view);
61 if let Some(mut formatter) = ui.status_formatter() {
62 write!(formatter, "Restored to operation: ")?;
63 let template = tx.base_workspace_helper().operation_summary_template();
64 template.format(&target_op, formatter.as_mut())?;
65 writeln!(formatter)?;
66 }
67 tx.finish(ui, format!("restore to operation {}", target_op.id().hex()))
68 .await?;
69
70 Ok(())
71 }