backend.rs: slight refactor and better reliance on types

This commit is contained in:
Emma Tebibyte 2025-08-08 04:53:40 -06:00
parent 1223e6f84c
commit cffe1525e0
Signed by: emma
GPG Key ID: 427287A2F16F44FA

View File

@ -49,14 +49,16 @@ use std::{
path::{ PathBuf, Path }, path::{ PathBuf, Path },
}; };
use git2::{ Commit, ErrorClass, ErrorCode, Repository, Sort }; use git2::{ Commit, Repository, Sort };
use serde::Serialize; use serde::Serialize;
#[derive(Serialize)] #[derive(Serialize)]
pub struct GitCommit { pub struct GitCommit {
author: (Option<String>, Option<String>), // name, e-mail
hash: String, hash: String,
message: String, message: Option<String>,
short_hash: String, short_hash: Option<String>,
time: i64, // seconds since Unix epoch
} }
impl TryFrom<Commit<'_>> for GitCommit { impl TryFrom<Commit<'_>> for GitCommit {
@ -64,46 +66,34 @@ impl TryFrom<Commit<'_>> for GitCommit {
fn try_from(commit: Commit) -> Result<Self, Self::Error> { fn try_from(commit: Commit) -> Result<Self, Self::Error> {
let hash = commit.id().to_string(); let hash = commit.id().to_string();
let short_hash = commit.as_object().short_id()?.as_str().ok_or( let short_hash = commit.as_object().short_id()?.as_str().map(|f| f.to_owned());
Self::Error::new(ErrorCode::Invalid, ErrorClass::Object, "Short ID is not valid UTF-8") let message = commit.message().map(|f| f.to_owned());
)?.to_owned(); let commit_signature = commit.author();
let message = commit.message().ok_or( let name = commit_signature.name().map(|f| f.to_owned());
Self::Error::new(ErrorCode::NotFound, ErrorClass::None, "No commit message") let address = commit_signature.email().map(|f| f.to_owned());
)?.to_owned(); let author = (name, address);
Ok(GitCommit { hash, message, short_hash }) let time = commit.time().seconds();
Ok(GitCommit { author, hash, message, short_hash, time })
} }
} }
#[derive(Serialize)] #[derive(Serialize)]
struct GitEntry { struct GitEntry {
committer: String, last_commit: GitCommit,
last_commit: String,
last_commit_short: String,
last_commit_time: i64,
path: String, path: String,
} }
impl GitEntry { impl GitEntry {
fn new(commit: &Commit, path: String) -> Result<Self, git2::Error> { fn new(commit: GitCommit, path: String) -> Self {
let commit_id = commit.id(); GitEntry { last_commit: commit, path }
Ok(GitEntry {
committer: commit
.committer()
.name()
.unwrap_or("")
.to_owned(),
last_commit: commit_id.to_string(),
last_commit_short: commit.as_object().short_id()?.as_str().unwrap_or("").to_owned(),
last_commit_time: commit.time().seconds(),
path,
})
} }
} }
#[derive(Serialize)] #[derive(Serialize)]
pub struct GitRepo { pub struct GitRepo {
branch: Option<String>,
entries: Vec<GitEntry>, entries: Vec<GitEntry>,
last_commit: GitCommit, last_commit: GitCommit,
name: String, name: String,
@ -138,9 +128,12 @@ impl GitRepo {
.to_vec() .to_vec()
)?; )?;
let last_commit = GitCommit::try_from(repo.head()?.peel_to_commit()?)?; let head = repo.head()?;
let branch = head.shorthand().map(|f| f.to_owned());
Ok(GitRepo { entries, last_commit, name, owner }) let last_commit = GitCommit::try_from(head.peel_to_commit()?)?;
Ok(GitRepo { branch, entries, last_commit, name, owner })
} }
fn get_remote_repo() { // for AP repos fn get_remote_repo() { // for AP repos
@ -165,7 +158,9 @@ impl GitRepo {
_ => unreachable!(), _ => unreachable!(),
}; };
let diff = repo.diff_tree_to_tree(prev_tree.as_ref(), Some(&tree), None)?; let diff = repo
.diff_tree_to_tree(prev_tree.as_ref(), Some(&tree), None)?;
for delta in diff.deltas() { for delta in diff.deltas() {
if let Some(file_path) = delta.new_file().path() { if let Some(file_path) = delta.new_file().path() {
let p = String::from_utf8(file_path let p = String::from_utf8(file_path
@ -174,7 +169,8 @@ impl GitRepo {
.as_encoded_bytes() .as_encoded_bytes()
.to_vec() .to_vec()
)?; )?;
entries.push(GitEntry::new(&commit, p)?); let c = GitCommit::try_from(commit.clone())?;
entries.push(GitEntry::new(c, p));
} }
} }
} }