Skip to content

Commit

Permalink
Dedupe op/commit language creation for graph commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
algmyr committed Mar 20, 2024
1 parent 527bd2f commit 82f5349
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
11 changes: 8 additions & 3 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,14 +904,19 @@ Set which revision the branch points to with `jj branch set {branch_name} -r <RE
&self,
template_text: &str,
) -> Result<Box<dyn Template<Commit> + '_>, CommandError> {
let language = CommitTemplateLanguage::new(
let language = self.commit_template_language()?;
self.parse_template(&language, template_text)
}

/// Creates commit template language environment for this workspace.
pub fn commit_template_language(&self) -> Result<CommitTemplateLanguage<'_>, CommandError> {
Ok(CommitTemplateLanguage::new(
self.repo().as_ref(),
self.workspace_id(),
self.revset_parse_context(),
self.id_prefix_context()?,
self.commit_template_extension.as_deref(),
);
self.parse_template(&language, template_text)
))
}

/// Template for one-line summary of a commit.
Expand Down
11 changes: 8 additions & 3 deletions cli/src/commands/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,16 @@ pub(crate) fn cmd_log(
.settings()
.config()
.get_bool("ui.log-synthetic-elided-nodes")?;
let template = workspace_command.parse_commit_template(&template_string)?;
let with_content_format = LogContentFormat::new(ui, command.settings())?;

let commit_node_template =
workspace_command.parse_commit_template(&command.settings().commit_node_template())?;
let template;
let commit_node_template;
{
let language = workspace_command.commit_template_language()?;
template = workspace_command.parse_template(&language, &template_string)?;
commit_node_template = workspace_command
.parse_template(&language, &command.settings().commit_node_template())?;
}

let elided_node_template = workspace_command.parse_template(
&GenericTemplateLanguage::new(),
Expand Down
15 changes: 10 additions & 5 deletions cli/src/commands/obslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,27 @@ pub(crate) fn cmd_obslog(

let diff_formats =
diff_util::diff_formats_for_log(command.settings(), &args.diff_format, args.patch)?;
let with_content_format = LogContentFormat::new(ui, command.settings())?;

let template_string = match &args.template {
Some(value) => value.to_string(),
None => command.settings().config().get_string("templates.log")?,
};
let template = workspace_command.parse_commit_template(&template_string)?;
let with_content_format = LogContentFormat::new(ui, command.settings())?;

let template;
let commit_node_template;
{
let language = workspace_command.commit_template_language()?;
template = workspace_command.parse_template(&language, &template_string)?;
commit_node_template = workspace_command
.parse_template(&language, &command.settings().commit_node_template())?;
}

ui.request_pager();
let mut formatter = ui.stdout_formatter();
let formatter = formatter.as_mut();
formatter.push_label("log")?;

let commit_node_template =
workspace_command.parse_commit_template(&command.settings().commit_node_template())?;

let mut commits = topo_order_reverse(
vec![start_commit],
|commit: &Commit| commit.id().clone(),
Expand Down
21 changes: 8 additions & 13 deletions cli/src/commands/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ fn cmd_op_log(
[op] => Some(op.id()),
_ => None,
};
let with_content_format = LogContentFormat::new(ui, command.settings())?;

let template = {
let template;
let op_node_template;
{
let language = OperationTemplateLanguage::new(
repo_loader.op_store().root_operation_id(),
current_op_id,
Expand All @@ -168,18 +171,10 @@ fn cmd_op_log(
Some(value) => value.to_owned(),
None => command.settings().config().get_string("templates.op_log")?,
};
command.parse_template(ui, &language, &text)?
};
let with_content_format = LogContentFormat::new(ui, command.settings())?;

let op_node_template = {
let language = OperationTemplateLanguage::new(
repo_loader.op_store().root_operation_id(),
current_op_id,
command.operation_template_extension(),
);
command.parse_template(ui, &language, &command.settings().op_node_template())?
};
template = command.parse_template(ui, &language, &text)?;
op_node_template =
command.parse_template(ui, &language, &command.settings().op_node_template())?;
}

ui.request_pager();
let mut formatter = ui.stdout_formatter();
Expand Down

0 comments on commit 82f5349

Please sign in to comment.