Serve commit page entirely from the db
This commit is contained in:
parent
0d3cd15b03
commit
7768e4ad4b
12 changed files with 241 additions and 79 deletions
39
src/db.rs
Normal file
39
src/db.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use time::{format_description::well_known::Rfc3339, macros::format_description, OffsetDateTime};
|
||||
|
||||
use crate::somehow;
|
||||
|
||||
pub fn format_time(time: &str) -> somehow::Result<String> {
|
||||
let now = OffsetDateTime::now_utc();
|
||||
let time = OffsetDateTime::parse(time, &Rfc3339)?;
|
||||
let delta = time - now;
|
||||
|
||||
let formatted_time = time.format(format_description!(
|
||||
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
|
||||
))?;
|
||||
let formatted_delta =
|
||||
humantime::format_duration(Duration::from_secs(delta.unsigned_abs().as_secs()));
|
||||
Ok(if delta.is_positive() {
|
||||
format!("{formatted_time} (in {formatted_delta})")
|
||||
} else {
|
||||
format!("{formatted_time} ({formatted_delta} ago)")
|
||||
})
|
||||
}
|
||||
|
||||
pub fn summary(message: &str) -> String {
|
||||
// Take everything up to the first double newline
|
||||
let title = message
|
||||
.split_once("\n\n")
|
||||
.map(|(t, _)| t)
|
||||
.unwrap_or(message);
|
||||
|
||||
// Turn consecutive whitespace into a single space
|
||||
title.split_whitespace().collect::<Vec<_>>().join(" ")
|
||||
}
|
||||
|
||||
pub fn format_commit_short(hash: &str, message: &str) -> String {
|
||||
let short_hash = hash.chars().take(8).collect::<String>();
|
||||
let summary = summary(message);
|
||||
format!("{short_hash} ({summary})")
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
mod config;
|
||||
mod db;
|
||||
mod recurring;
|
||||
mod repo;
|
||||
mod somehow;
|
||||
|
|
|
|||
11
src/repo.rs
11
src/repo.rs
|
|
@ -1,22 +1,17 @@
|
|||
//! Utility functions for accessing a [`Repository`].
|
||||
|
||||
use gix::{actor::IdentityRef, date::Time, Commit};
|
||||
use time::macros::format_description;
|
||||
use gix::{actor::IdentityRef, Commit};
|
||||
|
||||
use crate::somehow;
|
||||
|
||||
// TODO Remove this function
|
||||
pub fn format_actor(author: IdentityRef<'_>) -> somehow::Result<String> {
|
||||
let mut buffer = vec![];
|
||||
author.trim().write_to(&mut buffer)?;
|
||||
Ok(String::from_utf8_lossy(&buffer).to_string())
|
||||
}
|
||||
|
||||
pub fn format_time(time: Time) -> String {
|
||||
time.format(format_description!(
|
||||
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
|
||||
))
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
|
|
|||
|
|
@ -1,29 +1,27 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use askama::Template;
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
response::IntoResponse,
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use gix::{prelude::ObjectIdExt, Id, ObjectId, ThreadSafeRepository};
|
||||
use futures::TryStreamExt;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::{config::Config, repo, somehow};
|
||||
use crate::{config::Config, db, somehow};
|
||||
|
||||
struct Commit {
|
||||
hash: String,
|
||||
description: String,
|
||||
tracked: bool,
|
||||
reachable: i64,
|
||||
}
|
||||
|
||||
impl Commit {
|
||||
fn new(id: Id<'_>, tracked: bool) -> somehow::Result<Self> {
|
||||
let commit = id.object()?.try_into_commit()?;
|
||||
Ok(Self {
|
||||
hash: id.to_string(),
|
||||
description: repo::format_commit_short(&commit)?,
|
||||
tracked,
|
||||
})
|
||||
fn new(hash: String, message: &str, reachable: i64) -> Self {
|
||||
Self {
|
||||
description: db::format_commit_short(&hash, message),
|
||||
hash,
|
||||
reachable,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -34,66 +32,69 @@ struct CommitIdTemplate {
|
|||
repo_name: String,
|
||||
current: String,
|
||||
hash: String,
|
||||
summary: String,
|
||||
message: String,
|
||||
author: String,
|
||||
author_date: String,
|
||||
commit: String,
|
||||
commit_date: String,
|
||||
parents: Vec<Commit>,
|
||||
children: Vec<Commit>,
|
||||
summary: String,
|
||||
message: String,
|
||||
reachable: i64,
|
||||
}
|
||||
|
||||
pub async fn get(
|
||||
Path(hash): Path<String>,
|
||||
State(config): State<&'static Config>,
|
||||
State(db): State<SqlitePool>,
|
||||
State(repo): State<Arc<ThreadSafeRepository>>,
|
||||
) -> somehow::Result<impl IntoResponse> {
|
||||
// Do this first because a &Repository can't be kept across awaits.
|
||||
let child_rows = sqlx::query!(
|
||||
"
|
||||
SELECT child, reachable FROM commit_links
|
||||
JOIN commits ON hash = child
|
||||
WHERE parent = ?
|
||||
",
|
||||
) -> somehow::Result<Response> {
|
||||
let Some(commit) = sqlx::query!("SELECT * FROM commits WHERE hash = ?", hash)
|
||||
.fetch_optional(&db)
|
||||
.await?
|
||||
else {
|
||||
return Ok(StatusCode::NOT_FOUND.into_response());
|
||||
};
|
||||
|
||||
let parents = sqlx::query!(
|
||||
"\
|
||||
SELECT hash, message, reachable FROM commits \
|
||||
JOIN commit_links ON hash = parent \
|
||||
WHERE child = ? \
|
||||
",
|
||||
hash
|
||||
)
|
||||
.fetch_all(&db)
|
||||
.fetch(&db)
|
||||
.map_ok(|r| Commit::new(r.hash, &r.message, r.reachable))
|
||||
.try_collect::<Vec<_>>()
|
||||
.await?;
|
||||
|
||||
// TODO Store commit info in db and avoid Repository
|
||||
// TODO Include untracked info for current commit
|
||||
let repo = repo.to_thread_local();
|
||||
let id = hash.parse::<ObjectId>()?.attach(&repo);
|
||||
let commit = id.object()?.try_into_commit()?;
|
||||
let author_info = commit.author()?;
|
||||
let committer_info = commit.committer()?;
|
||||
|
||||
let mut parents = vec![];
|
||||
for id in commit.parent_ids() {
|
||||
// TODO Include untracked info for parents
|
||||
parents.push(Commit::new(id, true)?);
|
||||
}
|
||||
|
||||
let mut children = vec![];
|
||||
for row in child_rows {
|
||||
let id = row.child.parse::<ObjectId>()?.attach(&repo);
|
||||
children.push(Commit::new(id, row.reachable != 0)?);
|
||||
}
|
||||
let children = sqlx::query!(
|
||||
"\
|
||||
SELECT hash, message, reachable FROM commits \
|
||||
JOIN commit_links ON hash = child \
|
||||
WHERE parent = ? \
|
||||
",
|
||||
hash
|
||||
)
|
||||
.fetch(&db)
|
||||
.map_ok(|r| Commit::new(r.hash, &r.message, r.reachable))
|
||||
.try_collect::<Vec<_>>()
|
||||
.await?;
|
||||
|
||||
Ok(CommitIdTemplate {
|
||||
base: config.web.base(),
|
||||
repo_name: config.repo.name(),
|
||||
current: "commit".to_string(),
|
||||
hash: id.to_string(),
|
||||
summary: commit.message()?.summary().to_string(),
|
||||
message: commit.message_raw()?.to_string().trim_end().to_string(),
|
||||
author: repo::format_actor(author_info.actor())?,
|
||||
author_date: repo::format_time(author_info.time),
|
||||
commit: repo::format_actor(committer_info.actor())?,
|
||||
commit_date: repo::format_time(committer_info.time),
|
||||
hash: commit.hash,
|
||||
author: commit.author,
|
||||
author_date: db::format_time(&commit.author_date)?,
|
||||
commit: commit.committer,
|
||||
commit_date: db::format_time(&commit.committer_date)?,
|
||||
parents,
|
||||
children,
|
||||
})
|
||||
summary: db::summary(&commit.message),
|
||||
message: commit.message.trim_end().to_string(),
|
||||
reachable: commit.reachable,
|
||||
}
|
||||
.into_response())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue