From 10794461b96e730ebc4c806cf531e23406713acf Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Mon, 2 Jan 2023 15:34:54 -0800 Subject: [PATCH] Templater: Pass `Repo` to CommitOrChageIdShortest template objects It will be necessary to compute the shorter unique prefix. --- src/template_parser.rs | 34 ++++++++++++++++++++++------------ src/templater.rs | 6 ++++-- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/template_parser.rs b/src/template_parser.rs index c33278bf3..e231548d5 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -98,7 +98,10 @@ impl TemplateProperty for RelativeTimestampString { enum Property<'a, I> { String(Box + 'a>), Boolean(Box + 'a>), - CommitOrChangeId(Box + 'a>), + CommitOrChangeId( + Box + 'a>, + RepoRef<'a>, + ), Signature(Box + 'a>), Timestamp(Box + 'a>), } @@ -114,9 +117,13 @@ impl<'a, I: 'a> Property<'a, I> { first, Box::new(move |value| property.extract(&value)), ))), - Property::CommitOrChangeId(property) => Property::CommitOrChangeId(Box::new( - TemplateFunction::new(first, Box::new(move |value| property.extract(&value))), - )), + Property::CommitOrChangeId(property, repo) => Property::CommitOrChangeId( + Box::new(TemplateFunction::new( + first, + Box::new(move |value| property.extract(&value)), + )), + repo, + ), Property::Signature(property) => Property::Signature(Box::new(TemplateFunction::new( first, Box::new(move |value| property.extract(&value)), @@ -154,9 +161,9 @@ fn parse_method_chain<'a, I: 'a>( let PropertyAndLabels(next_method, labels) = parse_boolean_method(method); (next_method.after(property), labels) } - Property::CommitOrChangeId(property) => { + Property::CommitOrChangeId(property, repo) => { let PropertyAndLabels(next_method, labels) = - parse_commit_or_chain_id_method(method); + parse_commit_or_chain_id_method(method, repo); (next_method.after(property), labels) } Property::Signature(property) => { @@ -197,10 +204,9 @@ fn parse_boolean_method<'a>(method: Pair) -> PropertyAndLabels<'a, bool> { panic!("no such boolean method: {}", name.as_str()); } -// TODO: pass a context to the returned function (we need the repo to find the -// shortest unambiguous prefix) fn parse_commit_or_chain_id_method<'a>( method: Pair, + repo: RepoRef<'a>, ) -> PropertyAndLabels<'a, CommitOrChangeId> { assert_eq!(method.as_rule(), Rule::method); let mut inner = method.into_inner(); @@ -208,7 +214,7 @@ fn parse_commit_or_chain_id_method<'a>( // TODO: validate arguments let this_function = match name.as_str() { - "short" => Property::String(Box::new(CommitOrChangeIdShort)), + "short" => Property::String(Box::new(CommitOrChangeIdShort { repo })), name => panic!("no such commit ID method: {name}"), }; let chain_method = inner.last().unwrap(); @@ -255,8 +261,12 @@ fn parse_commit_keyword<'a>( assert_eq!(pair.as_rule(), Rule::identifier); let property = match pair.as_str() { "description" => Property::String(Box::new(DescriptionProperty)), - "change_id" => Property::CommitOrChangeId(Box::new(CommitOrChangeIdKeyword::change())), - "commit_id" => Property::CommitOrChangeId(Box::new(CommitOrChangeIdKeyword::commit())), + "change_id" => { + Property::CommitOrChangeId(Box::new(CommitOrChangeIdKeyword::change()), repo) + } + "commit_id" => { + Property::CommitOrChangeId(Box::new(CommitOrChangeIdKeyword::commit()), repo) + } "author" => Property::Signature(Box::new(AuthorProperty)), "committer" => Property::Signature(Box::new(CommitterProperty)), "working_copies" => Property::String(Box::new(WorkingCopiesProperty { repo })), @@ -285,7 +295,7 @@ fn coerce_to_string<'a, I: 'a>( property, Box::new(|value| String::from(if value { "true" } else { "false" })), )), - Property::CommitOrChangeId(property) => Box::new(TemplateFunction::new( + Property::CommitOrChangeId(property, _) => Box::new(TemplateFunction::new( property, Box::new(CommitOrChangeIdKeyword::default_format), )), diff --git a/src/templater.rs b/src/templater.rs index 968a8f5c4..29713c3d7 100644 --- a/src/templater.rs +++ b/src/templater.rs @@ -446,9 +446,11 @@ impl CommitOrChangeIdKeyword { } } -pub struct CommitOrChangeIdShort; +pub struct CommitOrChangeIdShort<'a> { + pub repo: RepoRef<'a>, +} -impl TemplateProperty for CommitOrChangeIdShort { +impl TemplateProperty for CommitOrChangeIdShort<'_> { fn extract(&self, context: &CommitOrChangeId) -> String { CommitOrChangeIdKeyword::short_format(context.clone()) }