aboutsummaryrefslogtreecommitdiff
path: root/src/repo.rs
use git2::{Commit, Error, Repository as Backend};
use std::collections::HashSet;

/// A structure that represents an existing bare repo on disk
pub struct Repository {
    inner: Backend,
}

impl Repository {
    /// Open an existing bare repo from disk storage
    pub fn open(path: &'static str) -> Self {
        Self {
            inner: Backend::open_bare(path).unwrap(),
        }
    }

    /// 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(|oid| {
                let oid = oid.unwrap();
                self.inner.find_commit(oid)
            })
            .collect()
    }

    /// Return the list of contributors
    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())
    }
}