Show tracked refs from db

This commit is contained in:
Joscha 2023-08-06 13:11:36 +02:00
parent 21d97a5bf4
commit dca83eed55
4 changed files with 29 additions and 35 deletions

View file

@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "SELECT name, hash FROM refs WHERE tracked", "query": "SELECT name, hash, message FROM refs JOIN commits USING (hash) WHERE tracked ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@ -12,15 +12,21 @@
"name": "hash", "name": "hash",
"ordinal": 1, "ordinal": 1,
"type_info": "Text" "type_info": "Text"
},
{
"name": "message",
"ordinal": 2,
"type_info": "Text"
} }
], ],
"parameters": { "parameters": {
"Right": 0 "Right": 0
}, },
"nullable": [ "nullable": [
false,
false, false,
false false
] ]
}, },
"hash": "3e31ed6194d487b58b8aa0f10438b731232104af05b8e5bd056b69e69c91b703" "hash": "0ac905715f402ec22f28dad89ba60f21644418d1f483aecbcd425a0b69c16bb3"
} }

View file

@ -1,6 +1,6 @@
//! Utility functions for accessing a [`Repository`]. //! Utility functions for accessing a [`Repository`].
use gix::{actor::IdentityRef, Commit}; use gix::actor::IdentityRef;
use crate::somehow; use crate::somehow;
@ -10,10 +10,3 @@ pub fn format_actor(author: IdentityRef<'_>) -> somehow::Result<String> {
author.trim().write_to(&mut buffer)?; author.trim().write_to(&mut buffer)?;
Ok(String::from_utf8_lossy(&buffer).to_string()) Ok(String::from_utf8_lossy(&buffer).to_string())
} }
// TODO Remove this function
pub fn format_commit_short(commit: &Commit<'_>) -> somehow::Result<String> {
let id = commit.id().shorten_or_id();
let summary = commit.message()?.summary();
Ok(format!("{id} ({summary})"))
}

View file

@ -1,11 +1,9 @@
use std::sync::Arc;
use askama::Template; use askama::Template;
use axum::{extract::State, response::IntoResponse}; use axum::{extract::State, response::IntoResponse};
use gix::{prelude::ObjectIdExt, ObjectId, ThreadSafeRepository}; use futures::TryStreamExt;
use sqlx::SqlitePool; use sqlx::SqlitePool;
use crate::{config::Config, repo, somehow}; use crate::{config::Config, db, somehow};
struct Ref { struct Ref {
name: String, name: String,
@ -19,36 +17,33 @@ struct IndexTemplate {
base: String, base: String,
repo_name: String, repo_name: String,
current: String, current: String,
refs: Vec<Ref>, tracked_refs: Vec<Ref>,
} }
pub async fn get( pub async fn get(
State(config): State<&'static Config>, State(config): State<&'static Config>,
State(db): State<SqlitePool>, State(db): State<SqlitePool>,
State(repo): State<Arc<ThreadSafeRepository>>,
) -> somehow::Result<impl IntoResponse> { ) -> somehow::Result<impl IntoResponse> {
let repo = repo.to_thread_local(); let tracked_refs = sqlx::query!(
"\
let rows = sqlx::query!("SELECT name, hash FROM refs WHERE tracked") SELECT name, hash, message FROM refs \
.fetch_all(&db) JOIN commits USING (hash) \
WHERE tracked \
"
)
.fetch(&db)
.map_ok(|r| Ref {
name: r.name,
short: db::format_commit_short(&r.hash, &r.message),
hash: r.hash,
})
.try_collect::<Vec<_>>()
.await?; .await?;
let mut refs = vec![];
for row in rows {
let id = row.hash.parse::<ObjectId>()?.attach(&repo);
let commit = id.object()?.try_into_commit()?;
refs.push(Ref {
name: row.name,
hash: row.hash,
short: repo::format_commit_short(&commit)?,
});
}
Ok(IndexTemplate { Ok(IndexTemplate {
base: config.web.base(), base: config.web.base(),
repo_name: config.repo.name(), repo_name: config.repo.name(),
current: "index".to_string(), current: "index".to_string(),
refs, tracked_refs,
}) })
} }

View file

@ -5,7 +5,7 @@
{% block body %} {% block body %}
<h2>Tracked refs</h2> <h2>Tracked refs</h2>
<dl> <dl>
{% for ref in refs %} {% for ref in tracked_refs %}
<dt>{{ ref.name }}</dt> <dt>{{ ref.name }}</dt>
<dd><a href="{{ base }}/commit/{{ ref.hash }}">{{ ref.short }}</a></dd> <dd><a href="{{ base }}/commit/{{ ref.hash }}">{{ ref.short }}</a></dd>
{% endfor %} {% endfor %}