Templater: Pass Repo to CommitOrChageIdShortest template objects

It will be necessary to compute the shorter unique prefix.
This commit is contained in:
Ilya Grigoriev 2023-01-02 15:34:54 -08:00
parent dae42ea34a
commit 10794461b9
2 changed files with 26 additions and 14 deletions

View file

@ -98,7 +98,10 @@ impl TemplateProperty<Timestamp, String> for RelativeTimestampString {
enum Property<'a, I> { enum Property<'a, I> {
String(Box<dyn TemplateProperty<I, String> + 'a>), String(Box<dyn TemplateProperty<I, String> + 'a>),
Boolean(Box<dyn TemplateProperty<I, bool> + 'a>), Boolean(Box<dyn TemplateProperty<I, bool> + 'a>),
CommitOrChangeId(Box<dyn TemplateProperty<I, CommitOrChangeId> + 'a>), CommitOrChangeId(
Box<dyn TemplateProperty<I, CommitOrChangeId> + 'a>,
RepoRef<'a>,
),
Signature(Box<dyn TemplateProperty<I, Signature> + 'a>), Signature(Box<dyn TemplateProperty<I, Signature> + 'a>),
Timestamp(Box<dyn TemplateProperty<I, Timestamp> + 'a>), Timestamp(Box<dyn TemplateProperty<I, Timestamp> + 'a>),
} }
@ -114,9 +117,13 @@ impl<'a, I: 'a> Property<'a, I> {
first, first,
Box::new(move |value| property.extract(&value)), Box::new(move |value| property.extract(&value)),
))), ))),
Property::CommitOrChangeId(property) => Property::CommitOrChangeId(Box::new( Property::CommitOrChangeId(property, repo) => Property::CommitOrChangeId(
TemplateFunction::new(first, Box::new(move |value| property.extract(&value))), Box::new(TemplateFunction::new(
)), first,
Box::new(move |value| property.extract(&value)),
)),
repo,
),
Property::Signature(property) => Property::Signature(Box::new(TemplateFunction::new( Property::Signature(property) => Property::Signature(Box::new(TemplateFunction::new(
first, first,
Box::new(move |value| property.extract(&value)), 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); let PropertyAndLabels(next_method, labels) = parse_boolean_method(method);
(next_method.after(property), labels) (next_method.after(property), labels)
} }
Property::CommitOrChangeId(property) => { Property::CommitOrChangeId(property, repo) => {
let PropertyAndLabels(next_method, labels) = 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) (next_method.after(property), labels)
} }
Property::Signature(property) => { Property::Signature(property) => {
@ -197,10 +204,9 @@ fn parse_boolean_method<'a>(method: Pair<Rule>) -> PropertyAndLabels<'a, bool> {
panic!("no such boolean method: {}", name.as_str()); 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>( fn parse_commit_or_chain_id_method<'a>(
method: Pair<Rule>, method: Pair<Rule>,
repo: RepoRef<'a>,
) -> PropertyAndLabels<'a, CommitOrChangeId> { ) -> PropertyAndLabels<'a, CommitOrChangeId> {
assert_eq!(method.as_rule(), Rule::method); assert_eq!(method.as_rule(), Rule::method);
let mut inner = method.into_inner(); let mut inner = method.into_inner();
@ -208,7 +214,7 @@ fn parse_commit_or_chain_id_method<'a>(
// TODO: validate arguments // TODO: validate arguments
let this_function = match name.as_str() { 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}"), name => panic!("no such commit ID method: {name}"),
}; };
let chain_method = inner.last().unwrap(); let chain_method = inner.last().unwrap();
@ -255,8 +261,12 @@ fn parse_commit_keyword<'a>(
assert_eq!(pair.as_rule(), Rule::identifier); assert_eq!(pair.as_rule(), Rule::identifier);
let property = match pair.as_str() { let property = match pair.as_str() {
"description" => Property::String(Box::new(DescriptionProperty)), "description" => Property::String(Box::new(DescriptionProperty)),
"change_id" => Property::CommitOrChangeId(Box::new(CommitOrChangeIdKeyword::change())), "change_id" => {
"commit_id" => Property::CommitOrChangeId(Box::new(CommitOrChangeIdKeyword::commit())), Property::CommitOrChangeId(Box::new(CommitOrChangeIdKeyword::change()), repo)
}
"commit_id" => {
Property::CommitOrChangeId(Box::new(CommitOrChangeIdKeyword::commit()), repo)
}
"author" => Property::Signature(Box::new(AuthorProperty)), "author" => Property::Signature(Box::new(AuthorProperty)),
"committer" => Property::Signature(Box::new(CommitterProperty)), "committer" => Property::Signature(Box::new(CommitterProperty)),
"working_copies" => Property::String(Box::new(WorkingCopiesProperty { repo })), "working_copies" => Property::String(Box::new(WorkingCopiesProperty { repo })),
@ -285,7 +295,7 @@ fn coerce_to_string<'a, I: 'a>(
property, property,
Box::new(|value| String::from(if value { "true" } else { "false" })), 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, property,
Box::new(CommitOrChangeIdKeyword::default_format), Box::new(CommitOrChangeIdKeyword::default_format),
)), )),

View file

@ -446,9 +446,11 @@ impl CommitOrChangeIdKeyword {
} }
} }
pub struct CommitOrChangeIdShort; pub struct CommitOrChangeIdShort<'a> {
pub repo: RepoRef<'a>,
}
impl TemplateProperty<CommitOrChangeId, String> for CommitOrChangeIdShort { impl TemplateProperty<CommitOrChangeId, String> for CommitOrChangeIdShort<'_> {
fn extract(&self, context: &CommitOrChangeId) -> String { fn extract(&self, context: &CommitOrChangeId) -> String {
CommitOrChangeIdKeyword::short_format(context.clone()) CommitOrChangeIdKeyword::short_format(context.clone())
} }