From 5d9a1047578f33e71fc721b19382858813355693 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Wed, 06 Jul 2022 12:54:40 +0100 Subject: [PATCH] Add tag view --- src/git.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- statics/style.css | 6 ++++++ src/methods/repo.rs | 27 +++++++++++++++++++++++++++ templates/repo/commit.html | 1 - templates/repo/tag.html | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 6 deletions(-) diff --git a/src/git.rs b/src/git.rs index b20f84d..f025d13 100644 --- a/src/git.rs +++ a/src/git.rs @@ -7,7 +7,7 @@ }; use arc_swap::ArcSwapOption; -use git2::{Oid, Repository, Signature}; +use git2::{ObjectType, Oid, Repository, Signature}; use moka::future::Cache; use time::OffsetDateTime; @@ -59,6 +59,29 @@ .await } + pub async fn get_tag(&self, repo: PathBuf, tag_name: &str) -> DetailedTag { + let repo = Repository::open_bare(repo).unwrap(); + let tag = repo + .find_reference(&format!("refs/tags/{tag_name}")) + .unwrap() + .peel_to_tag() + .unwrap(); + let tag_target = tag.target().unwrap(); + + let tagged_object = match tag_target.kind() { + Some(ObjectType::Commit) => Some(TaggedObject::Commit(tag_target.id().to_string())), + Some(ObjectType::Tree) => Some(TaggedObject::Tree(tag_target.id().to_string())), + None | Some(_) => None, + }; + + DetailedTag { + name: tag_name.to_string(), + tagger: tag.tagger().map(Into::into), + message: tag.message().unwrap().to_string(), + tagged_object, + } + } + pub async fn get_refs(&self, repo: PathBuf) -> Arc { self.refs .get_with(repo.clone(), async { @@ -114,11 +137,12 @@ continue; }; - let object = object.to_object(&repo) - .unwrap(); + let object = object.to_object(&repo).unwrap(); let blob = object.into_blob().unwrap(); - return Some(Arc::from(String::from_utf8(blob.content().to_vec()).unwrap())); + return Some(Arc::from( + String::from_utf8(blob.content().to_vec()).unwrap(), + )); } None @@ -215,7 +239,21 @@ #[derive(Debug)] pub struct Remote { + pub name: String, +} + +#[derive(Debug)] +pub enum TaggedObject { + Commit(String), + Tree(String), +} + +#[derive(Debug)] +pub struct DetailedTag { pub name: String, + pub tagger: Option, + pub message: String, + pub tagged_object: Option, } #[derive(Debug)] diff --git a/statics/style.css b/statics/style.css index c0e96ce..51cae2b 100644 --- a/statics/style.css +++ a/statics/style.css @@ -85,3 +85,9 @@ color: #000; background-color: #ccc; } + +pre.h2-first-line::first-line { + font-family: sans-serif; + font-size: 1.5em; + font-weight: bold; +} diff --git a/src/methods/repo.rs b/src/methods/repo.rs index f75710c..8dd6269 100644 --- a/src/methods/repo.rs +++ a/src/methods/repo.rs @@ -16,7 +16,7 @@ use serde::Deserialize; use tower::{util::BoxCloneService, Service}; -use crate::git::Refs; +use crate::git::{DetailedTag, Refs}; use crate::{git::Commit, layers::UnwrapInfallible, Git}; #[derive(Clone)] @@ -58,6 +58,7 @@ Some("commit") => BoxCloneService::new(handle_commit.into_service()), Some("diff") => BoxCloneService::new(handle_diff.into_service()), Some("stats") => BoxCloneService::new(handle_stats.into_service()), + Some("tag") => BoxCloneService::new(handle_tag.into_service()), Some(v) => { uri_parts.push(v); BoxCloneService::new(handle_summary.into_service()) @@ -87,6 +88,30 @@ } Html(View { repo }.render().unwrap()) +} + +#[derive(Deserialize)] +pub struct TagQuery { + #[serde(rename = "h")] + name: String, +} + +pub async fn handle_tag( + Extension(repo): Extension, + Extension(RepositoryPath(repository_path)): Extension, + Extension(git): Extension, + Query(query): Query, +) -> Html { + #[derive(Template)] + #[template(path = "repo/tag.html")] + pub struct View { + repo: Repository, + tag: DetailedTag, + } + + let tag = git.get_tag(repository_path, &query.name).await; + + Html(View { repo, tag }.render().unwrap()) } #[derive(Deserialize)] diff --git a/templates/repo/commit.html b/templates/repo/commit.html index f4e13b7..70eeee2 100644 --- a/templates/repo/commit.html +++ a/templates/repo/commit.html @@ -34,5 +34,4 @@

{{ commit.summary() }}

{{ commit.body() }}
- {% endblock %}diff --git a/templates/repo/tag.html b/templates/repo/tag.html new file mode 100644 index 0000000..09bd4bb 100644 --- /dev/null +++ a/templates/repo/tag.html @@ -1,0 +1,37 @@ +{% extends "repo/base.html" %} + +{% block content %} + + + + + + + {% if let Some(tagger) = tag.tagger %} + + + + + + + + + {% endif %} + {% if let Some(tagged_object) = tag.tagged_object %} + + + + + {% endif %} + +
tag name{{ tag.name }}
tag date{{ tagger.time() }}
tagged by{{ tagger.name() }} <{{ tagger.email() }}>
tagged object + {% match tagged_object %} + {% when crate::git::TaggedObject::Commit with (commit) %} + commit {{ commit.as_str()[0..10] }}... + {% when crate::git::TaggedObject::Tree with (tree) %} + tree {{ tree }} + {% endmatch %} +
+ +
{{ tag.message }}
+{% endblock %} -- rgit 0.1.3