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/main.rs | 15 --------------- src/pages/repo.rs | 36 ------------------------------------ src/pages/repo/about.rs | 36 ++++++++++++++++++++++++++++++++++++ src/pages/repo/mod.rs | 3 +++ src/repo.rs | 48 ++++++++++++++++++++++++++++-------------------- 5 files changed, 67 insertions(+), 71 deletions(-) delete mode 100644 src/pages/repo.rs create mode 100644 src/pages/repo/about.rs create mode 100644 src/pages/repo/mod.rs diff --git a/src/main.rs b/src/main.rs index ed633e0..c0b0f61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,20 +14,5 @@ fn main() -> std::io::Result<()> { // .bind("127.0.0.1:8080")? // .run()?; - let r = repo::Repo::new("./testrepo"); - let mut walker = r.inner.revwalk().unwrap(); - walker.push_head().unwrap(); - dbg!(walker - .into_iter() - .map(|oid| { - let oid = oid.unwrap(); - r.inner - .find_commit(oid) - .unwrap() - .message() - .unwrap() - .to_owned() - }) - .collect::>()); Ok(()) } diff --git a/src/pages/repo.rs b/src/pages/repo.rs deleted file mode 100644 index aa66839..0000000 --- a/src/pages/repo.rs +++ /dev/null @@ -1,36 +0,0 @@ -//! The view that renders a repository - -use actix_web::{web, HttpRequest, HttpResponse, Result}; -use askama::Template; - -#[derive(Template)] -#[template(path = "repo.html")] -struct Repo<'a> { - project_owner: &'a str, - project_summary: &'a str, - project_name: &'a str, - project_logo: &'a str, - project_logo_alt_text: &'a str, - first_commit: &'a str, - num_commits: usize, - num_contributors: usize, -} - -pub fn render(req: HttpRequest, path: web::Path<(String)>) -> Result { - println!("Rendering path: {:#?}", path); - dbg!(req); - - let repo = Repo { - project_owner: "spacekookie", - project_summary: "A lightweight web frontend for git repositories", - project_name: "webgit", - project_logo: "rust.png", - project_logo_alt_text: "Rust logo", - first_commit: "f6ca929", - num_commits: 123, - num_contributors: 3, - } - .render() - .unwrap(); - Ok(HttpResponse::Ok().content_type("text/html").body(repo)) -} diff --git a/src/pages/repo/about.rs b/src/pages/repo/about.rs new file mode 100644 index 0000000..adfa46e --- /dev/null +++ b/src/pages/repo/about.rs @@ -0,0 +1,36 @@ +//! The "repository/about" subpage + +use actix_web::{web, HttpRequest, HttpResponse, Result}; +use askama::Template; + +#[derive(Template)] +#[template(path = "repo.html")] +struct Repo<'a> { + project_owner: &'a str, + project_summary: &'a str, + project_name: &'a str, + project_logo: &'a str, + project_logo_alt_text: &'a str, + first_commit: &'a str, + num_commits: usize, + num_contributors: usize, +} + +pub fn render(req: HttpRequest, path: web::Path<(String)>) -> Result { + println!("Rendering path: {:#?}", path); + dbg!(req); + + let repo = Repo { + project_owner: "spacekookie", + project_summary: "A lightweight web frontend for git repositories", + project_name: "webgit", + project_logo: "rust.png", + project_logo_alt_text: "Rust logo", + first_commit: "f6ca929", + num_commits: 123, + num_contributors: 3, + } + .render() + .unwrap(); + Ok(HttpResponse::Ok().content_type("text/html").body(repo)) +} diff --git a/src/pages/repo/mod.rs b/src/pages/repo/mod.rs new file mode 100644 index 0000000..c339350 --- /dev/null +++ b/src/pages/repo/mod.rs @@ -0,0 +1,3 @@ +//! The repository page subtree + +pub mod about; 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