From 01742ecb810c1c7e2184f9c91985a954a5d9d485 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Wed, 06 Jul 2022 01:20:04 +0100 Subject: [PATCH] Allow looking up a commit directly --- Cargo.lock | 1 + Cargo.toml | 1 + src/git.rs | 13 +++++++++++++ src/methods/repo.rs | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c0b44e..8565f39 100644 --- a/Cargo.lock +++ a/Cargo.lock @@ -754,6 +754,7 @@ "humantime", "owning_ref", "path-clean", + "serde", "time", "tokio", "tower", diff --git a/Cargo.toml b/Cargo.toml index 3a8dfe2..5a09a41 100644 --- a/Cargo.toml +++ a/Cargo.toml @@ -14,6 +14,7 @@ humantime = "2.1" path-clean = "0.1" owning_ref = "0.4" +serde = { version = "1.0", features = ["derive"] } time = "0.3" tokio = { version = "1.19", features = ["full"] } tower = "0.4" diff --git a/src/git.rs b/src/git.rs index d644fdf..2d7eeb9 100644 --- a/src/git.rs +++ a/src/git.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, collections::BTreeMap, fmt::Display, path::Path, time::Duration}; -use git2::{Repository, Signature}; +use git2::{Oid, Repository, Signature}; use owning_ref::OwningHandle; use time::OffsetDateTime; @@ -62,6 +62,17 @@ pub fn body(&self) -> &str { self.0.message().unwrap() } +} + +pub fn get_commit(path: &Path, commit: &str) -> Commit { + let repo = Repository::open_bare(path).unwrap(); + + let commit = OwningHandle::new_with_fn(Box::new(repo), |v| { + Box::new(unsafe { (*v).find_commit(Oid::from_str(commit).unwrap()).unwrap() }) + }); + + // TODO: we can cache this + Commit(commit) } pub fn get_latest_commit(path: &Path) -> Commit { diff --git a/src/methods/repo.rs b/src/methods/repo.rs index 99e4b43..a494568 100644 --- a/src/methods/repo.rs +++ a/src/methods/repo.rs @@ -1,9 +1,10 @@ use std::{ ops::Deref, path::{Path, PathBuf}, }; use askama::Template; +use axum::extract::Query; use axum::{ handler::Handler, http::Request, @@ -11,8 +12,10 @@ Extension, }; use path_clean::PathClean; +use serde::Deserialize; use tower::{util::BoxCloneService, Service}; +use crate::git::get_commit; use crate::{get_latest_commit, git::Commit, layers::UnwrapInfallible}; #[derive(Clone)] @@ -118,10 +121,16 @@ Html(View { repo }.render().unwrap()) } +#[derive(Deserialize)] +pub struct CommitQuery { + id: Option, +} + #[allow(clippy::unused_async)] pub async fn handle_commit( Extension(repo): Extension, Extension(RepositoryPath(repository_path)): Extension, + Query(query): Query, ) -> Html { #[derive(Template)] #[template(path = "repo/commit.html")] @@ -133,7 +142,11 @@ Html( View { repo, - commit: get_latest_commit(&repository_path), + commit: if let Some(commit) = query.id { + get_commit(&repository_path, &commit) + } else { + get_latest_commit(&repository_path) + }, } .render() .unwrap(), -- rgit 0.1.3