Debian Codegraph @

cli/src/commands/operation/show.rs

raw
1 // Copyright 2024 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 jj_lib::operation::Operation;
17
18 use super::diff::parse_op_diff_changes_in;
19 use super::diff::show_op_diff;
20 use crate::cli_util::CommandHelper;
21 use crate::cli_util::LogContentFormat;
22 use crate::cli_util::merge_operations;
23 use crate::command_error::CommandError;
24 use crate::complete;
25 use crate::diff_util::DiffFormatArgs;
26 use crate::diff_util::DiffRenderer;
27 use crate::diff_util::diff_formats_for_log;
28 use crate::graphlog::GraphStyle;
29 use crate::templater::TemplateRenderer;
30 use crate::ui::Ui;
31
32 /// Show changes to the repository in an operation
33 #[derive(clap::Args, Clone, Debug)]
34 pub struct OperationShowArgs {
35 /// Show repository changes in this operation, compared to its parent(s)
36 #[arg(default_value = "@")]
37 #[arg(add = ArgValueCandidates::new(complete::operations))]
38 operation: String,
39
40 /// Show a flat list of modified changes instead of a graph
41 #[arg(long, short = 'G')]
42 no_graph: bool,
43
44 /// Render the operation using the given template
45 ///
46 /// You can specify arbitrary template expressions using the
47 /// [built-in keywords]. See [`jj help -k templates`] for more
48 /// information.
49 ///
50 /// [built-in keywords]:
51 /// https://docs.jj-vcs.dev/latest/templates/#operation-keywords
52 ///
53 /// [`jj help -k templates`]:
54 /// https://docs.jj-vcs.dev/latest/templates/
55 #[arg(long, short = 'T')]
56 #[arg(add = ArgValueCandidates::new(complete::template_aliases))]
57 template: Option<String>,
58
59 /// Show patch of modifications to changes
60 ///
61 /// If the previous version has different parents, it will be temporarily
62 /// rebased to the parents of the new version, so the diff is not
63 /// contaminated by unrelated changes.
64 #[arg(long, short = 'p')]
65 patch: bool,
66
67 /// Do not show operation diff
68 #[arg(long, conflicts_with_all = ["patch", "DiffFormatArgs"])]
69 no_op_diff: bool,
70
71 #[command(flatten)]
72 diff_format: DiffFormatArgs,
73
74 /// Show only changed revisions matching the given revset expression
75 ///
76 /// If no revisions are specified, this defaults to the
77 /// `revsets.op-diff-changes-in` setting.
78 #[arg(long, value_name = "REVSETS")]
79 show_changes_in: Option<String>,
80 }
81
82 pub async fn cmd_op_show(
83 ui: &mut Ui,
84 command: &CommandHelper,
85 args: &OperationShowArgs,
86 ) -> Result<(), CommandError> {
87 let workspace_command = command.workspace_helper(ui).await?;
88 let workspace_env = workspace_command.env();
89 let repo_loader = workspace_command.workspace().repo_loader();
90 let settings = workspace_command.settings();
91 let op = workspace_command.resolve_single_op(&args.operation)?;
92 let parent_ops = op.parents().await?;
93 let workspace_name = None;
94 let transaction_description = None;
95 let command_args = [];
96 let merged_parent_op = merge_operations(
97 None,
98 repo_loader,
99 parent_ops.clone(),
100 workspace_name,
101 transaction_description,
102 &command_args,
103 )
104 .await?;
105 let parent_repo = repo_loader.load_at(&merged_parent_op).await?;
106 let repo = repo_loader.load_at(&op).await?;
107
108 let id_prefix_context = workspace_env.new_id_prefix_context();
109 let commit_summary_template = {
110 let language = workspace_env.commit_template_language(repo.as_ref(), &id_prefix_context);
111 let text = settings.get_string("templates.commit_summary")?;
112 workspace_env
113 .parse_template(ui, &language, &text)?
114 .labeled(["op_show", "commit"])
115 };
116
117 let graph_style = GraphStyle::from_settings(settings)?;
118 let with_content_format = LogContentFormat::new(ui, settings)?;
119 let diff_renderer = {
120 let formats = diff_formats_for_log(settings, &args.diff_format, args.patch)?;
121 let path_converter = workspace_env.path_converter();
122 let conflict_marker_style = workspace_env.conflict_marker_style();
123 (!formats.is_empty()).then(|| {
124 DiffRenderer::new(
125 repo.as_ref(),
126 path_converter,
127 conflict_marker_style,
128 formats,
129 )
130 })
131 };
132
133 let template: TemplateRenderer<Operation> = {
134 let text = match &args.template {
135 Some(value) => value.to_owned(),
136 None => settings.get_string("templates.op_show")?,
137 };
138 workspace_command
139 .parse_operation_template(ui, &text)?
140 .labeled(["op_show", "operation"])
141 };
142
143 let op_diff_changes_expr =
144 parse_op_diff_changes_in(ui, settings, workspace_env, args.show_changes_in.as_deref())?;
145
146 ui.request_pager();
147 let mut formatter = ui.stdout_formatter();
148 template.format(&op, formatter.as_mut())?;
149
150 if !args.no_op_diff {
151 // TODO: Merged repo may have newly rebased commits, which wouldn't exist in
152 // the index. (#4465)
153 if parent_ops.len() > 1 {
154 return Ok(());
155 }
156 show_op_diff(
157 ui,
158 workspace_env,
159 formatter.as_mut(),
160 repo.as_ref(),
161 &parent_repo,
162 &repo,
163 &commit_summary_template,
164 (!args.no_graph).then_some(graph_style),
165 &with_content_format,
166 diff_renderer.as_ref(),
167 op_diff_changes_expr,
168 )
169 .await?;
170 }
171 Ok(())
172 }