From e9d7a22fc03bd7fb3df400392dbe13b289d71036 Mon Sep 17 00:00:00 2001 From: Antoine Cezar Date: Sun, 29 Oct 2023 13:34:10 +0100 Subject: [PATCH] commands: move interdiff code to interdiff.rs --- cli/src/commands/interdiff.rs | 68 +++++++++++++++++++++++++++++++++++ cli/src/commands/mod.rs | 52 ++------------------------- 2 files changed, 71 insertions(+), 49 deletions(-) create mode 100644 cli/src/commands/interdiff.rs diff --git a/cli/src/commands/interdiff.rs b/cli/src/commands/interdiff.rs new file mode 100644 index 000000000..bb3e44a36 --- /dev/null +++ b/cli/src/commands/interdiff.rs @@ -0,0 +1,68 @@ +// 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 clap::ArgGroup; +use tracing::instrument; + +use super::rebase_to_dest_parent; +use crate::cli_util::{CommandError, CommandHelper, RevisionArg}; +use crate::diff_util::{self, DiffFormatArgs}; +use crate::ui::Ui; + +/// Compare the changes of two commits +/// +/// This excludes changes from other commits by temporarily rebasing `--from` +/// onto `--to`'s parents. If you wish to compare the same change across +/// versions, consider `jj obslog -p` instead. +#[derive(clap::Args, Clone, Debug)] +#[command(group(ArgGroup::new("to_diff").args(&["from", "to"]).multiple(true).required(true)))] +pub(crate) struct InterdiffArgs { + /// Show changes from this revision + #[arg(long)] + from: Option, + /// Show changes to this revision + #[arg(long)] + to: Option, + /// Restrict the diff to these paths + #[arg(value_hint = clap::ValueHint::AnyPath)] + paths: Vec, + #[command(flatten)] + format: DiffFormatArgs, +} + +#[instrument(skip_all)] +pub(crate) fn cmd_interdiff( + ui: &mut Ui, + command: &CommandHelper, + args: &InterdiffArgs, +) -> Result<(), CommandError> { + let workspace_command = command.workspace_helper(ui)?; + let from = workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"), ui)?; + let to = workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"), ui)?; + + let from_tree = rebase_to_dest_parent(&workspace_command, &from, &to)?; + let to_tree = to.tree()?; + let matcher = workspace_command.matcher_from_values(&args.paths)?; + let diff_formats = diff_util::diff_formats_for(command.settings(), &args.format)?; + ui.request_pager(); + diff_util::show_diff( + ui, + ui.stdout_formatter().as_mut(), + &workspace_command, + &from_tree, + &to_tree, + matcher.as_ref(), + &diff_formats, + ) +} diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 35440ce9b..bd5939347 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -31,6 +31,7 @@ mod edit; mod files; mod git; mod init; +mod interdiff; mod operation; use std::collections::{BTreeMap, HashSet}; @@ -104,7 +105,7 @@ enum Commands { #[command(subcommand)] Git(git::GitCommands), Init(init::InitArgs), - Interdiff(InterdiffArgs), + Interdiff(interdiff::InterdiffArgs), Log(LogArgs), /// Merge work from multiple branches /// @@ -247,27 +248,6 @@ struct ObslogArgs { diff_format: DiffFormatArgs, } -/// Compare the changes of two commits -/// -/// This excludes changes from other commits by temporarily rebasing `--from` -/// onto `--to`'s parents. If you wish to compare the same change across -/// versions, consider `jj obslog -p` instead. -#[derive(clap::Args, Clone, Debug)] -#[command(group(ArgGroup::new("to_diff").args(&["from", "to"]).multiple(true).required(true)))] -struct InterdiffArgs { - /// Show changes from this revision - #[arg(long)] - from: Option, - /// Show changes to this revision - #[arg(long)] - to: Option, - /// Restrict the diff to these paths - #[arg(value_hint = clap::ValueHint::AnyPath)] - paths: Vec, - #[command(flatten)] - format: DiffFormatArgs, -} - /// 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 @@ -1319,32 +1299,6 @@ fn show_predecessor_patch( ) } -#[instrument(skip_all)] -fn cmd_interdiff( - ui: &mut Ui, - command: &CommandHelper, - args: &InterdiffArgs, -) -> Result<(), CommandError> { - let workspace_command = command.workspace_helper(ui)?; - let from = workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"), ui)?; - let to = workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"), ui)?; - - let from_tree = rebase_to_dest_parent(&workspace_command, &from, &to)?; - let to_tree = to.tree()?; - let matcher = workspace_command.matcher_from_values(&args.paths)?; - let diff_formats = diff_util::diff_formats_for(command.settings(), &args.format)?; - ui.request_pager(); - diff_util::show_diff( - ui, - ui.stdout_formatter().as_mut(), - &workspace_command, - &from_tree, - &to_tree, - matcher.as_ref(), - &diff_formats, - ) -} - fn rebase_to_dest_parent( workspace_command: &WorkspaceCommandHelper, source: &Commit, @@ -3006,7 +2960,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co Commands::Show(sub_args) => cmd_show(ui, command_helper, sub_args), Commands::Status(sub_args) => cmd_status(ui, command_helper, sub_args), Commands::Log(sub_args) => cmd_log(ui, command_helper, sub_args), - Commands::Interdiff(sub_args) => cmd_interdiff(ui, command_helper, sub_args), + Commands::Interdiff(sub_args) => interdiff::cmd_interdiff(ui, command_helper, sub_args), Commands::Obslog(sub_args) => cmd_obslog(ui, command_helper, sub_args), Commands::Describe(sub_args) => describe::cmd_describe(ui, command_helper, sub_args), Commands::Commit(sub_args) => commit::cmd_commit(ui, command_helper, sub_args),