From 71ebc3bb8e6a5205861b34665d92a06b271aa67f Mon Sep 17 00:00:00 2001 From: Antoine Cezar Date: Fri, 27 Oct 2023 00:56:06 +0200 Subject: [PATCH] commands: move checkout code to checkout.rs --- cli/src/commands/checkout.rs | 63 ++++++++++++++++++++++++++++++++++++ cli/src/commands/mod.rs | 46 ++------------------------ 2 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 cli/src/commands/checkout.rs diff --git a/cli/src/commands/checkout.rs b/cli/src/commands/checkout.rs new file mode 100644 index 0000000000..f3938bf51f --- /dev/null +++ b/cli/src/commands/checkout.rs @@ -0,0 +1,63 @@ +// 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 tracing::instrument; + +use jj_lib::backend::ObjectId; + +use crate::{ + cli_util::{join_message_paragraphs, CommandError, CommandHelper, RevisionArg}, + ui::Ui, +}; + +/// Create a new, empty change and edit it in the working copy +/// +/// For more information, see +/// https://github.com/martinvonz/jj/blob/main/docs/working-copy.md. +#[derive(clap::Args, Clone, Debug)] +#[command(visible_aliases = &["co"])] +pub(crate) struct CheckoutArgs { + /// The revision to update to + revision: RevisionArg, + /// Ignored (but lets you pass `-r` for consistency with other commands) + #[arg(short = 'r', hide = true)] + unused_revision: bool, + /// The change description to use + #[arg(long = "message", short, value_name = "MESSAGE")] + message_paragraphs: Vec, +} + +#[instrument(skip_all)] +pub(crate) fn cmd_checkout( + ui: &mut Ui, + command: &CommandHelper, + args: &CheckoutArgs, +) -> Result<(), CommandError> { + let mut workspace_command = command.workspace_helper(ui)?; + let target = workspace_command.resolve_single_rev(&args.revision, ui)?; + let mut tx = + workspace_command.start_transaction(&format!("check out commit {}", target.id().hex())); + let commit_builder = tx + .mut_repo() + .new_commit( + command.settings(), + vec![target.id().clone()], + target.tree_id().clone(), + ) + .set_description(join_message_paragraphs(&args.message_paragraphs)); + let new_commit = commit_builder.write()?; + tx.edit(&new_commit).unwrap(); + tx.finish(ui)?; + Ok(()) +} diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 0afd3ac7f1..7c311a4230 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -18,6 +18,7 @@ mod backout; mod bench; mod branch; mod cat; +mod checkout; mod debug; mod git; mod operation; @@ -81,7 +82,7 @@ enum Commands { Branch(branch::BranchSubcommand), #[command(alias = "print")] Cat(cat::CatArgs), - Checkout(CheckoutArgs), + Checkout(checkout::CheckoutArgs), Chmod(ChmodArgs), Commit(CommitArgs), #[command(subcommand)] @@ -253,23 +254,6 @@ struct ConfigEditArgs { pub config_args: ConfigArgs, } -/// Create a new, empty change and edit it in the working copy -/// -/// For more information, see -/// https://github.com/martinvonz/jj/blob/main/docs/working-copy.md. -#[derive(clap::Args, Clone, Debug)] -#[command(visible_aliases = &["co"])] -struct CheckoutArgs { - /// The revision to update to - revision: RevisionArg, - /// Ignored (but lets you pass `-r` for consistency with other commands) - #[arg(short = 'r', hide = true)] - unused_revision: bool, - /// The change description to use - #[arg(long = "message", short, value_name = "MESSAGE")] - message_paragraphs: Vec, -} - /// Stop tracking specified paths in the working copy #[derive(clap::Args, Clone, Debug)] struct UntrackArgs { @@ -1350,30 +1334,6 @@ fn cmd_config_edit( run_ui_editor(command.settings(), &config_path) } -#[instrument(skip_all)] -fn cmd_checkout( - ui: &mut Ui, - command: &CommandHelper, - args: &CheckoutArgs, -) -> Result<(), CommandError> { - let mut workspace_command = command.workspace_helper(ui)?; - let target = workspace_command.resolve_single_rev(&args.revision, ui)?; - let mut tx = - workspace_command.start_transaction(&format!("check out commit {}", target.id().hex())); - let commit_builder = tx - .mut_repo() - .new_commit( - command.settings(), - vec![target.id().clone()], - target.tree_id().clone(), - ) - .set_description(cli_util::join_message_paragraphs(&args.message_paragraphs)); - let new_commit = commit_builder.write()?; - tx.edit(&new_commit).unwrap(); - tx.finish(ui)?; - Ok(()) -} - #[instrument(skip_all)] fn cmd_untrack( ui: &mut Ui, @@ -3886,7 +3846,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co Commands::Version(sub_args) => cmd_version(ui, command_helper, sub_args), Commands::Init(sub_args) => cmd_init(ui, command_helper, sub_args), Commands::Config(sub_args) => cmd_config(ui, command_helper, sub_args), - Commands::Checkout(sub_args) => cmd_checkout(ui, command_helper, sub_args), + Commands::Checkout(sub_args) => checkout::cmd_checkout(ui, command_helper, sub_args), Commands::Untrack(sub_args) => cmd_untrack(ui, command_helper, sub_args), Commands::Files(sub_args) => cmd_files(ui, command_helper, sub_args), Commands::Cat(sub_args) => cat::cmd_cat(ui, command_helper, sub_args),