From 8df7857706cb2e7992270b00df52c9a753b42c09 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 18 Jul 2024 00:47:12 +0900 Subject: [PATCH] cli_util: add tx.commit_summary_template() and related helper functions These functions mirror the ones in WorkspaceCommandHelper. I'm not sure if all of them will have to be public, but parse_commit_template() might be useful in order to implement description template. --- cli/src/cli_util.rs | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index 8f49130b34..eff1ca76fe 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -1659,27 +1659,43 @@ impl WorkspaceCommandTransaction<'_> { formatter: &mut dyn Formatter, commit: &Commit, ) -> std::io::Result<()> { + self.commit_summary_template().format(commit, formatter) + } + + /// Template for one-line summary of a commit within transaction. + pub fn commit_summary_template(&self) -> TemplateRenderer<'_, Commit> { + self.parse_commit_template(&self.helper.commit_summary_template_text) + .expect("parse error should be confined by WorkspaceCommandHelper::new()") + } + + /// Creates commit template language environment capturing the current + /// transaction state. + pub fn commit_template_language(&self) -> CommitTemplateLanguage<'_> { let id_prefix_context = self .id_prefix_context .get_or_try_init(|| self.helper.new_id_prefix_context()) .expect("parse error should be confined by WorkspaceCommandHelper::new()"); - let language = CommitTemplateLanguage::new( + CommitTemplateLanguage::new( self.tx.repo(), &self.helper.path_converter, self.helper.workspace_id(), self.helper.revset_parse_context(), id_prefix_context, &self.helper.commit_template_extensions, - ); - let template = self - .helper - .parse_template( - &language, - &self.helper.commit_summary_template_text, - CommitTemplateLanguage::wrap_commit, - ) - .expect("parse error should be confined by WorkspaceCommandHelper::new()"); - template.format(commit, formatter) + ) + } + + /// Parses commit template with the current transaction state. + pub fn parse_commit_template( + &self, + template_text: &str, + ) -> Result, CommandError> { + let language = self.commit_template_language(); + self.helper.parse_template( + &language, + template_text, + CommitTemplateLanguage::wrap_commit, + ) } pub fn finish(self, ui: &mut Ui, description: impl Into) -> Result<(), CommandError> {