aboutsummaryrefslogtreecommitdiff
path: root/src/repo.rs
blob: ca1f889027e7d633e54c5c0a8bdc7c01351d6e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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())
    }
}