I need to get the list of authors for a git repository, like git shortlog -s does in the command line.
What I'm doing currently, and works, is to do a revwalk (going over all the revisions) and collecting the list of authors for all the commits one at a time, but this is quite slow, especially compared to the command-line git command.
I am using rust but I expect the solution is similar in C or another language binding to libgit2.
This is my current, working, but slow, code:
let repo = Repository::open(&git_path)?;
let mut revwalk = repo.revwalk()?;
revwalk.push_head()?;
let mut authors: Vec<String> = revwalk
.map(|r| {
let oid = r?;
repo.find_commit(oid)
})
.filter_map(|c| match c {
Ok(commit) => Some(commit),
Err(e) => {
println!("Error walking the revisions {}, skipping", e);
None
}
})
.fold(HashSet::new(), |mut sofar, cur| {
if let Some(name) = cur.author().name() {
sofar.insert(name.to_string());
}
sofar
})
.into_iter()
.collect();
authors.sort();
EDIT Ok when building with rust in release mode it is noticeably faster. I still wonder whether there's not a more efficient way still.