git.rs: refactors code

This commit is contained in:
2026-04-23 20:38:17 -06:00
parent 44418909ff
commit a2ac39db43

View File

@@ -1,9 +1,28 @@
/*
* Copyright (c) 2026 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This file is part of Mintee.
*
* Mintee is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Mintee is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Mintee. If not, see https://www.gnu.org/licenses/.
*/
use std::error::Error;
use gix::{
ObjectId,
Repository,
diff::object::bstr::BStr,
Tree,
object::tree::EntryKind,
revision::walk::Sorting,
};
@@ -21,40 +40,24 @@ struct Entry {
path: String,
}
pub fn repo_to_context(r: Repository) -> Result<Context, Box<dyn Error>> {
fn get_entries(tree: Tree) -> Result<Vec<Entry>, Box<dyn Error>> {
let mut entries = Vec::new();
let branches = r.clone().branch_names()
.iter()
.map(|x| x.to_owned().to_owned())
.collect::<Vec<_>>();
let tree = r.rev_parse_single("@")?.object()?.peel_to_tree()?;
/* replace with configurable branch name when we have a database */
let current_branch = r.head()?.referent_name().unwrap().to_string();
let order = Sorting::ByCommitTime(CommitTimeOrder::NewestFirst);
for e in tree.iter() {
let entry = e.unwrap();
let class = match entry.kind() {
EntryKind::Tree => "directory",
EntryKind::Blob => "file",
_ => "",
}.to_owned();
let path = entry.filename().to_string();
let mut last_commit = String::new();
let mut last_commit_message = String::new();
let mut last_commit_time = String::new();
for i in r
.head_commit()?
.ancestors()
.sorting(Sorting::ByCommitTime(CommitTimeOrder::NewestFirst))
.all()? {
let commit = i?.id().object()?.peel_to_commit()?;
let class = match entry.kind() {
EntryKind::Tree | EntryKind::Commit => "directory",
_ => "file",
}.to_owned();
for i in tree.repo.head_commit()?.ancestors().sorting(order).all()? {
let commit = i?.id().object()?.peel_to_commit()?;
let path_slice: &[u8] = path.as_ref();
if commit.tree()?.lookup_entry([path_slice])?.is_some() {
@@ -73,9 +76,46 @@ pub fn repo_to_context(r: Repository) -> Result<Context, Box<dyn Error>> {
});
}
let dir = r.git_dir();
Ok(entries)
}
pub fn repo_to_context(r: Repository) -> Result<Context, Box<dyn Error>> {
let branches = r.clone().branch_names()
.iter()
.map(|x| x.to_owned().to_owned())
.collect::<Vec<_>>();
/* @ is HEAD */
let tree = r.rev_parse_single("@")?.object()?.peel_to_tree()?;
/* replace with configurable branch name when we have a database */
let current_branch = r.head()?.referent_name().unwrap().shorten().to_string();
let entries = get_entries(tree)?;
let mut readme_content = String::new();
/*
/* TODO: determine what file to use as README better */
let readme_candidates = entries.iter().filter(|e| {
match e.path.as_str().to_uppercase().as_str() {
"README" => true,
"README.MD" => true,
_ => false,
}
});
if let Some(readme) = readme_candidates.get(0) {
/* unwrapping is ok because readme exists in Tree */
readme_content = tree.find_entry(readme).unwrap();
todo!("convert Entry into BlobRef to get data")
}
*/
let dir = r.common_dir();
let name = dir.file_name().unwrap().to_str().unwrap();
let directory = dir.as_os_str().to_str().unwrap();
let mut ctx = Context::new();
@@ -84,6 +124,7 @@ pub fn repo_to_context(r: Repository) -> Result<Context, Box<dyn Error>> {
ctx.insert("site", "TiB.");
ctx.insert("notif_count", "");
ctx.insert("ticket_count", "(47)");
ctx.insert("directory", "");
ctx.insert("readme_content", "test readme");
@@ -92,7 +133,6 @@ pub fn repo_to_context(r: Repository) -> Result<Context, Box<dyn Error>> {
ctx.insert("owner", "anon");
ctx.insert("branch", &current_branch);
ctx.insert("repo", name);
ctx.insert("directory", directory);
ctx.insert("entries", &entries);
Ok(ctx)