Refactor formatting functions
This commit is contained in:
parent
732c33d6f7
commit
eb29f1166a
2 changed files with 17 additions and 10 deletions
13
src/repo.rs
13
src/repo.rs
|
|
@ -1,14 +1,17 @@
|
|||
//! Utility functions for accessing a [`Repository`].
|
||||
|
||||
use gix::{ObjectId, Repository};
|
||||
use gix::{actor::IdentityRef, Commit};
|
||||
|
||||
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()?;
|
||||
pub fn format_actor(author: IdentityRef<'_>) -> somehow::Result<String> {
|
||||
let mut buffer = vec![];
|
||||
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();
|
||||
Ok(format!("{id} ({summary})"))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use std::sync::Arc;
|
|||
|
||||
use askama::Template;
|
||||
use axum::{extract::State, response::IntoResponse};
|
||||
use gix::ThreadSafeRepository;
|
||||
use gix::{prelude::ObjectIdExt, ObjectId, ThreadSafeRepository};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::{config::Config, repo, somehow};
|
||||
|
|
@ -35,10 +35,14 @@ pub async fn get(
|
|||
|
||||
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 });
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue