Allow looking up a commit directly
Diff
Cargo.lock | 1 +
Cargo.toml | 1 +
src/git.rs | 13 +++++++++++++
src/methods/repo.rs | 15 +++++++++++++++
4 files changed, 28 insertions(+), 2 deletions(-)
@@ -754,6 +754,7 @@
"humantime",
"owning_ref",
"path-clean",
"serde",
"time",
"tokio",
"tower",
@@ -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"
@@ -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() })
});
Commit(commit)
}
pub fn get_latest_commit(path: &Path) -> Commit {
@@ -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<String>,
}
#[allow(clippy::unused_async)]
pub async fn handle_commit(
Extension(repo): Extension<Repository>,
Extension(RepositoryPath(repository_path)): Extension<RepositoryPath>,
Query(query): Query<CommitQuery>,
) -> Html<String> {
#[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(),