List tracked refs on index page

This commit is contained in:
Joscha 2023-08-05 21:03:18 +02:00
parent 2b4a5d4021
commit 3b4d8dab72
5 changed files with 89 additions and 8 deletions

View file

@ -1,5 +1,6 @@
mod config;
mod recurring;
mod repo;
mod somehow;
mod state;
mod web;

14
src/repo.rs Normal file
View file

@ -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<String> {
let hash = hash.parse::<ObjectId>()?;
let commit = repo.find_object(hash)?.try_into_commit()?;
let id = commit.short_id()?;
let summary = commit.message()?.summary();
Ok(format!("{id} ({summary})"))
}

View file

@ -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<Ref>,
}
pub async fn get(
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
State(repo): State<Arc<ThreadSafeRepository>>,
) -> somehow::Result<impl IntoResponse> {
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 });
}
pub async fn get(State(config): State<&'static Config>) -> super::Result<impl IntoResponse> {
Ok(IndexTemplate {
base: config.web.base(),
repo_name: config.repo.name(),
refs,
})
}

View file

@ -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;
}

View file

@ -1,5 +1,21 @@
{% extends "base.html" %}
{% block body %}
<h1>{{ repo_name }}</h1>
<h2>Tracked refs</h2>
<table>
<thead>
<tr>
<th>ref</th>
<th>points to</th>
</tr>
</thead>
<tbody>
{% for ref in refs %}
<tr>
<td>{{ ref.name }}</td>
<td><a href="{{ base }}/commit/{{ ref.hash }}">{{ ref.short }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}