From 8b38bdd1478baf6fd53b9a8a53eb3a183c59b781 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Sun, 29 Dec 2019 16:34:39 +0100 Subject: data: adding a small wrapper around libgit2 --- src/repo.rs | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) (limited to 'src/repo.rs') 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 { - vec!["f6ca929", "43fb776", "1a0fa2f".into()] + /// Get all commits on head + pub fn head<'a>(&'a self) -> Result>, 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 { - vec![ - "Katharina Fey ", - ] - .into_iter() - .map(String::from) - .collect() + fn contributors(&self) -> Result, 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()) } } -- cgit v1.2.3