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