🏡 index : ~doyle/rgit.git

author Jordan Doyle <jordan@doyle.la> 2022-07-06 1:20:04.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-07-06 1:20:04.0 +01:00:00
commit
01742ecb810c1c7e2184f9c91985a954a5d9d485 [patch]
tree
4c23344504fc41bbf447819a412edea4b2fc14db
parent
afaae25fb783d32ca1a2c3a6b39c0604a6437c77
download
01742ecb810c1c7e2184f9c91985a954a5d9d485.tar.gz

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(-)

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<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(),