Skip to content

Commit

Permalink
op-diff: Use op_summary template
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Sep 15, 2024
1 parent 8e727de commit 7ff12ec
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 132 deletions.
23 changes: 23 additions & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ pub struct WorkspaceCommandHelper {
env: WorkspaceCommandEnvironment,
// TODO: Parsed template can be cached if it doesn't capture 'repo lifetime
commit_summary_template_text: String,
op_summary_template_text: String,
may_update_working_copy: bool,
working_copy_shared_with_git: bool,
}
Expand All @@ -740,6 +741,7 @@ impl WorkspaceCommandHelper {
let settings = env.settings();
let commit_summary_template_text =
settings.config().get_string("templates.commit_summary")?;
let op_summary_template_text = settings.config().get_string("templates.op_summary")?;
let may_update_working_copy =
loaded_at_head && !env.command.global_args().ignore_working_copy;
let working_copy_shared_with_git = is_colocated_git_workspace(&workspace, &repo);
Expand All @@ -748,11 +750,13 @@ impl WorkspaceCommandHelper {
user_repo: ReadonlyUserRepo::new(repo),
env,
commit_summary_template_text,
op_summary_template_text,
may_update_working_copy,
working_copy_shared_with_git,
};
// Parse commit_summary template early to report error before starting
// mutable operation.
helper.parse_operation_template(&helper.op_summary_template_text)?;
helper.parse_commit_template(&helper.commit_summary_template_text)?;
helper.parse_commit_template(SHORT_CHANGE_ID_TEMPLATE_TEXT)?;
Ok(helper)
Expand Down Expand Up @@ -1288,6 +1292,19 @@ impl WorkspaceCommandHelper {
)
}

/// Parses commit template into evaluation tree.
pub fn parse_operation_template(
&self,
template_text: &str,
) -> TemplateParseResult<TemplateRenderer<'_, Operation>> {
let language = self.operation_template_language();
self.parse_template(
&language,
template_text,
OperationTemplateLanguage::wrap_operation,
)
}

/// Creates commit template language environment for this workspace.
pub fn commit_template_language(&self) -> CommitTemplateLanguage<'_> {
self.env
Expand All @@ -1309,6 +1326,12 @@ impl WorkspaceCommandHelper {
.expect("parse error should be confined by WorkspaceCommandHelper::new()")
}

/// Template for one-line summary of an operation.
pub fn operation_summary_template(&self) -> TemplateRenderer<'_, Operation> {
self.parse_operation_template(&self.op_summary_template_text)
.expect("parse error should be confined by WorkspaceCommandHelper::new()")
}

pub fn short_change_id_template(&self) -> TemplateRenderer<'_, Commit> {
self.parse_commit_template(SHORT_CHANGE_ID_TEMPLATE_TEXT)
.expect("parse error should be confined by WorkspaceCommandHelper::new()")
Expand Down
34 changes: 3 additions & 31 deletions cli/src/commands/operation/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use jj_lib::revset;
use jj_lib::revset::RevsetIteratorExt as _;

use crate::cli_util::short_change_hash;
use crate::cli_util::short_operation_hash;
use crate::cli_util::CommandHelper;
use crate::cli_util::LogContentFormat;
use crate::command_error::CommandError;
Expand Down Expand Up @@ -123,42 +122,15 @@ pub fn cmd_op_diff(
workspace_env.parse_template(&language, &text, CommitTemplateLanguage::wrap_commit)?
};

let op_summary_template = workspace_command.operation_summary_template();
ui.request_pager();
let mut formatter = ui.stdout_formatter();
formatter.with_label("op_log", |formatter| {
write!(formatter, "From operation ")?;
write!(
formatter.labeled("id"),
"{}",
short_operation_hash(from_op.id()),
)?;
write!(formatter, ": ")?;
write!(
formatter.labeled("description"),
"{}",
if from_op.id() == from_op.op_store().root_operation_id() {
"root()"
} else {
&from_op.metadata().description
}
)?;
op_summary_template.format(&from_op, &mut *formatter)?;
writeln!(formatter)?;
write!(formatter, " To operation ")?;
write!(
formatter.labeled("id"),
"{}",
short_operation_hash(to_op.id()),
)?;
write!(formatter, ": ")?;
write!(
formatter.labeled("description"),
"{}",
if to_op.id() == to_op.op_store().root_operation_id() {
"root()"
} else {
&to_op.metadata().description
}
)?;
op_summary_template.format(&to_op, &mut *formatter)?;
writeln!(formatter)
})?;

Expand Down
17 changes: 1 addition & 16 deletions cli/src/commands/operation/undo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use super::DEFAULT_UNDO_WHAT;
use crate::cli_util::CommandHelper;
use crate::command_error::user_error;
use crate::command_error::CommandError;
use crate::operation_templater::OperationTemplateLanguage;
use crate::ui::Ui;

/// Create a new operation that undoes an earlier operation
Expand Down Expand Up @@ -73,21 +72,7 @@ pub fn cmd_op_undo(

if let Some(mut formatter) = ui.status_formatter() {
write!(formatter, "Undid operation ")?;
let workspace_env = workspace_command.env();
let language = OperationTemplateLanguage::new(
bad_op.op_store().root_operation_id(),
Some(bad_op.id()),
workspace_env.operation_template_extensions(),
);
let text = command
.settings()
.config()
.get_string("templates.op_summary")?;
let template = workspace_env.parse_template(
&language,
&text,
OperationTemplateLanguage::wrap_operation,
)?;
let template = workspace_command.operation_summary_template();
template.format(&bad_op, &mut *formatter)?;
writeln!(formatter)?;
}
Expand Down
Loading

0 comments on commit 7ff12ec

Please sign in to comment.