| 1 |
// Copyright 2020-2023 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 std::slice; |
| 16 |
|
| 17 |
use clap_complete::ArgValueCandidates; |
| 18 |
use futures::StreamExt as _; |
| 19 |
use futures::TryStreamExt as _; |
| 20 |
use futures::stream; |
| 21 |
use futures::stream::LocalBoxStream; |
| 22 |
use jj_lib::graph::GraphEdge; |
| 23 |
use jj_lib::graph::reverse_graph; |
| 24 |
use jj_lib::op_walk; |
| 25 |
use jj_lib::operation::Operation; |
| 26 |
use jj_lib::repo::RepoLoader; |
| 27 |
|
| 28 |
use super::diff::parse_op_diff_changes_in; |
| 29 |
use super::diff::show_op_diff; |
| 30 |
use crate::cli_util::CommandHelper; |
| 31 |
use crate::cli_util::LogContentFormat; |
| 32 |
use crate::cli_util::WorkspaceCommandEnvironment; |
| 33 |
use crate::cli_util::format_template; |
| 34 |
use crate::cli_util::merge_operations; |
| 35 |
use crate::command_error::CommandError; |
| 36 |
use crate::complete; |
| 37 |
use crate::diff_util::DiffFormatArgs; |
| 38 |
use crate::diff_util::DiffRenderer; |
| 39 |
use crate::diff_util::diff_formats_for_log; |
| 40 |
use crate::formatter::Formatter; |
| 41 |
use crate::graphlog::GraphStyle; |
| 42 |
use crate::graphlog::get_graphlog; |
| 43 |
use crate::operation_templater::OperationTemplateLanguage; |
| 44 |
use crate::templater::TemplateRenderer; |
| 45 |
use crate::ui::Ui; |
| 46 |
|
| 47 |
/// Show the operation log |
| 48 |
/// |
| 49 |
/// Like other commands, `jj op log` snapshots the current working-copy changes |
| 50 |
/// and reconciles divergent operations. Use `--at-op=@ --ignore-working-copy` |
| 51 |
/// to inspect the current state without mutation. |
| 52 |
#[derive(clap::Args, Clone, Debug)] |
| 53 |
pub struct OperationLogArgs { |
| 54 |
/// Limit number of operations to show |
| 55 |
/// |
| 56 |
/// Applied after operations are reordered topologically, but before being |
| 57 |
/// reversed. |
| 58 |
#[arg(long, short = 'n')] |
| 59 |
limit: Option<usize>, |
| 60 |
|
| 61 |
/// Show operations in the opposite order (older operations first) |
| 62 |
#[arg(long)] |
| 63 |
reversed: bool, |
| 64 |
|
| 65 |
/// Show a flat list of operations instead of a graph |
| 66 |
#[arg(long, short = 'G')] |
| 67 |
no_graph: bool, |
| 68 |
|
| 69 |
/// Render each operation using the given template |
| 70 |
/// |
| 71 |
/// You can specify arbitrary template expressions using the |
| 72 |
/// [built-in keywords]. See [`jj help -k templates`] for more |
| 73 |
/// information. |
| 74 |
/// |
| 75 |
/// [built-in keywords]: |
| 76 |
/// https://docs.jj-vcs.dev/latest/templates/#operation-keywords |
| 77 |
/// |
| 78 |
/// [`jj help -k templates`]: |
| 79 |
/// https://docs.jj-vcs.dev/latest/templates/ |
| 80 |
#[arg(long, short = 'T')] |
| 81 |
#[arg(add = ArgValueCandidates::new(complete::template_aliases))] |
| 82 |
template: Option<String>, |
| 83 |
|
| 84 |
/// Show changes to the repository at each operation |
| 85 |
#[arg(long, short = 'd')] |
| 86 |
op_diff: bool, |
| 87 |
|
| 88 |
/// Show patch of modifications to changes (implies --op-diff) |
| 89 |
/// |
| 90 |
/// If the previous version has different parents, it will be temporarily |
| 91 |
/// rebased to the parents of the new version, so the diff is not |
| 92 |
/// contaminated by unrelated changes. |
| 93 |
#[arg(long, short = 'p')] |
| 94 |
patch: bool, |
| 95 |
|
| 96 |
#[command(flatten)] |
| 97 |
diff_format: DiffFormatArgs, |
| 98 |
|
| 99 |
/// Show only changed revisions matching the given revset expression |
| 100 |
/// |
| 101 |
/// If no revisions are specified, this defaults to the |
| 102 |
/// `revsets.op-diff-changes-in` setting. |
| 103 |
#[arg(long, value_name = "REVSETS")] |
| 104 |
show_changes_in: Option<String>, |
| 105 |
} |
| 106 |
|
| 107 |
pub async fn cmd_op_log( |
| 108 |
ui: &mut Ui, |
| 109 |
command: &CommandHelper, |
| 110 |
args: &OperationLogArgs, |
| 111 |
) -> Result<(), CommandError> { |
| 112 |
if command.is_working_copy_writable() { |
| 113 |
let workspace_command = command.workspace_helper(ui).await?; |
| 114 |
let current_op = workspace_command.repo().operation(); |
| 115 |
let repo_loader = workspace_command.workspace().repo_loader(); |
| 116 |
do_op_log(ui, workspace_command.env(), repo_loader, current_op, args).await |
| 117 |
} else { |
| 118 |
// Don't load the repo so that the operation history can be inspected |
| 119 |
// even with a corrupted repo state. For example, you can find the first |
| 120 |
// bad operation id to be abandoned. |
| 121 |
let workspace = command.load_workspace()?; |
| 122 |
let workspace_env = command.workspace_environment(ui, &workspace)?; |
| 123 |
let repo_loader = workspace.repo_loader(); |
| 124 |
let current_op = |
| 125 |
command.resolve_operation(ui, workspace.repo_loader(), workspace.workspace_name())?; |
| 126 |
do_op_log(ui, &workspace_env, repo_loader, ¤t_op, args).await |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
async fn do_op_log( |
| 131 |
ui: &mut Ui, |
| 132 |
workspace_env: &WorkspaceCommandEnvironment, |
| 133 |
repo_loader: &RepoLoader, |
| 134 |
current_op: &Operation, |
| 135 |
args: &OperationLogArgs, |
| 136 |
) -> Result<(), CommandError> { |
| 137 |
let settings = repo_loader.settings(); |
| 138 |
let graph_style = GraphStyle::from_settings(settings)?; |
| 139 |
let with_content_format = LogContentFormat::new(ui, settings)?; |
| 140 |
|
| 141 |
let template: TemplateRenderer<Operation>; |
| 142 |
let op_node_template: TemplateRenderer<Operation>; |
| 143 |
{ |
| 144 |
let language = OperationTemplateLanguage::new( |
| 145 |
repo_loader, |
| 146 |
Some(current_op.id()), |
| 147 |
workspace_env.cwd(), |
| 148 |
workspace_env.operation_template_extensions(), |
| 149 |
); |
| 150 |
let text = match &args.template { |
| 151 |
Some(value) => value.to_owned(), |
| 152 |
None => settings.get_string("templates.op_log")?, |
| 153 |
}; |
| 154 |
template = workspace_env |
| 155 |
.parse_template(ui, &language, &text)? |
| 156 |
.labeled(["op_log", "operation"]); |
| 157 |
op_node_template = workspace_env |
| 158 |
.parse_template( |
| 159 |
ui, |
| 160 |
&language, |
| 161 |
&settings.get_string("templates.op_log_node")?, |
| 162 |
)? |
| 163 |
.labeled(["op_log", "operation", "node"]); |
| 164 |
} |
| 165 |
|
| 166 |
let diff_formats = diff_formats_for_log(settings, &args.diff_format, args.patch)?; |
| 167 |
let maybe_show_op_diff = if args.op_diff || !diff_formats.is_empty() { |
| 168 |
let template_text = settings.get_string("templates.commit_summary")?; |
| 169 |
let op_diff_changes_expr = |
| 170 |
parse_op_diff_changes_in(ui, settings, workspace_env, args.show_changes_in.as_deref())?; |
| 171 |
let show = async move |ui: &Ui, |
| 172 |
formatter: &mut dyn Formatter, |
| 173 |
op: &Operation, |
| 174 |
with_content_format: &LogContentFormat| { |
| 175 |
let parent_ops = op.parents().await?; |
| 176 |
let workspace_name = None; |
| 177 |
let transaction_description = None; |
| 178 |
let command_args = []; |
| 179 |
let merged_parent_op = merge_operations( |
| 180 |
None, |
| 181 |
repo_loader, |
| 182 |
parent_ops.clone(), |
| 183 |
workspace_name, |
| 184 |
transaction_description, |
| 185 |
&command_args, |
| 186 |
) |
| 187 |
.await?; |
| 188 |
let parent_repo = repo_loader.load_at(&merged_parent_op).await?; |
| 189 |
let repo = repo_loader.load_at(op).await?; |
| 190 |
|
| 191 |
let id_prefix_context = workspace_env.new_id_prefix_context(); |
| 192 |
let commit_summary_template = { |
| 193 |
let language = |
| 194 |
workspace_env.commit_template_language(repo.as_ref(), &id_prefix_context); |
| 195 |
workspace_env |
| 196 |
.parse_template(ui, &language, &template_text)? |
| 197 |
.labeled(["op_log", "commit"]) |
| 198 |
}; |
| 199 |
let path_converter = workspace_env.path_converter(); |
| 200 |
let conflict_marker_style = workspace_env.conflict_marker_style(); |
| 201 |
let diff_renderer = (!diff_formats.is_empty()).then(|| { |
| 202 |
DiffRenderer::new( |
| 203 |
repo.as_ref(), |
| 204 |
path_converter, |
| 205 |
conflict_marker_style, |
| 206 |
diff_formats.clone(), |
| 207 |
) |
| 208 |
}); |
| 209 |
|
| 210 |
// TODO: Merged repo may have newly rebased commits, which wouldn't |
| 211 |
// exist in the index. (#4465) |
| 212 |
if parent_ops.len() > 1 { |
| 213 |
return Ok(()); |
| 214 |
} |
| 215 |
show_op_diff( |
| 216 |
ui, |
| 217 |
workspace_env, |
| 218 |
formatter, |
| 219 |
repo.as_ref(), |
| 220 |
&parent_repo, |
| 221 |
&repo, |
| 222 |
&commit_summary_template, |
| 223 |
(!args.no_graph).then_some(graph_style), |
| 224 |
with_content_format, |
| 225 |
diff_renderer.as_ref(), |
| 226 |
op_diff_changes_expr.clone(), |
| 227 |
) |
| 228 |
.await |
| 229 |
}; |
| 230 |
Some(show) |
| 231 |
} else { |
| 232 |
None |
| 233 |
}; |
| 234 |
|
| 235 |
ui.request_pager(); |
| 236 |
let mut formatter = ui.stdout_formatter(); |
| 237 |
let formatter = formatter.as_mut(); |
| 238 |
let stream = |
| 239 |
op_walk::walk_ancestors(slice::from_ref(current_op)).take(args.limit.unwrap_or(usize::MAX)); |
| 240 |
|
| 241 |
if !args.no_graph { |
| 242 |
let mut raw_output = formatter.raw()?; |
| 243 |
let mut graph = get_graphlog(graph_style, raw_output.as_mut()); |
| 244 |
let stream = stream.map_ok(|op| { |
| 245 |
let ids = op.parent_ids(); |
| 246 |
let edges = ids.iter().cloned().map(GraphEdge::direct).collect(); |
| 247 |
(op, edges) |
| 248 |
}); |
| 249 |
let mut stream_nodes: LocalBoxStream<'_, _> = if args.reversed { |
| 250 |
stream::iter( |
| 251 |
reverse_graph(stream.collect::<Vec<_>>().await.into_iter(), Operation::id)? |
| 252 |
.into_iter() |
| 253 |
.map(Ok), |
| 254 |
) |
| 255 |
.boxed() |
| 256 |
} else { |
| 257 |
stream.boxed_local() |
| 258 |
}; |
| 259 |
while let Some(node) = stream_nodes.next().await { |
| 260 |
let (op, edges) = node?; |
| 261 |
let mut buffer = vec![]; |
| 262 |
let within_graph = with_content_format.sub_width(graph.width(op.id(), &edges)); |
| 263 |
within_graph |
| 264 |
.write(ui.new_formatter(&mut buffer).as_mut(), async |formatter| { |
| 265 |
template.format(&op, formatter) |
| 266 |
}) |
| 267 |
.await?; |
| 268 |
if let Some(show) = &maybe_show_op_diff { |
| 269 |
let mut formatter = ui.new_formatter(&mut buffer); |
| 270 |
show(ui, formatter.as_mut(), &op, &within_graph).await?; |
| 271 |
} |
| 272 |
let node_symbol = format_template(ui, &op, &op_node_template); |
| 273 |
graph.add_node( |
| 274 |
op.id(), |
| 275 |
&edges, |
| 276 |
&node_symbol, |
| 277 |
&String::from_utf8_lossy(&buffer), |
| 278 |
)?; |
| 279 |
} |
| 280 |
} else { |
| 281 |
let mut stream: LocalBoxStream<'_, _> = if args.reversed { |
| 282 |
stream::iter(stream.collect::<Vec<_>>().await.into_iter().rev()).boxed() |
| 283 |
} else { |
| 284 |
stream.boxed_local() |
| 285 |
}; |
| 286 |
while let Some(op) = stream.try_next().await? { |
| 287 |
with_content_format |
| 288 |
.write(formatter, async |formatter| template.format(&op, formatter)) |
| 289 |
.await?; |
| 290 |
if let Some(show) = &maybe_show_op_diff { |
| 291 |
show(ui, formatter, &op, &with_content_format).await?; |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
Ok(()) |
| 297 |
} |