From 6cf7d984658543a7eb6bf60d0694080565815a33 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 17 Dec 2022 10:17:50 -0800 Subject: [PATCH] templater: replace `is_git_head` keyword by `git_head` keyword I'm about to make it possible for `HEAD@git` to be conflicted. For that, we need to be able to include the trailing `?`. This patch prepares for that. --- src/commands/mod.rs | 2 +- src/template_parser.rs | 6 +++--- src/templater.rs | 16 ++++++++++------ tests/test_commit_template.rs | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6670540b1..b474864ef 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1391,7 +1391,7 @@ fn log_template(settings: &UserSettings) -> String { if(branches, " " branches) if(tags, " " tags) if(working_copies, " " working_copies) - if(is_git_head, label("git_head", " HEAD@git")) + if(git_head, " " git_head) " " commit_id.{prefix_format} if(conflict, label("conflict", " conflict")) "\n" diff --git a/src/template_parser.rs b/src/template_parser.rs index a2c8a89ab..3ae0dbce9 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -26,8 +26,8 @@ use crate::templater::{ AuthorProperty, BranchProperty, ChangeIdProperty, CommitIdProperty, CommitOrChangeId, CommitOrChangeIdShort, CommitOrChangeIdShortPrefixAndBrackets, CommitterProperty, ConditionalTemplate, ConflictProperty, DescriptionProperty, DivergentProperty, - DynamicLabelTemplate, EmptyProperty, FormattablePropertyTemplate, GitRefsProperty, - IsGitHeadProperty, IsWorkingCopyProperty, LabelTemplate, ListTemplate, Literal, + DynamicLabelTemplate, EmptyProperty, FormattablePropertyTemplate, GitHeadProperty, + GitRefsProperty, IsWorkingCopyProperty, LabelTemplate, ListTemplate, Literal, SignatureTimestamp, TagProperty, Template, TemplateFunction, TemplateProperty, WorkingCopiesProperty, }; @@ -239,7 +239,7 @@ fn parse_commit_keyword<'a>( "branches" => Property::String(Box::new(BranchProperty { repo })), "tags" => Property::String(Box::new(TagProperty { repo })), "git_refs" => Property::String(Box::new(GitRefsProperty { repo })), - "is_git_head" => Property::Boolean(Box::new(IsGitHeadProperty::new(repo))), + "git_head" => Property::String(Box::new(GitHeadProperty::new(repo))), "divergent" => Property::Boolean(Box::new(DivergentProperty::new(repo))), "conflict" => Property::Boolean(Box::new(ConflictProperty)), "empty" => Property::Boolean(Box::new(EmptyProperty { repo })), diff --git a/src/templater.rs b/src/templater.rs index 51c66c90b..4dea86b9a 100644 --- a/src/templater.rs +++ b/src/templater.rs @@ -348,21 +348,25 @@ impl TemplateProperty for GitRefsProperty<'_> { } } -pub struct IsGitHeadProperty<'a> { +pub struct GitHeadProperty<'a> { repo: RepoRef<'a>, } -impl<'a> IsGitHeadProperty<'a> { +impl<'a> GitHeadProperty<'a> { pub fn new(repo: RepoRef<'a>) -> Self { Self { repo } } } -impl TemplateProperty for IsGitHeadProperty<'_> { - type Output = bool; +impl TemplateProperty for GitHeadProperty<'_> { + type Output = String; - fn extract(&self, context: &Commit) -> Self::Output { - self.repo.view().git_head().as_ref() == Some(context.id()) + fn extract(&self, context: &Commit) -> String { + if self.repo.view().git_head().as_ref() == Some(context.id()) { + "HEAD@git".to_string() + } else { + "".to_string() + } } } diff --git a/tests/test_commit_template.rs b/tests/test_commit_template.rs index 98504edbd..edcb880b3 100644 --- a/tests/test_commit_template.rs +++ b/tests/test_commit_template.rs @@ -173,7 +173,7 @@ fn test_log_git_head() { insta::assert_snapshot!(stdout, @r###" @ 8[e4fac809c] test.user@example.com 2001-02-03 04:05:09.000 +07:00 5[0aaf4754c] | initial - o 9[a45c67d3e] test.user@example.com 2001-02-03 04:05:07.000 +07:00 master HEAD@git 23[0dd059e1] + o 9[a45c67d3e] test.user@example.com 2001-02-03 04:05:07.000 +07:00 master HEAD@git 23[0dd059e1] | (empty) (no description set) o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set)