just playing with tangled
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
15use std::slice;
16
17use clap::ArgGroup;
18use clap_complete::ArgValueCandidates;
19use clap_complete::ArgValueCompleter;
20use tracing::instrument;
21
22use crate::cli_util::CommandHelper;
23use crate::cli_util::RevisionArg;
24use crate::command_error::CommandError;
25use crate::complete;
26use crate::diff_util::DiffFormatArgs;
27use crate::ui::Ui;
28
29/// Compare the changes of two commits
30///
31/// This excludes changes from other commits by temporarily rebasing `--from`
32/// onto `--to`'s parents. If you wish to compare the same change across
33/// versions, consider `jj evolog -p` instead.
34#[derive(clap::Args, Clone, Debug)]
35#[command(group(ArgGroup::new("to_diff").args(&["from", "to"]).multiple(true).required(true)))]
36#[command(mut_arg("ignore_all_space", |a| a.short('w')))]
37#[command(mut_arg("ignore_space_change", |a| a.short('b')))]
38pub(crate) struct InterdiffArgs {
39 /// Show changes from this revision
40 #[arg(
41 long,
42 short,
43 value_name = "REVSET",
44 add = ArgValueCandidates::new(complete::all_revisions)
45 )]
46 from: Option<RevisionArg>,
47 /// Show changes to this revision
48 #[arg(
49 long,
50 short,
51 value_name = "REVSET",
52 add = ArgValueCandidates::new(complete::all_revisions)
53 )]
54 to: Option<RevisionArg>,
55 /// Restrict the diff to these paths
56 #[arg(
57 value_name = "FILESETS",
58 value_hint = clap::ValueHint::AnyPath,
59 add = ArgValueCompleter::new(complete::interdiff_files),
60 )]
61 paths: Vec<String>,
62 #[command(flatten)]
63 format: DiffFormatArgs,
64}
65
66#[instrument(skip_all)]
67pub(crate) fn cmd_interdiff(
68 ui: &mut Ui,
69 command: &CommandHelper,
70 args: &InterdiffArgs,
71) -> Result<(), CommandError> {
72 let workspace_command = command.workspace_helper(ui)?;
73 let from =
74 workspace_command.resolve_single_rev(ui, args.from.as_ref().unwrap_or(&RevisionArg::AT))?;
75 let to =
76 workspace_command.resolve_single_rev(ui, args.to.as_ref().unwrap_or(&RevisionArg::AT))?;
77 let matcher = workspace_command
78 .parse_file_patterns(ui, &args.paths)?
79 .to_matcher();
80 let diff_renderer = workspace_command.diff_renderer_for(&args.format)?;
81 ui.request_pager();
82 diff_renderer.show_inter_diff(
83 ui,
84 ui.stdout_formatter().as_mut(),
85 slice::from_ref(&from),
86 &to,
87 matcher.as_ref(),
88 ui.term_width(),
89 )?;
90 Ok(())
91}