From 3b4d8dab728765db18181d08d03f1796b2d0db3f Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 5 Aug 2023 21:03:18 +0200 Subject: [PATCH] List tracked refs on index page --- src/main.rs | 1 + src/repo.rs | 14 ++++++++++++++ src/web/index.rs | 34 ++++++++++++++++++++++++++++++++-- static/base.css | 30 +++++++++++++++++++++++++----- templates/index.html | 18 +++++++++++++++++- 5 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 src/repo.rs diff --git a/src/main.rs b/src/main.rs index 1c59f24..09c3ce0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod config; mod recurring; +mod repo; mod somehow; mod state; mod web; diff --git a/src/repo.rs b/src/repo.rs new file mode 100644 index 0000000..6ddba76 --- /dev/null +++ b/src/repo.rs @@ -0,0 +1,14 @@ +//! Utility functions for accessing a [`Repository`]. + +use gix::{ObjectId, Repository}; + +use crate::somehow; + +pub fn short_commit(repo: &Repository, hash: &str) -> somehow::Result { + let hash = hash.parse::()?; + let commit = repo.find_object(hash)?.try_into_commit()?; + + let id = commit.short_id()?; + let summary = commit.message()?.summary(); + Ok(format!("{id} ({summary})")) +} diff --git a/src/web/index.rs b/src/web/index.rs index 610a94e..4d8afba 100644 --- a/src/web/index.rs +++ b/src/web/index.rs @@ -1,18 +1,48 @@ +use std::sync::Arc; + use askama::Template; use axum::{extract::State, response::IntoResponse}; +use gix::ThreadSafeRepository; +use sqlx::SqlitePool; -use crate::config::Config; +use crate::{config::Config, repo, somehow}; + +struct Ref { + name: String, + hash: String, + short: String, +} #[derive(Template)] #[template(path = "index.html")] struct IndexTemplate { base: String, repo_name: String, + refs: Vec, } -pub async fn get(State(config): State<&'static Config>) -> super::Result { +pub async fn get( + State(config): State<&'static Config>, + State(db): State, + State(repo): State>, +) -> somehow::Result { + let repo = repo.to_thread_local(); + + let rows = sqlx::query!("SELECT name, hash FROM tracked_refs") + .fetch_all(&db) + .await?; + + let mut refs = vec![]; + for row in rows { + let name = row.name; + let hash = row.hash; + let short = repo::short_commit(&repo, &hash)?; + refs.push(Ref { name, hash, short }); + } + Ok(IndexTemplate { base: config.web.base(), repo_name: config.repo.name(), + refs, }) } diff --git a/static/base.css b/static/base.css index 71b9934..097c5e9 100644 --- a/static/base.css +++ b/static/base.css @@ -3,9 +3,12 @@ color-scheme: light dark; } +/* Nav bar */ + nav { font-size: 1.5em; line-height: 1.2em; + margin-bottom: 1.5em; padding: 0.3em; background-color: #bdf; border-radius: 0.3em; @@ -33,11 +36,6 @@ nav> :last-child { nav a { color: black; - text-decoration: none; -} - -nav a:hover { - text-decoration: underline; } nav img { @@ -45,3 +43,25 @@ nav img { height: 1.2em; vertical-align: -20%; } + +/* General */ + +a { + text-decoration: none; +} + +a:hover { + text-decoration: underline; +} + +table { + border-collapse: collapse; +} + +td { + padding: 0 1ch; +} + +tbody tr:nth-child(odd) { + background-color: #ddd; +} diff --git a/templates/index.html b/templates/index.html index 1cfabbf..b86203c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,5 +1,21 @@ {% extends "base.html" %} {% block body %} -

{{ repo_name }}

+

Tracked refs

+ + + + + + + + + {% for ref in refs %} + + + + + {% endfor %} + +
refpoints to
{{ ref.name }}{{ ref.short }}
{% endblock %}