From 2409bb20bb16dfaabfa7d1ba826733aefb99f064 Mon Sep 17 00:00:00 2001 From: Sondre Nilsen Date: Mon, 15 Oct 2018 02:35:37 +0200 Subject: [PATCH] Walk the commit tree for authors --- src/main.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e7c5ba1..805d947 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ #![forbid(unsafe_code)] extern crate git2; -use git2::Repository; +use git2::{Commit, Repository}; use std::collections::HashMap; use std::env; use std::error::Error; @@ -28,16 +28,39 @@ fn main() -> Result<(), Box> { let curses: Vec<&str> = CURSES.lines().collect(); let path = env::current_dir()?; let repo = Repository::open(path)?; + let mut revwalk = repo.revwalk()?; let mut occurrences: HashMap<&str, usize> = HashMap::new(); + let mut commits: Vec = Vec::new(); for curse in &curses { occurrences.entry(curse).or_insert(0); } + revwalk.push_head()?; + for commit in revwalk { + let commit = repo.find_commit(commit?)?; + commits.push(commit); + } + let mut authors: Vec = find_authors(&commits); println!("{:?}", repo.workdir()); filter_occurrences(&mut occurrences); println!("{:#?}", occurrences); + println!("{:#?}", commits); + print!("{:#?}", authors); Ok(()) } +fn find_authors(commits: &[Commit]) -> Vec { + let mut names: Vec = Vec::new(); + let mut res: Vec = Vec::new(); + for commit in commits { + let name = commit.author().name().unwrap().to_string(); + if !names.contains(&name) { + res.push(Author::new(name.as_str(), HashMap::new())); + } + names.push(name); + } + res +} + fn update_occurrence<'a>(word: &'a str, map: &mut HashMap<&'a str, usize>) { map.entry(word).and_modify(|i| *i += 1); }