added commit information to repository page
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
use std::error::Error;
|
||||
|
||||
use gix::{ Repository, object::tree::EntryKind };
|
||||
use gix::{
|
||||
ObjectId,
|
||||
Repository,
|
||||
diff::object::bstr::BStr,
|
||||
object::tree::EntryKind,
|
||||
revision::walk::Sorting,
|
||||
};
|
||||
use gix_traverse::commit::simple::CommitTimeOrder;
|
||||
use serde::Serialize;
|
||||
use tera::Context;
|
||||
use time_format::format_iso8601_utc;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Entry {
|
||||
@@ -29,22 +37,39 @@ pub fn repo_to_context(r: Repository) -> Result<Context, Box<dyn Error>> {
|
||||
for e in tree.iter() {
|
||||
let entry = e.unwrap();
|
||||
|
||||
let last_commit = "TODO".to_string();
|
||||
let last_commit_message = "TODO".to_string();
|
||||
let last_commit_time = "1970-01-01T00:00:00".to_string();
|
||||
|
||||
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 path_slice: &[u8] = path.as_ref();
|
||||
|
||||
if commit.tree()?.lookup_entry([path_slice])?.is_some() {
|
||||
last_commit = commit.short_id()?.to_string();
|
||||
last_commit_message = commit.message_raw()?.to_string();
|
||||
last_commit_time = format_iso8601_utc(commit.time()?.seconds)?;
|
||||
}
|
||||
}
|
||||
|
||||
entries.push(Entry {
|
||||
class,
|
||||
last_commit,
|
||||
last_commit_message,
|
||||
last_commit_time,
|
||||
path: entry.filename().to_string(),
|
||||
path,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
use std::error::Error;
|
||||
|
||||
use gix::{ Repository, traverse::tree::Recorder, prelude::* };
|
||||
use serde::Serialize;
|
||||
use tera::Context;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Entry {
|
||||
mode: String,
|
||||
hash: String,
|
||||
path: String,
|
||||
}
|
||||
|
||||
pub fn repo_to_context(r: Repository) -> Result<Context, 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 mut rec = Recorder::default();
|
||||
|
||||
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();
|
||||
|
||||
tree.traverse().breadthfirst(&mut rec)?;
|
||||
|
||||
for e in rec.records.iter() {
|
||||
entries.push(Entry {
|
||||
mode: e.mode.value().to_string(),
|
||||
hash: e.oid.to_string(),
|
||||
path: e.filepath.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let dir = r.git_dir();
|
||||
let name = dir.file_name().unwrap().to_str().unwrap();
|
||||
let directory = dir.as_os_str().to_str().unwrap();
|
||||
|
||||
let mut ctx = Context::new();
|
||||
|
||||
/* stubs til we have a real database */
|
||||
ctx.insert("user", "anon");
|
||||
ctx.insert("site", "TiB.");
|
||||
ctx.insert("notif_count", "");
|
||||
ctx.insert("ticket_count", "(47)");
|
||||
|
||||
ctx.insert("readme_content", "test readme");
|
||||
|
||||
ctx.insert("branches", &branches);
|
||||
ctx.insert("entries", &entries);
|
||||
ctx.insert("owner", "anon");
|
||||
ctx.insert("branch", ¤t_branch);
|
||||
ctx.insert("repo", name);
|
||||
ctx.insert("directory", directory);
|
||||
ctx.insert("entries", &entries);
|
||||
|
||||
Ok(ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user