List tracked refs on index page
This commit is contained in:
parent
2b4a5d4021
commit
3b4d8dab72
5 changed files with 89 additions and 8 deletions
|
|
@ -1,5 +1,6 @@
|
|||
mod config;
|
||||
mod recurring;
|
||||
mod repo;
|
||||
mod somehow;
|
||||
mod state;
|
||||
mod web;
|
||||
|
|
|
|||
14
src/repo.rs
Normal file
14
src/repo.rs
Normal 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})"))
|
||||
}
|
||||
|
|
@ -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>) -> super::Result<impl IntoResponse> {
|
||||
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 });
|
||||
}
|
||||
|
||||
Ok(IndexTemplate {
|
||||
base: config.web.base(),
|
||||
repo_name: config.repo.name(),
|
||||
refs,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue