Serve commit page entirely from the db

This commit is contained in:
Joscha 2023-08-06 11:37:22 +02:00
parent 0d3cd15b03
commit 7768e4ad4b
12 changed files with 241 additions and 79 deletions

View file

@ -1,16 +1,21 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "\nSELECT child, reachable FROM commit_links\nJOIN commits ON hash = child\nWHERE parent = ?\n ", "query": "SELECT hash, message, reachable FROM commits JOIN commit_links ON hash = parent WHERE child = ? ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
"name": "child", "name": "hash",
"ordinal": 0, "ordinal": 0,
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "reachable", "name": "message",
"ordinal": 1, "ordinal": 1,
"type_info": "Text"
},
{
"name": "reachable",
"ordinal": 2,
"type_info": "Int64" "type_info": "Int64"
} }
], ],
@ -18,9 +23,10 @@
"Right": 1 "Right": 1
}, },
"nullable": [ "nullable": [
false,
false, false,
false false
] ]
}, },
"hash": "e58a4211444bfe1c965c021085f0e204ca93fd93778b360730976a76e299ffef" "hash": "167a3c4e2ea3540b2608b75019a112916c8f4c413355366cca8b3e41715081c6"
} }

View file

@ -0,0 +1,62 @@
{
"db_name": "SQLite",
"query": "SELECT * FROM commits WHERE hash = ?",
"describe": {
"columns": [
{
"name": "hash",
"ordinal": 0,
"type_info": "Text"
},
{
"name": "author",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "author_date",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "committer",
"ordinal": 3,
"type_info": "Text"
},
{
"name": "committer_date",
"ordinal": 4,
"type_info": "Text"
},
{
"name": "message",
"ordinal": 5,
"type_info": "Text"
},
{
"name": "reachable",
"ordinal": 6,
"type_info": "Int64"
},
{
"name": "new",
"ordinal": 7,
"type_info": "Int64"
}
],
"parameters": {
"Right": 1
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false
]
},
"hash": "61bcc32d29fb7b162f3a51b5b463bc917ddce4a5fc292fb19036a88f697f9056"
}

View file

@ -0,0 +1,32 @@
{
"db_name": "SQLite",
"query": "SELECT hash, message, reachable FROM commits JOIN commit_links ON hash = child WHERE parent = ? ",
"describe": {
"columns": [
{
"name": "hash",
"ordinal": 0,
"type_info": "Text"
},
{
"name": "message",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "reachable",
"ordinal": 2,
"type_info": "Int64"
}
],
"parameters": {
"Right": 1
},
"nullable": [
false,
false,
false
]
},
"hash": "73bdd8a83f317d9f815d5d00215de3f740730b01048f73e62f8931e5dafa3d2f"
}

View file

@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "\nINSERT OR IGNORE INTO commits (hash, author, author_date, committer, committer_date, message)\nVALUES (?, ?, ?, ?, ?, ?)\n ", "query": "\nINSERT OR IGNORE INTO commits (hash, author, author_date, committer, committer_date, message)\nVALUES (?, ?, ?, ?, ?, ?)\n",
"describe": { "describe": {
"columns": [], "columns": [],
"parameters": { "parameters": {
@ -8,5 +8,5 @@
}, },
"nullable": [] "nullable": []
}, },
"hash": "35e5c16c2952f550783b234f27839cf5110d11798d749f523d0e0c98a77194f5" "hash": "a3771c256dde301f1e99aa87da9345a271287beb7e0fea8f90bff9475a8de568"
} }

1
Cargo.lock generated
View file

@ -2761,6 +2761,7 @@ dependencies = [
"directories", "directories",
"futures", "futures",
"gix", "gix",
"humantime",
"humantime-serde", "humantime-serde",
"mime_guess", "mime_guess",
"rust-embed", "rust-embed",

View file

@ -11,12 +11,13 @@ axum = { version = "0.6.19", features = ["macros"] }
clap = { version = "4.3.19", features = ["derive", "deprecated"] } clap = { version = "4.3.19", features = ["derive", "deprecated"] }
directories = "5.0.1" directories = "5.0.1"
futures = "0.3.28" futures = "0.3.28"
humantime = "2.1.0"
humantime-serde = "1.1.1" humantime-serde = "1.1.1"
mime_guess = "2.0.4" mime_guess = "2.0.4"
rust-embed = "6.8.1" rust-embed = "6.8.1"
serde = { version = "1.0.181", features = ["derive"] } serde = { version = "1.0.181", features = ["derive"] }
sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite"] } sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite"] }
time = { version = "0.3.25", features = ["formatting", "macros"] } time = { version = "0.3.25", features = ["formatting", "macros", "parsing"] }
tokio = { version = "1.29.1", features = ["full"] } tokio = { version = "1.29.1", features = ["full"] }
toml = "0.7.6" toml = "0.7.6"
tracing = "0.1.37" tracing = "0.1.37"

39
src/db.rs Normal file
View 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})")
}

View file

@ -1,4 +1,5 @@
mod config; mod config;
mod db;
mod recurring; mod recurring;
mod repo; mod repo;
mod somehow; mod somehow;

View file

@ -1,22 +1,17 @@
//! Utility functions for accessing a [`Repository`]. //! Utility functions for accessing a [`Repository`].
use gix::{actor::IdentityRef, date::Time, Commit}; use gix::{actor::IdentityRef, Commit};
use time::macros::format_description;
use crate::somehow; use crate::somehow;
// TODO Remove this function
pub fn format_actor(author: IdentityRef<'_>) -> somehow::Result<String> { pub fn format_actor(author: IdentityRef<'_>) -> somehow::Result<String> {
let mut buffer = vec![]; let mut buffer = vec![];
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())
} }
pub fn format_time(time: Time) -> String { // TODO Remove this function
time.format(format_description!(
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
))
}
pub fn format_commit_short(commit: &Commit<'_>) -> somehow::Result<String> { pub fn format_commit_short(commit: &Commit<'_>) -> somehow::Result<String> {
let id = commit.id().shorten_or_id(); let id = commit.id().shorten_or_id();
let summary = commit.message()?.summary(); let summary = commit.message()?.summary();

View file

@ -1,29 +1,27 @@
use std::sync::Arc;
use askama::Template; use askama::Template;
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
response::IntoResponse, http::StatusCode,
response::{IntoResponse, Response},
}; };
use gix::{prelude::ObjectIdExt, Id, ObjectId, ThreadSafeRepository}; use futures::TryStreamExt;
use sqlx::SqlitePool; use sqlx::SqlitePool;
use crate::{config::Config, repo, somehow}; use crate::{config::Config, db, somehow};
struct Commit { struct Commit {
hash: String, hash: String,
description: String, description: String,
tracked: bool, reachable: i64,
} }
impl Commit { impl Commit {
fn new(id: Id<'_>, tracked: bool) -> somehow::Result<Self> { fn new(hash: String, message: &str, reachable: i64) -> Self {
let commit = id.object()?.try_into_commit()?; Self {
Ok(Self { description: db::format_commit_short(&hash, message),
hash: id.to_string(), hash,
description: repo::format_commit_short(&commit)?, reachable,
tracked, }
})
} }
} }
@ -34,66 +32,69 @@ struct CommitIdTemplate {
repo_name: String, repo_name: String,
current: String, current: String,
hash: String, hash: String,
summary: String,
message: String,
author: String, author: String,
author_date: String, author_date: String,
commit: String, commit: String,
commit_date: String, commit_date: String,
parents: Vec<Commit>, parents: Vec<Commit>,
children: Vec<Commit>, children: Vec<Commit>,
summary: String,
message: String,
reachable: i64,
} }
pub async fn get( pub async fn get(
Path(hash): Path<String>, Path(hash): Path<String>,
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<Response> {
) -> somehow::Result<impl IntoResponse> { let Some(commit) = sqlx::query!("SELECT * FROM commits WHERE hash = ?", hash)
// Do this first because a &Repository can't be kept across awaits. .fetch_optional(&db)
let child_rows = sqlx::query!( .await?
" else {
SELECT child, reachable FROM commit_links return Ok(StatusCode::NOT_FOUND.into_response());
JOIN commits ON hash = child };
WHERE parent = ?
", let parents = sqlx::query!(
"\
SELECT hash, message, reachable FROM commits \
JOIN commit_links ON hash = parent \
WHERE child = ? \
",
hash hash
) )
.fetch_all(&db) .fetch(&db)
.map_ok(|r| Commit::new(r.hash, &r.message, r.reachable))
.try_collect::<Vec<_>>()
.await?; .await?;
// TODO Store commit info in db and avoid Repository let children = sqlx::query!(
// TODO Include untracked info for current commit "\
let repo = repo.to_thread_local(); SELECT hash, message, reachable FROM commits \
let id = hash.parse::<ObjectId>()?.attach(&repo); JOIN commit_links ON hash = child \
let commit = id.object()?.try_into_commit()?; WHERE parent = ? \
let author_info = commit.author()?; ",
let committer_info = commit.committer()?; hash
)
let mut parents = vec![]; .fetch(&db)
for id in commit.parent_ids() { .map_ok(|r| Commit::new(r.hash, &r.message, r.reachable))
// TODO Include untracked info for parents .try_collect::<Vec<_>>()
parents.push(Commit::new(id, true)?); .await?;
}
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)?);
}
Ok(CommitIdTemplate { Ok(CommitIdTemplate {
base: config.web.base(), base: config.web.base(),
repo_name: config.repo.name(), repo_name: config.repo.name(),
current: "commit".to_string(), current: "commit".to_string(),
hash: id.to_string(), hash: commit.hash,
summary: commit.message()?.summary().to_string(), author: commit.author,
message: commit.message_raw()?.to_string().trim_end().to_string(), author_date: db::format_time(&commit.author_date)?,
author: repo::format_actor(author_info.actor())?, commit: commit.committer,
author_date: repo::format_time(author_info.time), commit_date: db::format_time(&commit.committer_date)?,
commit: repo::format_actor(committer_info.actor())?,
commit_date: repo::format_time(committer_info.time),
parents, parents,
children, children,
}) summary: db::summary(&commit.message),
message: commit.message.trim_end().to_string(),
reachable: commit.reachable,
}
.into_response())
} }

View file

@ -81,11 +81,16 @@ dd {
column-gap: 1ch; column-gap: 1ch;
} }
.commit .untracked, .commit .reachable,
.commit .untracked a { .commit .reachable a {
color: #777; color: #777;
} }
.commit .orphaned,
.commit .orphaned a {
color: #a33;
}
.commit pre { .commit pre {
white-space: pre-wrap; white-space: pre-wrap;
} }

View file

@ -2,6 +2,26 @@
{% block title %}{{ summary }}{% endblock %} {% block title %}{{ summary }}{% endblock %}
{% macro r_class(reachable) %}
{%- if reachable == 0 -%}
orphaned
{%- else if reachable == 1 -%}
reachable
{%- else -%}
tracked
{%- endif -%}
{% endmacro %}
{% macro r_title(reachable) %}
{%- if reachable == 0 -%}
This commit is orphaned. It can't be reached from any ref.
{%- else if reachable == 1 -%}
This commit can only be reached from untracked refs.
{%- else -%}
This commit can be reached from a tracked ref.
{%- endif -%}
{% endmacro %}
{% block body %} {% block body %}
<h2>Commit</h2> <h2>Commit</h2>
<div class="commit"> <div class="commit">
@ -20,20 +40,19 @@
<dd>{{ commit_date }}</dd> <dd>{{ commit_date }}</dd>
{% for commit in parents %} {% for commit in parents %}
<dt>Parent:</dt> <dt class="{% call r_class(commit.reachable) %}" title="{% call r_title(commit.reachable) %}">Parent:</dt>
<dd><a href="{{ commit.hash }}">{{ commit.description }}</a></dd> <dd class="{% call r_class(commit.reachable) %}" title="{% call r_title(commit.reachable) %}">
<a href="{{ commit.hash }}">{{ commit.description }}</a>
</dd>
{% endfor %} {% endfor %}
{% for commit in children %} {% for commit in children %}
{% if commit.tracked %} <dt class="{% call r_class(commit.reachable) %}" title="{% call r_title(commit.reachable) %}">Child:</dt>
<dt>Child:</dt> <dd class="{% call r_class(commit.reachable) %}" title="{% call r_title(commit.reachable) %}">
<dd><a href="{{ commit.hash }}">{{ commit.description }}</a></dd> <a href="{{ commit.hash }}">{{ commit.description }}</a>
{% else %} </dd>
<dt class="untracked">Child:</dt>
<dd class="untracked"><a href="{{ commit.hash }}">{{ commit.description }}</a></dd>
{% endif %}
{% endfor %} {% endfor %}
</dl> </dl>
<pre class="message">{{ message }}</pre> <pre class="message {% call r_class(reachable) %}" title="{% call r_title(reachable) %}">{{ message }}</pre>
</div> </div>
{% endblock %} {% endblock %}