just playing with tangled
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
15use std::io::Write as _;
16
17use clap_complete::ArgValueCandidates;
18use jj_lib::object_id::ObjectId as _;
19use tracing::instrument;
20
21use crate::cli_util::CommandHelper;
22use crate::cli_util::RevisionArg;
23use crate::command_error::CommandError;
24use crate::complete;
25use 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://jj-vcs.github.io/jj/latest/FAQ#how-do-i-resume-working-on-an-existing-change
34#[derive(clap::Args, Clone, Debug)]
35pub(crate) struct EditArgs {
36 /// The commit to edit
37 #[arg(value_name = "REVSET", add = ArgValueCandidates::new(complete::mutable_revisions))]
38 revision: RevisionArg,
39 /// Ignored (but lets you pass `-r` for consistency with other commands)
40 #[arg(short = 'r', hide = true)]
41 unused_revision: bool,
42}
43
44#[instrument(skip_all)]
45pub(crate) fn cmd_edit(
46 ui: &mut Ui,
47 command: &CommandHelper,
48 args: &EditArgs,
49) -> Result<(), CommandError> {
50 let mut workspace_command = command.workspace_helper(ui)?;
51 let new_commit = workspace_command.resolve_single_rev(ui, &args.revision)?;
52 workspace_command.check_rewritable([new_commit.id()])?;
53 if workspace_command.get_wc_commit_id() == Some(new_commit.id()) {
54 writeln!(ui.status(), "Already editing that commit")?;
55 } else {
56 let mut tx = workspace_command.start_transaction();
57 tx.edit(&new_commit)?;
58 tx.finish(ui, format!("edit commit {}", new_commit.id().hex()))?;
59 }
60 Ok(())
61}