Skip to content

Commit

Permalink
lib: add topic support to commit templates
Browse files Browse the repository at this point in the history
  • Loading branch information
noahmayr committed May 4, 2024
1 parent acd0a0a commit 6de408e
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::any::Any;
use std::cmp::max;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::io;
use std::rc::Rc;

Expand Down Expand Up @@ -399,6 +399,7 @@ pub struct CommitKeywordCache<'repo> {
// Build index lazily, and Rc to get away from &self lifetime.
branches_index: OnceCell<Rc<RefNamesIndex>>,
tags_index: OnceCell<Rc<RefNamesIndex>>,
topics_index: OnceCell<Rc<RefNamesIndex>>,
git_refs_index: OnceCell<Rc<RefNamesIndex>>,
is_immutable_fn: OnceCell<Rc<RevsetContainingFn<'repo>>>,
}
Expand All @@ -414,6 +415,11 @@ impl<'repo> CommitKeywordCache<'repo> {
.get_or_init(|| Rc::new(build_ref_names_index(repo.view().tags())))
}

pub fn topics_index(&self, repo: &dyn Repo) -> &Rc<RefNamesIndex> {
self.topics_index
.get_or_init(|| Rc::new(build_topic_index(repo.view().topics().iter())))
}

pub fn git_refs_index(&self, repo: &dyn Repo) -> &Rc<RefNamesIndex> {
self.git_refs_index
.get_or_init(|| Rc::new(build_ref_names_index(repo.view().git_refs())))
Expand Down Expand Up @@ -568,6 +574,12 @@ fn builtin_commit_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'repo, Comm
let out_property = self_property.map(move |commit| index.get(commit.id()).to_vec());
Ok(L::wrap_ref_name_list(out_property))
});
map.insert("topics", |language, _build_ctx, self_property, function| {
template_parser::expect_no_arguments(function)?;
let index = language.keyword_cache.topics_index(language.repo).clone();
let out_property = self_property.map(move |commit| index.get(commit.id()).to_vec());
Ok(L::wrap_ref_name_list(out_property))
});
map.insert(
"git_refs",
|language, _build_ctx, self_property, function| {
Expand Down Expand Up @@ -918,6 +930,21 @@ fn build_branches_index(repo: &dyn Repo) -> RefNamesIndex {
index
}

fn build_topic_index<'a, T>(topics: T) -> RefNamesIndex
where
T: Iterator<Item = (&'a String, &'a HashSet<CommitId>)>,
{
let mut index = RefNamesIndex::default();
for (topic, commits) in topics {
// TODO: single refname for all commits
for id in commits {
let ref_name = RefName::local_only(topic, RefTarget::normal(id.clone()));
index.insert([id], ref_name);
}
}
index
}

fn build_ref_names_index<'a>(
ref_pairs: impl IntoIterator<Item = (&'a String, &'a RefTarget)>,
) -> RefNamesIndex {
Expand Down

0 comments on commit 6de408e

Please sign in to comment.