Debian Codegraph @

cli/src/commands/edit.rs

raw
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::ArgValueCompleter;
18 use jj_lib::object_id::ObjectId as _;
19 use tracing::instrument;
20
21 use crate::cli_util::CommandHelper;
22 use crate::cli_util::RevisionArg;
23 use crate::command_error::CommandError;
24 use crate::complete;
25 use crate::ui::Ui;
26
27 /// Sets the specified revision as the working-copy revision
28 ///
29 /// Note: it is [generally recommended] to instead use `jj new` and `jj
30 /// squash`.
31 ///
32 /// [generally recommended]:
33 /// https://docs.jj-vcs.dev/latest/FAQ#how-do-i-resume-working-on-an-existing-change
34 #[derive(clap::Args, Clone, Debug)]
35 #[command(group(clap::ArgGroup::new("revision").required(true)))]
36 pub(crate) struct EditArgs {
37 /// The commit to edit [aliases: -r]
38 #[arg(group = "revision", value_name = "REVSET")]
39 #[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))]
40 revision_pos: Option<RevisionArg>,
41
42 #[arg(short = 'r', group = "revision", hide = true, value_name = "REVSET")]
43 #[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))]
44 revision_opt: Option<RevisionArg>,
45 }
46
47 #[instrument(skip_all)]
48 pub(crate) async fn cmd_edit(
49 ui: &mut Ui,
50 command: &CommandHelper,
51 args: &EditArgs,
52 ) -> Result<(), CommandError> {
53 let mut workspace_command = command.workspace_helper(ui).await?;
54 let revision_arg = args
55 .revision_pos
56 .as_ref()
57 .or(args.revision_opt.as_ref())
58 .expect("either positional or -r arg should be provided");
59 let new_commit = workspace_command
60 .resolve_single_rev(ui, revision_arg)
61 .await?;
62 workspace_command
63 .check_rewritable([new_commit.id()])
64 .await?;
65 if workspace_command.get_wc_commit_id() == Some(new_commit.id()) {
66 writeln!(ui.status(), "Already editing that commit.")?;
67 } else {
68 let mut tx = workspace_command.start_transaction();
69 tx.edit(&new_commit)?;
70 tx.finish(ui, format!("edit commit {}", new_commit.id().hex()))
71 .await?;
72 }
73 Ok(())
74 }