| 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 |
|
| 15 |
use std::slice; |
| 16 |
|
| 17 |
use clap::ArgGroup; |
| 18 |
use clap_complete::ArgValueCompleter; |
| 19 |
use tracing::instrument; |
| 20 |
|
| 21 |
use crate::cli_util::CommandHelper; |
| 22 |
use crate::cli_util::RevisionArg; |
| 23 |
use crate::cli_util::print_unmatched_explicit_paths; |
| 24 |
use crate::command_error::CommandError; |
| 25 |
use crate::complete; |
| 26 |
use crate::diff_util::DiffFormatArgs; |
| 27 |
use crate::ui::Ui; |
| 28 |
|
| 29 |
/// Show differences between the diffs of two revisions |
| 30 |
/// |
| 31 |
/// This is like running `jj diff -r` on each change, then comparing those |
| 32 |
/// results. It answers: "How do the modifications introduced by revision A |
| 33 |
/// differ from the modifications introduced by revision B?" |
| 34 |
/// |
| 35 |
/// For example, if two changes both add a feature but implement it |
| 36 |
/// differently, `jj interdiff --from @- --to other` shows what one |
| 37 |
/// implementation adds or removes that the other doesn't. |
| 38 |
/// |
| 39 |
/// A common use of this command is to compare how a change has changed |
| 40 |
/// since the last push to a remote: |
| 41 |
/// |
| 42 |
/// ```sh |
| 43 |
/// $ jj interdiff --from push-xyz@origin --to push-xyz |
| 44 |
/// ``` |
| 45 |
/// |
| 46 |
/// This command is different from `jj diff --from A --to B`, which compares |
| 47 |
/// file contents directly. `interdiff` compares what the changes do in terms of |
| 48 |
/// their patches, rather than their file contents. This makes a difference when |
| 49 |
/// the two revisions have different parents: `jj diff --from A --to B` will |
| 50 |
/// include the changes between their parents while `jj interdiff --from A --to |
| 51 |
/// B` will not. |
| 52 |
/// |
| 53 |
/// Technically, this works by rebasing `--from` onto `--to`'s parents and |
| 54 |
/// comparing the result to `--to`. |
| 55 |
/// |
| 56 |
/// To see the changes throughout the whole evolution of a change instead of |
| 57 |
/// between just two revisions, use `jj evolog -p` instead. |
| 58 |
#[derive(clap::Args, Clone, Debug)] |
| 59 |
#[command(group(ArgGroup::new("to_diff").args(&["from", "to"]).multiple(true).required(true)))] |
| 60 |
#[command(mut_arg("ignore_all_space", |a| a.short('w')))] |
| 61 |
#[command(mut_arg("ignore_space_change", |a| a.short('b')))] |
| 62 |
pub(crate) struct InterdiffArgs { |
| 63 |
/// The first revision to compare (default: @) |
| 64 |
#[arg(long, short, value_name = "REVSET")] |
| 65 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 66 |
from: Option<RevisionArg>, |
| 67 |
|
| 68 |
/// The second revision to compare (default: @) |
| 69 |
#[arg(long, short, value_name = "REVSET")] |
| 70 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 71 |
to: Option<RevisionArg>, |
| 72 |
|
| 73 |
/// Restrict the diff to these paths |
| 74 |
#[arg(value_name = "FILESETS", value_hint = clap::ValueHint::AnyPath)] |
| 75 |
#[arg(add = ArgValueCompleter::new(complete::interdiff_files))] |
| 76 |
paths: Vec<String>, |
| 77 |
|
| 78 |
#[command(flatten)] |
| 79 |
format: DiffFormatArgs, |
| 80 |
} |
| 81 |
|
| 82 |
#[instrument(skip_all)] |
| 83 |
pub(crate) async fn cmd_interdiff( |
| 84 |
ui: &mut Ui, |
| 85 |
command: &CommandHelper, |
| 86 |
args: &InterdiffArgs, |
| 87 |
) -> Result<(), CommandError> { |
| 88 |
let workspace_command = command.workspace_helper(ui).await?; |
| 89 |
let from = workspace_command |
| 90 |
.resolve_single_rev(ui, args.from.as_ref().unwrap_or(&RevisionArg::AT)) |
| 91 |
.await?; |
| 92 |
let to = workspace_command |
| 93 |
.resolve_single_rev(ui, args.to.as_ref().unwrap_or(&RevisionArg::AT)) |
| 94 |
.await?; |
| 95 |
let repo = workspace_command.repo(); |
| 96 |
let fileset_expression = workspace_command.parse_file_patterns(ui, &args.paths)?; |
| 97 |
let matcher = fileset_expression.to_matcher(); |
| 98 |
|
| 99 |
print_unmatched_explicit_paths( |
| 100 |
ui, |
| 101 |
&workspace_command, |
| 102 |
&fileset_expression, |
| 103 |
// We check the parent commits to account for deleted files. |
| 104 |
[ |
| 105 |
&from.parent_tree(repo.as_ref()).await?, |
| 106 |
&from.tree(), |
| 107 |
&to.parent_tree(repo.as_ref()).await?, |
| 108 |
&to.tree(), |
| 109 |
], |
| 110 |
) |
| 111 |
.await?; |
| 112 |
|
| 113 |
let diff_renderer = workspace_command.diff_renderer_for(&args.format)?; |
| 114 |
ui.request_pager(); |
| 115 |
diff_renderer |
| 116 |
.show_inter_diff( |
| 117 |
ui, |
| 118 |
ui.stdout_formatter().as_mut(), |
| 119 |
slice::from_ref(&from), |
| 120 |
&to, |
| 121 |
matcher.as_ref(), |
| 122 |
ui.term_width(), |
| 123 |
) |
| 124 |
.await?; |
| 125 |
Ok(()) |
| 126 |
} |