diff options
Diffstat (limited to 'src/repo.rs')
-rw-r--r-- | src/repo.rs | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/src/repo.rs b/src/repo.rs index 0056575..3dae808 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -1,5 +1,5 @@ -use git2::Repository; - +use git2::{Commit, Error, Repository as Backend}; +use std::collections::HashSet; // /// Represents a repository on disk // struct Repository { @@ -12,34 +12,42 @@ use git2::Repository; // } /// A structure that represents an existing bare repo on disk -pub struct Repo { - pub inner: Repository, +pub struct Repository { + inner: Backend, } -impl Repo { - pub fn new(path: &'static str) -> Self { +impl Repository { + /// Open an existing bare repo from disk storage + pub fn open(path: &'static str) -> Self { Self { - inner: Repository::open_bare(path).unwrap(), + inner: Backend::open_bare(path).unwrap(), } } - /// Returns a list of commit hashes for a project - pub fn commits(&self) -> Vec<String> { - vec!["f6ca929", "43fb776", "1a0fa2f".into()] + /// Get all commits on head + pub fn head<'a>(&'a self) -> Result<Vec<Commit<'a>>, Error> { + let mut walker = self.inner.revwalk().unwrap(); + walker.push_head()?; + walker .into_iter() - .map(String::from) + .map(|oid| { + let oid = oid.unwrap(); + self.inner.find_commit(oid) + }) .collect() } /// Return the list of contributors - fn contributors(&self) -> Vec<String> { - vec![ - "Katharina Fey <kookie@spacekookie.de", - "Robin Example <robin@example.com", - "Ferris <ferris@the.crab>", - ] - .into_iter() - .map(String::from) - .collect() + fn contributors(&self) -> Result<Vec<String>, Error> { + let head = self.head()?; + Ok(head + .iter() + .map(|c| c.author()) + .fold(HashSet::new(), |mut set, author| { + set.insert(author.name().unwrap().to_owned()); + set + }) + .into_iter() + .collect()) } } |