Diffstat (limited to 'src/git/tree.rs')
1 file changed, 82 insertions(+), 76 deletions(-)
@@ -1,4 +1,3 @@
use crate::prelude::*;
use anyhow::Context;
use gix::objs::tree::EntryKind;
use serde::Serialize;
@@ -6,9 +5,9 @@
#[derive(Clone)]
pub struct TreeEntry {
pub name: String,
pub path: String, // full path from repo root, for building links
pub path: String,
pub kind: EntryKind,
pub size: Option<u64>, // only set for files
pub size: Option<u64>,
pub mode: u32,
pub id: String,
}
@@ -22,76 +21,21 @@
}
pub fn mode_display(&self) -> String {
format_mode(self.mode, &self.kind)
format_entry_mode(self.mode, &self.kind)
}
}
impl<'repo> Commit<'repo> {
pub fn tree(&self, dir_path: &str) -> anyhow::Result<Vec<TreeEntry>> {
let mut entries: Vec<TreeEntry> = Vec::new();
let base_tree = if dir_path.is_empty() {
self.gix.tree().context("failed to get root tree")?
} else {
self.gix
.tree()
.context("failed to get root tree")?
.lookup_entry_by_path(dir_path)
.with_context(|| format!("failed lookup for path: {dir_path}"))?
.with_context(|| format!("path not found: {dir_path}"))?
.object()
.context("failed to load object")?
.try_into_tree()
.context("path did not resolve to a tree")?
};
for entry in base_tree.iter() {
let entry = entry.context("failed to read tree entry")?;
let name = entry.filename().to_string();
let full_path = if dir_path.is_empty() {
name.clone()
} else {
format!("{dir_path}/{name}")
};
let id = entry.oid().to_string();
let mode = entry.mode();
let kind = mode.kind();
let size = if matches!(kind, EntryKind::Blob | EntryKind::BlobExecutable) {
self.gix
.repo
.find_header(entry.oid())
.ok()
.map(|h| h.size())
} else {
None
};
entries.push(TreeEntry {
name,
path: full_path,
kind,
size,
mode: mode.value() as u32,
id,
});
}
Ok(entries)
}
}
#[derive(Clone, Serialize)]
pub struct Blob {
pub id: String,
pub mode: u32,
pub size: u64,
pub lines: Option<Vec<String>>, // None => binary, can't show as text
pub lines: Option<Vec<String>>,
}
impl Blob {
pub fn mode_display(&self) -> String {
format_mode(self.mode, &EntryKind::Blob)
format_entry_mode(self.mode, &EntryKind::Blob)
}
}
@@ -103,9 +47,56 @@
#[derive(Clone, Serialize)]
pub struct Breadcrumb {
pub name: String,
pub path: String, // cumulative path up to and including this segment
pub path: String,
}
pub fn read_entries(base_tree: &gix::Tree<'_>, dir_path: &str) -> anyhow::Result<Vec<TreeEntry>> {
let mut entries = Vec::new();
for entry in base_tree.iter() {
let entry = entry.context("failed to read tree entry")?;
let name = entry.filename().to_string();
let path = if dir_path.is_empty() {
name.clone()
} else {
format!("{dir_path}/{name}")
};
let mode = entry.mode();
let kind = mode.kind();
let size = if matches!(kind, EntryKind::Blob | EntryKind::BlobExecutable) {
base_tree
.repo
.find_header(entry.oid())
.ok()
.map(|h| h.size())
} else {
None
};
entries.push(TreeEntry {
name,
path,
kind,
size,
mode: mode.value() as u32,
id: entry.oid().to_string(),
});
}
// Directories first (right under `..`), then alphabetical by name
entries.sort_by(|a, b| {
let a_is_dir = matches!(a.kind, EntryKind::Tree);
let b_is_dir = matches!(b.kind, EntryKind::Tree);
b_is_dir
.cmp(&a_is_dir)
.then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase()))
.then_with(|| a.name.cmp(&b.name))
});
Ok(entries)
}
pub fn build_breadcrumbs(path: &str) -> Vec<Breadcrumb> {
let mut crumbs = Vec::new();
let mut acc = String::new();
@@ -129,27 +120,42 @@
}
match path.rsplit_once('/') {
Some((parent, _)) => Some(parent.to_string()),
None => Some(String::new()), // one level deep -> parent is root
None => Some(String::new()),
}
}
pub fn format_mode(mode: u32, kind: &EntryKind) -> String {
pub fn format_entry_mode(mode: u32, kind: &EntryKind) -> String {
match kind {
EntryKind::Tree => "drwxr-xr-x".to_string(),
EntryKind::Link => "lrwxrwxrwx".to_string(),
EntryKind::Commit => "m---------".to_string(), // submodule / gitlink
EntryKind::Blob | EntryKind::BlobExecutable => {
let perm = mode & 0o777;
let mut s = String::from("-");
for shift in [6, 3, 0] {
let bits = (perm >> shift) & 0o7;
s.push(if bits & 0o4 != 0 { 'r' } else { '-' });
s.push(if bits & 0o2 != 0 { 'w' } else { '-' });
s.push(if bits & 0o1 != 0 { 'x' } else { '-' });
}
s
}
EntryKind::Commit => "m---------".to_string(),
EntryKind::Blob | EntryKind::BlobExecutable => format_rwx('-', mode),
}
}
pub fn format_mode(mode: u32) -> String {
let file_type = match mode & 0o170000 {
0o040000 => 'd',
0o120000 => 'l',
0o160000 => 'm',
0o100000 => '-',
_ => '?',
};
format_rwx(file_type, mode)
}
fn format_rwx(file_type: char, mode: u32) -> String {
let perms = mode & 0o777;
let mut out = String::with_capacity(10);
out.push(file_type);
for shift in [6, 3, 0] {
let bits = (perms >> shift) & 0o7;
out.push(if bits & 0o4 != 0 { 'r' } else { '-' });
out.push(if bits & 0o2 != 0 { 'w' } else { '-' });
out.push(if bits & 0o1 != 0 { 'x' } else { '-' });
}
out
}
fn format_size(bytes: u64) -> String {