summary history branches tags files
commit:c37b7ed8bb91f69af24575dffd9425c421059988
author:Trevor Bentley
committer:Trevor Bentley
date:Thu Jan 12 02:25:13 2023 +0100
parents:581b4fbd7497e070d018289c290f247fed281b37
cache ref names instead of searching on every commit
diff --git a/src/main.rs b/src/main.rs
line changes: +15/-15
index b4a31cd..b8bec16
--- a/src/main.rs
+++ b/src/main.rs
@@ -253,6 +253,19 @@ fn parse_repo(repo: &Repository, name: &str, settings: &GitsySettingsRepo, metad
     let mut branch_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!();
     let mut revwalk = repo.revwalk()?;
     revwalk.set_sorting(git2::Sort::TOPOLOGICAL)?;
@@ -289,21 +302,8 @@ fn parse_repo(repo: &Repository, name: &str, settings: &GitsySettingsRepo, metad
             deletions: stats.deletions(),
         };
 
-        // TODO: is it acceptable to iterate over all references for
-        // every commit?  Is there another way?  Should probably cache
-        // 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());
-                    }
-                }
-            }
-        }
+        let alt_refs: Vec<String> = references.get(&commit.id().to_string())
+            .map(|x| x.to_owned()).unwrap_or_default();
 
         if history_count < settings.limit_history.unwrap_or(usize::MAX) {
             loudest!("   + {} {}", full_hash, first_line(commit.message_bytes()));