Debian Codegraph @

cli/src/commands/show.rs

raw
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::StreamExt as _;
18 use futures::TryStreamExt as _;
19 use futures::stream;
20 use jj_lib::matchers::EverythingMatcher;
21 use tracing::instrument;
22
23 use crate::cli_util::CommandHelper;
24 use crate::cli_util::RevisionArg;
25 use crate::command_error::CommandError;
26 use crate::complete;
27 use crate::diff_util::DiffFormatArgs;
28 use crate::ui::Ui;
29
30 /// Show revision metadata and diff
31 #[derive(clap::Args, Clone, Debug)]
32 #[command(mut_arg("ignore_all_space", |a| a.short('w')))]
33 #[command(mut_arg("ignore_space_change", |a| a.short('b')))]
34 pub(crate) struct ShowArgs {
35 /// Show changes in these revisions, compared to their parent(s)
36 /// [default: @] [aliases: -r]
37 #[arg(value_name = "REVSETS")]
38 #[arg(add = ArgValueCompleter::new(complete::revset_expression_all))]
39 revisions_pos: Vec<RevisionArg>,
40
41 #[arg(short = 'r', hide = true, value_name = "REVSETS")]
42 #[arg(add = ArgValueCompleter::new(complete::revset_expression_all))]
43 revisions_opt: Vec<RevisionArg>,
44
45 /// Show revisions in the opposite order (older revisions first)
46 #[arg(long)]
47 reversed: bool,
48
49 /// Render each revision using the given template
50 ///
51 /// You can specify arbitrary template expressions using the
52 /// [built-in keywords]. See [`jj help -k templates`] for more information.
53 ///
54 /// [built-in keywords]:
55 /// https://docs.jj-vcs.dev/latest/templates/#commit-keywords
56 ///
57 /// [`jj help -k templates`]:
58 /// https://docs.jj-vcs.dev/latest/templates/
59 #[arg(long, short = 'T')]
60 #[arg(add = ArgValueCandidates::new(complete::template_aliases))]
61 template: Option<String>,
62
63 #[command(flatten)]
64 format: DiffFormatArgs,
65
66 /// Do not show the patch
67 #[arg(long, conflicts_with = "DiffFormatArgs")]
68 no_patch: bool,
69 }
70
71 #[instrument(skip_all)]
72 pub(crate) async fn cmd_show(
73 ui: &mut Ui,
74 command: &CommandHelper,
75 args: &ShowArgs,
76 ) -> Result<(), CommandError> {
77 let workspace_command = command.workspace_helper(ui).await?;
78
79 let target_expr = if args.revisions_pos.is_empty() && args.revisions_opt.is_empty() {
80 workspace_command.parse_revset(ui, &RevisionArg::AT)?
81 } else {
82 workspace_command
83 .parse_union_revsets(ui, &[&*args.revisions_pos, &*args.revisions_opt].concat())?
84 };
85 let template_string = match &args.template {
86 Some(value) => value.clone(),
87 None => workspace_command.settings().get_string("templates.show")?,
88 };
89 let template = workspace_command
90 .parse_commit_template(ui, &template_string)?
91 .labeled(["show", "commit"]);
92 let diff_renderer = workspace_command.diff_renderer_for(&args.format)?;
93
94 let mut commit_stream = target_expr.evaluate_to_commits()?;
95 if args.reversed {
96 let commits: Vec<_> = commit_stream.try_collect().await?;
97 commit_stream = stream::iter(commits.into_iter().rev().map(Ok)).boxed_local();
98 }
99
100 ui.request_pager();
101 let mut formatter = ui.stdout_formatter();
102 let formatter = formatter.as_mut();
103
104 while let Some(commit) = commit_stream.try_next().await? {
105 template.format(&commit, formatter)?;
106
107 if !args.no_patch {
108 diff_renderer
109 .show_patch(ui, formatter, &commit, &EverythingMatcher, ui.term_width())
110 .await?;
111 }
112 }
113 Ok(())
114 }