From 72284c9fa6462e77d892bf3bc2463b59afd0f626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C3=84lgmyr?= Date: Tue, 19 Mar 2024 08:47:45 +0100 Subject: [PATCH] Dedupe op/commit language creation for graph commands. --- cli/src/cli_util.rs | 11 ++++++++--- cli/src/commands/log.rs | 11 ++++++++--- cli/src/commands/obslog.rs | 15 ++++++++++----- cli/src/commands/operation.rs | 21 ++++++++------------- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index b4773700a6..b348a82dfa 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -904,14 +904,19 @@ Set which revision the branch points to with `jj branch set {branch_name} -r Result + '_>, 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, 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. diff --git a/cli/src/commands/log.rs b/cli/src/commands/log.rs index fcc930a3d9..7d2aa556e0 100644 --- a/cli/src/commands/log.rs +++ b/cli/src/commands/log.rs @@ -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(), diff --git a/cli/src/commands/obslog.rs b/cli/src/commands/obslog.rs index 581892af16..362a8bbee6 100644 --- a/cli/src/commands/obslog.rs +++ b/cli/src/commands/obslog.rs @@ -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(), diff --git a/cli/src/commands/operation.rs b/cli/src/commands/operation.rs index 3abb60efdf..d4051d4f68 100644 --- a/cli/src/commands/operation.rs +++ b/cli/src/commands/operation.rs @@ -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, @@ -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();