From 4257341dbeb865f394973cae47dcc203ca5c7fe1 Mon Sep 17 00:00:00 2001 From: Antoine Cezar Date: Sat, 28 Oct 2023 16:20:13 +0200 Subject: [PATCH] commands: move edit code to edit.rs --- cli/src/commands/edit.rs | 54 ++++++++++++++++++++++++++++++++++++++++ cli/src/commands/mod.rs | 34 +++---------------------- 2 files changed, 57 insertions(+), 31 deletions(-) create mode 100644 cli/src/commands/edit.rs diff --git a/cli/src/commands/edit.rs b/cli/src/commands/edit.rs new file mode 100644 index 000000000..2b82388fa --- /dev/null +++ b/cli/src/commands/edit.rs @@ -0,0 +1,54 @@ +// Copyright 2020 The Jujutsu Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::io::Write; + +use jj_lib::backend::ObjectId; +use tracing::instrument; + +use crate::cli_util::{CommandError, CommandHelper, RevisionArg}; +use crate::ui::Ui; + +/// Edit a commit in the working copy +/// +/// Puts the contents of a commit in the working copy for editing. Any changes +/// you make in the working copy will update (amend) the commit. +#[derive(clap::Args, Clone, Debug)] +pub(crate) struct EditArgs { + /// The commit to edit + revision: RevisionArg, + /// Ignored (but lets you pass `-r` for consistency with other commands) + #[arg(short = 'r', hide = true)] + unused_revision: bool, +} + +#[instrument(skip_all)] +pub(crate) fn cmd_edit( + ui: &mut Ui, + command: &CommandHelper, + args: &EditArgs, +) -> Result<(), CommandError> { + let mut workspace_command = command.workspace_helper(ui)?; + let new_commit = workspace_command.resolve_single_rev(&args.revision, ui)?; + workspace_command.check_rewritable([&new_commit])?; + if workspace_command.get_wc_commit_id() == Some(new_commit.id()) { + writeln!(ui.stderr(), "Already editing that commit")?; + } else { + let mut tx = + workspace_command.start_transaction(&format!("edit commit {}", new_commit.id().hex())); + tx.edit(&new_commit)?; + tx.finish(ui)?; + } + Ok(()) +} diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index f17218c7e..fb67f88d1 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -27,6 +27,7 @@ mod describe; mod diff; mod diffedit; mod duplicate; +mod edit; mod git; mod operation; @@ -97,7 +98,7 @@ enum Commands { Diff(diff::DiffArgs), Diffedit(diffedit::DiffeditArgs), Duplicate(duplicate::DuplicateArgs), - Edit(EditArgs), + Edit(edit::EditArgs), Files(FilesArgs), #[command(subcommand)] Git(git::GitCommands), @@ -295,19 +296,6 @@ struct InterdiffArgs { format: DiffFormatArgs, } -/// Edit a commit in the working copy -/// -/// Puts the contents of a commit in the working copy for editing. Any changes -/// you make in the working copy will update (amend) the commit. -#[derive(clap::Args, Clone, Debug)] -struct EditArgs { - /// The commit to edit - revision: RevisionArg, - /// Ignored (but lets you pass `-r` for consistency with other commands) - #[arg(short = 'r', hide = true)] - unused_revision: bool, -} - /// Create a new, empty change and edit it in the working copy /// /// Note that you can create a merge commit by specifying multiple revisions as @@ -1614,22 +1602,6 @@ fn edit_sparse( .try_collect() } -#[instrument(skip_all)] -fn cmd_edit(ui: &mut Ui, command: &CommandHelper, args: &EditArgs) -> Result<(), CommandError> { - let mut workspace_command = command.workspace_helper(ui)?; - let new_commit = workspace_command.resolve_single_rev(&args.revision, ui)?; - workspace_command.check_rewritable([&new_commit])?; - if workspace_command.get_wc_commit_id() == Some(new_commit.id()) { - writeln!(ui.stderr(), "Already editing that commit")?; - } else { - let mut tx = - workspace_command.start_transaction(&format!("edit commit {}", new_commit.id().hex())); - tx.edit(&new_commit)?; - tx.finish(ui)?; - } - Ok(()) -} - /// Resolves revsets into revisions to rebase onto. These revisions don't have /// to be rewriteable. fn resolve_destination_revs( @@ -3171,7 +3143,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co Commands::Commit(sub_args) => commit::cmd_commit(ui, command_helper, sub_args), Commands::Duplicate(sub_args) => duplicate::cmd_duplicate(ui, command_helper, sub_args), Commands::Abandon(sub_args) => abandon::cmd_abandon(ui, command_helper, sub_args), - Commands::Edit(sub_args) => cmd_edit(ui, command_helper, sub_args), + Commands::Edit(sub_args) => edit::cmd_edit(ui, command_helper, sub_args), Commands::Next(sub_args) => cmd_next(ui, command_helper, sub_args), Commands::Prev(sub_args) => cmd_prev(ui, command_helper, sub_args), Commands::New(sub_args) => cmd_new(ui, command_helper, sub_args),