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 2, 2024
1 parent 25504f8 commit 8e3dbbf
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 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())))
}

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 @@ -847,6 +859,23 @@ 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 {
let ref_name = RefName {
name: topic.to_owned(),
remote: None,
conflict: false,
synced: true, // has no tracking remotes
};
index.insert(commits, 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 8e3dbbf

Please sign in to comment.