mirror of
https://github.com/martinvonz/jj.git
synced 2024-12-29 16:07:55 +00:00
commands: move diff code to diff.rs
This commit is contained in:
parent
bc3443291e
commit
b5e4e67090
2 changed files with 77 additions and 63 deletions
74
cli/src/commands/diff.rs
Normal file
74
cli/src/commands/diff.rs
Normal file
|
@ -0,0 +1,74 @@
|
|||
// 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 jj_lib::rewrite::merge_commit_trees;
|
||||
use tracing::instrument;
|
||||
|
||||
use crate::cli_util::{CommandError, CommandHelper, RevisionArg};
|
||||
use crate::diff_util::{diff_formats_for, show_diff, DiffFormatArgs};
|
||||
use crate::ui::Ui;
|
||||
|
||||
#[derive(clap::Args, Clone, Debug)]
|
||||
pub(crate) struct DiffArgs {
|
||||
/// Show changes in this revision, compared to its parent(s)
|
||||
#[arg(long, short)]
|
||||
revision: Option<RevisionArg>,
|
||||
/// Show changes from this revision
|
||||
#[arg(long, conflicts_with = "revision")]
|
||||
from: Option<RevisionArg>,
|
||||
/// Show changes to this revision
|
||||
#[arg(long, conflicts_with = "revision")]
|
||||
to: Option<RevisionArg>,
|
||||
/// Restrict the diff to these paths
|
||||
#[arg(value_hint = clap::ValueHint::AnyPath)]
|
||||
paths: Vec<String>,
|
||||
#[command(flatten)]
|
||||
format: DiffFormatArgs,
|
||||
}
|
||||
|
||||
#[instrument(skip_all)]
|
||||
pub(crate) fn cmd_diff(
|
||||
ui: &mut Ui,
|
||||
command: &CommandHelper,
|
||||
args: &DiffArgs,
|
||||
) -> Result<(), CommandError> {
|
||||
let workspace_command = command.workspace_helper(ui)?;
|
||||
let from_tree;
|
||||
let to_tree;
|
||||
if args.from.is_some() || args.to.is_some() {
|
||||
let from = workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"), ui)?;
|
||||
from_tree = from.tree()?;
|
||||
let to = workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"), ui)?;
|
||||
to_tree = to.tree()?;
|
||||
} else {
|
||||
let commit =
|
||||
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"), ui)?;
|
||||
let parents = commit.parents();
|
||||
from_tree = merge_commit_trees(workspace_command.repo().as_ref(), &parents)?;
|
||||
to_tree = commit.tree()?
|
||||
}
|
||||
let matcher = workspace_command.matcher_from_values(&args.paths)?;
|
||||
let diff_formats = diff_formats_for(command.settings(), &args.format)?;
|
||||
ui.request_pager();
|
||||
show_diff(
|
||||
ui,
|
||||
ui.stdout_formatter().as_mut(),
|
||||
&workspace_command,
|
||||
&from_tree,
|
||||
&to_tree,
|
||||
matcher.as_ref(),
|
||||
&diff_formats,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
|
@ -24,6 +24,7 @@ mod commit;
|
|||
mod config;
|
||||
mod debug;
|
||||
mod describe;
|
||||
mod diff;
|
||||
mod git;
|
||||
mod operation;
|
||||
|
||||
|
@ -91,7 +92,7 @@ enum Commands {
|
|||
#[command(subcommand)]
|
||||
Debug(debug::DebugCommands),
|
||||
Describe(describe::DescribeArgs),
|
||||
Diff(DiffArgs),
|
||||
Diff(diff::DiffArgs),
|
||||
Diffedit(DiffeditArgs),
|
||||
Duplicate(DuplicateArgs),
|
||||
Edit(EditArgs),
|
||||
|
@ -183,35 +184,6 @@ struct FilesArgs {
|
|||
paths: Vec<String>,
|
||||
}
|
||||
|
||||
/// Show changes in a revision
|
||||
///
|
||||
/// With the `-r` option, which is the default, shows the changes compared to
|
||||
/// the parent revision. If there are several parent revisions (i.e., the given
|
||||
/// revision is a merge), then they will be merged and the changes from the
|
||||
/// result to the given revision will be shown.
|
||||
///
|
||||
/// With the `--from` and/or `--to` options, shows the difference from/to the
|
||||
/// given revisions. If either is left out, it defaults to the working-copy
|
||||
/// commit. For example, `jj diff --from main` shows the changes from "main"
|
||||
/// (perhaps a branch name) to the working-copy commit.
|
||||
#[derive(clap::Args, Clone, Debug)]
|
||||
struct DiffArgs {
|
||||
/// Show changes in this revision, compared to its parent(s)
|
||||
#[arg(long, short)]
|
||||
revision: Option<RevisionArg>,
|
||||
/// Show changes from this revision
|
||||
#[arg(long, conflicts_with = "revision")]
|
||||
from: Option<RevisionArg>,
|
||||
/// Show changes to this revision
|
||||
#[arg(long, conflicts_with = "revision")]
|
||||
to: Option<RevisionArg>,
|
||||
/// Restrict the diff to these paths
|
||||
#[arg(value_hint = clap::ValueHint::AnyPath)]
|
||||
paths: Vec<String>,
|
||||
#[command(flatten)]
|
||||
format: DiffFormatArgs,
|
||||
}
|
||||
|
||||
/// Show commit description and changes in a revision
|
||||
#[derive(clap::Args, Clone, Debug)]
|
||||
struct ShowArgs {
|
||||
|
@ -1136,38 +1108,6 @@ fn cmd_files(ui: &mut Ui, command: &CommandHelper, args: &FilesArgs) -> Result<(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument(skip_all)]
|
||||
fn cmd_diff(ui: &mut Ui, command: &CommandHelper, args: &DiffArgs) -> Result<(), CommandError> {
|
||||
let workspace_command = command.workspace_helper(ui)?;
|
||||
let from_tree;
|
||||
let to_tree;
|
||||
if args.from.is_some() || args.to.is_some() {
|
||||
let from = workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"), ui)?;
|
||||
from_tree = from.tree()?;
|
||||
let to = workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"), ui)?;
|
||||
to_tree = to.tree()?;
|
||||
} else {
|
||||
let commit =
|
||||
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"), ui)?;
|
||||
let parents = commit.parents();
|
||||
from_tree = merge_commit_trees(workspace_command.repo().as_ref(), &parents)?;
|
||||
to_tree = commit.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,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument(skip_all)]
|
||||
fn cmd_show(ui: &mut Ui, command: &CommandHelper, args: &ShowArgs) -> Result<(), CommandError> {
|
||||
let workspace_command = command.workspace_helper(ui)?;
|
||||
|
@ -3388,7 +3328,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co
|
|||
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),
|
||||
Commands::Diff(sub_args) => cmd_diff(ui, command_helper, sub_args),
|
||||
Commands::Diff(sub_args) => diff::cmd_diff(ui, command_helper, sub_args),
|
||||
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),
|
||||
|
|
Loading…
Reference in a new issue