| 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 clap_complete::ArgValueCandidates; |
| 16 |
use clap_complete::ArgValueCompleter; |
| 17 |
use futures::TryStreamExt as _; |
| 18 |
use futures::future::try_join_all; |
| 19 |
use indexmap::IndexSet; |
| 20 |
use itertools::Itertools as _; |
| 21 |
use jj_lib::copies::CopyRecords; |
| 22 |
use jj_lib::merge::Diff; |
| 23 |
use jj_lib::repo::Repo as _; |
| 24 |
use jj_lib::rewrite::merge_commit_trees; |
| 25 |
use tracing::instrument; |
| 26 |
|
| 27 |
use crate::cli_util::CommandHelper; |
| 28 |
use crate::cli_util::RevisionArg; |
| 29 |
use crate::cli_util::print_unmatched_explicit_paths; |
| 30 |
use crate::cli_util::short_commit_hash; |
| 31 |
use crate::command_error::CommandError; |
| 32 |
use crate::command_error::user_error; |
| 33 |
use crate::complete; |
| 34 |
use crate::diff_util::DiffFormatArgs; |
| 35 |
use crate::diff_util::get_copy_records; |
| 36 |
use crate::diff_util::show_templated; |
| 37 |
use crate::ui::Ui; |
| 38 |
|
| 39 |
/// Compare file contents between two revisions |
| 40 |
/// |
| 41 |
/// With the `-r` option, shows the changes compared to the parent revision. |
| 42 |
/// If there are several parent revisions (i.e., the given revision is a |
| 43 |
/// merge), then they will be merged and the changes from the result to the |
| 44 |
/// given revision will be shown. |
| 45 |
/// |
| 46 |
/// With the `--from` and/or `--to` options, shows the difference from/to the |
| 47 |
/// given revisions. If either is left out, it defaults to the working-copy |
| 48 |
/// commit. For example, `jj diff --from main` shows the changes from "main" |
| 49 |
/// (perhaps a bookmark name) to the working-copy commit. |
| 50 |
/// |
| 51 |
/// If no option is specified, it defaults to `-r @`. |
| 52 |
#[derive(clap::Args, Clone, Debug)] |
| 53 |
#[command(mut_arg("ignore_all_space", |a| a.short('w')))] |
| 54 |
#[command(mut_arg("ignore_space_change", |a| a.short('b')))] |
| 55 |
pub(crate) struct DiffArgs { |
| 56 |
/// Show changes in these revisions |
| 57 |
/// |
| 58 |
/// If there are multiple revisions, then the total diff for all of them |
| 59 |
/// will be shown. For example, if you have a linear chain of revisions |
| 60 |
/// A..D, then `jj diff -r B::D` equals `jj diff --from A --to D`. Multiple |
| 61 |
/// heads and/or roots are supported, but gaps in the revset are not |
| 62 |
/// supported (e.g. `jj diff -r 'A|C'` in a linear chain A..C). |
| 63 |
/// |
| 64 |
/// If a revision is a merge commit, this shows changes *from* the |
| 65 |
/// automatic merge of the contents of all of its parents *to* the contents |
| 66 |
/// of the revision itself. |
| 67 |
/// |
| 68 |
/// If none of `-r`, `-f`, or `-t` is provided, then the default is `-r @`. |
| 69 |
#[arg(long, short, value_name = "REVSETS", alias = "revision")] |
| 70 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 71 |
revisions: Option<Vec<RevisionArg>>, |
| 72 |
|
| 73 |
/// Show changes from this revision |
| 74 |
/// |
| 75 |
/// If none of `-r`, `-f`, or `-t` is provided, then the default is `-r @`. |
| 76 |
#[arg(long, short, conflicts_with = "revisions", value_name = "REVSET")] |
| 77 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 78 |
from: Option<RevisionArg>, |
| 79 |
|
| 80 |
/// Show changes to this revision |
| 81 |
/// |
| 82 |
/// If none of `-r`, `-f`, or `-t` is provided, then the default is `-r @`. |
| 83 |
#[arg(long, short, conflicts_with = "revisions", value_name = "REVSET")] |
| 84 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 85 |
to: Option<RevisionArg>, |
| 86 |
|
| 87 |
/// Restrict the diff to these paths |
| 88 |
#[arg(value_name = "FILESETS", value_hint = clap::ValueHint::AnyPath)] |
| 89 |
#[arg(add = ArgValueCompleter::new(complete::modified_revision_or_range_files))] |
| 90 |
paths: Vec<String>, |
| 91 |
|
| 92 |
/// Render each file diff entry using the given template |
| 93 |
/// |
| 94 |
/// All 0-argument methods of the [`TreeDiffEntry` type] are available as |
| 95 |
/// keywords in the template expression. See [`jj help -k templates`] for |
| 96 |
/// more information. |
| 97 |
/// |
| 98 |
/// [`TreeDiffEntry` type]: |
| 99 |
/// https://docs.jj-vcs.dev/latest/templates/#treediffentry-type |
| 100 |
/// |
| 101 |
/// [`jj help -k templates`]: |
| 102 |
/// https://docs.jj-vcs.dev/latest/templates/ |
| 103 |
#[arg( |
| 104 |
long, |
| 105 |
short = 'T', |
| 106 |
conflicts_with_all = ["short-format", "long-format", "tool"], |
| 107 |
help_heading = "Diff Formatting Options", |
| 108 |
)] |
| 109 |
#[arg(add = ArgValueCandidates::new(complete::template_aliases))] |
| 110 |
template: Option<String>, |
| 111 |
|
| 112 |
#[command(flatten)] |
| 113 |
format: DiffFormatArgs, |
| 114 |
} |
| 115 |
|
| 116 |
#[instrument(skip_all)] |
| 117 |
pub(crate) async fn cmd_diff( |
| 118 |
ui: &mut Ui, |
| 119 |
command: &CommandHelper, |
| 120 |
args: &DiffArgs, |
| 121 |
) -> Result<(), CommandError> { |
| 122 |
let workspace_command = command.workspace_helper(ui).await?; |
| 123 |
let repo = workspace_command.repo(); |
| 124 |
let fileset_expression = workspace_command.parse_file_patterns(ui, &args.paths)?; |
| 125 |
let matcher = fileset_expression.to_matcher(); |
| 126 |
|
| 127 |
let from_tree; |
| 128 |
let to_tree; |
| 129 |
let mut copy_records = CopyRecords::default(); |
| 130 |
if args.from.is_some() || args.to.is_some() { |
| 131 |
let resolve_revision = async |r: &Option<RevisionArg>| { |
| 132 |
workspace_command |
| 133 |
.resolve_single_rev(ui, r.as_ref().unwrap_or(&RevisionArg::AT)) |
| 134 |
.await |
| 135 |
}; |
| 136 |
let from = resolve_revision(&args.from).await?; |
| 137 |
let to = resolve_revision(&args.to).await?; |
| 138 |
from_tree = from.tree(); |
| 139 |
to_tree = to.tree(); |
| 140 |
|
| 141 |
let records = get_copy_records(repo.store(), from.id(), to.id(), &matcher).await?; |
| 142 |
copy_records.add_records(records); |
| 143 |
} else { |
| 144 |
let revision_args = args |
| 145 |
.revisions |
| 146 |
.as_deref() |
| 147 |
.unwrap_or(std::slice::from_ref(&RevisionArg::AT)); |
| 148 |
let revisions_evaluator = workspace_command.parse_union_revsets(ui, revision_args)?; |
| 149 |
let target_expression = revisions_evaluator.expression(); |
| 150 |
let mut gaps_revset = workspace_command |
| 151 |
.attach_revset_evaluator( |
| 152 |
target_expression |
| 153 |
.roots() |
| 154 |
.range(&target_expression.heads()) |
| 155 |
.minus(target_expression), |
| 156 |
) |
| 157 |
.evaluate_to_commit_ids()?; |
| 158 |
if let Some(commit_id) = gaps_revset.try_next().await? { |
| 159 |
return Err( |
| 160 |
user_error("Cannot diff revsets with gaps in.").hinted(format!( |
| 161 |
"Revision {} would need to be in the set.", |
| 162 |
short_commit_hash(&commit_id) |
| 163 |
)), |
| 164 |
); |
| 165 |
} |
| 166 |
let heads: Vec<_> = workspace_command |
| 167 |
.attach_revset_evaluator(target_expression.heads()) |
| 168 |
.evaluate_to_commits()? |
| 169 |
.try_collect() |
| 170 |
.await?; |
| 171 |
let roots: Vec<_> = workspace_command |
| 172 |
.attach_revset_evaluator(target_expression.roots()) |
| 173 |
.evaluate_to_commits()? |
| 174 |
.try_collect() |
| 175 |
.await?; |
| 176 |
|
| 177 |
// Collect parents outside of revset to preserve parent order |
| 178 |
let parents: IndexSet<_> = try_join_all(roots.iter().map(|c| c.parents())) |
| 179 |
.await? |
| 180 |
.into_iter() |
| 181 |
.flatten() |
| 182 |
.collect(); |
| 183 |
let parents = parents.into_iter().collect_vec(); |
| 184 |
from_tree = merge_commit_trees(repo.as_ref(), &parents).await?; |
| 185 |
to_tree = merge_commit_trees(repo.as_ref(), &heads).await?; |
| 186 |
|
| 187 |
for p in &parents { |
| 188 |
for to in &heads { |
| 189 |
let records = get_copy_records(repo.store(), p.id(), to.id(), &matcher).await?; |
| 190 |
copy_records.add_records(records); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// -T disables both short/long rendering formats, but it might be okay to |
| 196 |
// enable long format if explicitly specified (assuming -T is for short or |
| 197 |
// summary output.) |
| 198 |
let maybe_template; |
| 199 |
let diff_renderer; |
| 200 |
if let Some(text) = &args.template { |
| 201 |
let language = workspace_command.commit_template_language(); |
| 202 |
let template = workspace_command |
| 203 |
.parse_template(ui, &language, text)? |
| 204 |
.labeled(["diff"]); |
| 205 |
maybe_template = Some(template); |
| 206 |
diff_renderer = workspace_command.diff_renderer(vec![]); |
| 207 |
} else { |
| 208 |
maybe_template = None; |
| 209 |
diff_renderer = workspace_command.diff_renderer_for(&args.format)?; |
| 210 |
} |
| 211 |
|
| 212 |
ui.request_pager(); |
| 213 |
if let Some(template) = &maybe_template { |
| 214 |
let tree_diff = from_tree.diff_stream_with_copies(&to_tree, &matcher, ©_records); |
| 215 |
show_templated(ui.stdout_formatter().as_mut(), tree_diff, template).await?; |
| 216 |
} |
| 217 |
diff_renderer |
| 218 |
.show_diff( |
| 219 |
ui, |
| 220 |
ui.stdout_formatter().as_mut(), |
| 221 |
Diff::new(&from_tree, &to_tree), |
| 222 |
&matcher, |
| 223 |
©_records, |
| 224 |
ui.term_width(), |
| 225 |
) |
| 226 |
.await?; |
| 227 |
print_unmatched_explicit_paths( |
| 228 |
ui, |
| 229 |
&workspace_command, |
| 230 |
&fileset_expression, |
| 231 |
[&from_tree, &to_tree], |
| 232 |
) |
| 233 |
.await?; |
| 234 |
Ok(()) |
| 235 |
} |