Refactor formatting functions

This commit is contained in:
Joscha 2023-08-05 23:04:28 +02:00
parent 732c33d6f7
commit eb29f1166a
2 changed files with 17 additions and 10 deletions

View file

@ -1,14 +1,17 @@
//! Utility functions for accessing a [`Repository`]. //! Utility functions for accessing a [`Repository`].
use gix::{ObjectId, Repository}; use gix::{actor::IdentityRef, Commit};
use crate::somehow; use crate::somehow;
pub fn short_commit(repo: &Repository, hash: &str) -> somehow::Result<String> { pub fn format_actor(author: IdentityRef<'_>) -> somehow::Result<String> {
let hash = hash.parse::<ObjectId>()?; let mut buffer = vec![];
let commit = repo.find_object(hash)?.try_into_commit()?; author.write_to(&mut buffer)?;
Ok(String::from_utf8_lossy(&buffer).to_string())
}
let id = commit.short_id()?; pub fn format_commit_short(commit: &Commit<'_>) -> somehow::Result<String> {
let id = commit.id().shorten_or_id();
let summary = commit.message()?.summary(); let summary = commit.message()?.summary();
Ok(format!("{id} ({summary})")) Ok(format!("{id} ({summary})"))
} }

View file

@ -2,7 +2,7 @@ use std::sync::Arc;
use askama::Template; use askama::Template;
use axum::{extract::State, response::IntoResponse}; use axum::{extract::State, response::IntoResponse};
use gix::ThreadSafeRepository; use gix::{prelude::ObjectIdExt, ObjectId, ThreadSafeRepository};
use sqlx::SqlitePool; use sqlx::SqlitePool;
use crate::{config::Config, repo, somehow}; use crate::{config::Config, repo, somehow};
@ -35,10 +35,14 @@ pub async fn get(
let mut refs = vec![]; let mut refs = vec![];
for row in rows { for row in rows {
let name = row.name; let id = row.hash.parse::<ObjectId>()?.attach(&repo);
let hash = row.hash; let commit = id.object()?.try_into_commit()?;
let short = repo::short_commit(&repo, &hash)?;
refs.push(Ref { name, hash, short }); refs.push(Ref {
name: row.name,
hash: row.hash,
short: repo::format_commit_short(&commit)?,
});
} }
Ok(IndexTemplate { Ok(IndexTemplate {