cache ref names instead of searching on every commit

This commit is contained in:
Trevor Bentley
2023-01-12 02:25:13 +01:00
parent 581b4fbd74
commit c37b7ed8bb
+15 -15
View File
@@ -253,6 +253,19 @@ fn parse_repo(repo: &Repository, name: &str, settings: &GitsySettingsRepo, metad
let mut branch_count = 0; let mut branch_count = 0;
let mut tag_count = 0; let mut tag_count = 0;
// Cache the shortnames of all references
let mut references: BTreeMap<String, Vec<String>> = BTreeMap::new();
for refr in repo.references()? {
let refr = refr?;
if let (Some(target), Some(name)) = (refr.target(), refr.shorthand()) {
let id = target.to_string();
match references.contains_key(&id) {
false => { references.insert(target.to_string(), vec!(name.to_string())); },
true => { references.get_mut(&id).unwrap().push(name.to_string()); },
}
}
}
loud!(); loud!();
let mut revwalk = repo.revwalk()?; let mut revwalk = repo.revwalk()?;
revwalk.set_sorting(git2::Sort::TOPOLOGICAL)?; revwalk.set_sorting(git2::Sort::TOPOLOGICAL)?;
@@ -289,21 +302,8 @@ fn parse_repo(repo: &Repository, name: &str, settings: &GitsySettingsRepo, metad
deletions: stats.deletions(), deletions: stats.deletions(),
}; };
// TODO: is it acceptable to iterate over all references for let alt_refs: Vec<String> = references.get(&commit.id().to_string())
// every commit? Is there another way? Should probably cache .map(|x| x.to_owned()).unwrap_or_default();
// all of the ref IDs in memory.
let mut alt_refs = vec!();
for refr in repo.references()? {
let refr = refr?;
if let Some(target) = refr.target() {
if target == commit.id() {
// TODO: save these
if let Some(name) = refr.shorthand() {
alt_refs.push(name.to_string());
}
}
}
}
if history_count < settings.limit_history.unwrap_or(usize::MAX) { if history_count < settings.limit_history.unwrap_or(usize::MAX) {
loudest!(" + {} {}", full_hash, first_line(commit.message_bytes())); loudest!(" + {} {}", full_hash, first_line(commit.message_bytes()));