use std::collections::HashSet; use std::path::Path; use gix::bstr::ByteSlice; use gix::objs::tree::EntryKind as GixEntryKind; use crate::error::{Error, Result}; const BINARY_PROBE_BYTES: usize = 8000; const MAX_PATH_BYTES: usize = 4096; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum RefKind { Branch, Tag, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct RefInfo { pub name: String, pub full_name: String, pub target: String, pub kind: RefKind, pub committer_time: i64, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum EntryKind { Blob, Executable, Symlink, Tree, Submodule, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct TreeEntry { pub name: String, pub kind: EntryKind, pub oid: String, pub size: Option, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct BlobData { pub oid: String, pub kind: EntryKind, pub size: u64, pub data: Vec, pub truncated: bool, pub is_binary: bool, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct CommitInfo { pub oid: String, pub parents: Vec, pub author_name: String, pub author_email: String, pub author_time: i64, pub committer_name: String, pub committer_email: String, pub committer_time: i64, pub summary: String, pub message: String, } fn gerr(e: impl std::fmt::Display) -> Error { Error::Failed(e.to_string()) } fn open(repo: &Path) -> Result { let options = gix::open::Options::isolated().config_overrides(["core.deltaBaseCacheLimit=16m"]); gix::open_opts(repo, options).map_err(gerr) } fn valid_ref_name(name: &str) -> bool { if name.is_empty() || name.len() > 255 || name == "@" { return false; } if name.starts_with('-') || name.starts_with('/') || name.ends_with('/') || name.ends_with('.') { return false; } if name.contains("..") || name.contains("@{") || name.contains("//") { return false; } if name .chars() .any(|c| c.is_control() || " ~^:?*[\\".contains(c)) { return false; } name.split('/') .all(|seg| !seg.is_empty() && !seg.starts_with('.') && !seg.ends_with(".lock")) } fn valid_hex_prefix(s: &str) -> bool { (4..=64).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_hexdigit()) } fn parse_oid(hex: &str) -> Result { gix::ObjectId::from_hex(hex.as_bytes()) .map_err(|_| Error::InvalidInput(format!("not a full object id: {hex}"))) } fn ref_commit(repo: &gix::Repository, name: &str) -> Result> { for prefix in ["refs/heads/", "refs/tags/"] { let full = format!("{prefix}{name}"); let Some(mut reference) = repo.try_find_reference(full.as_str()).map_err(gerr)? else { continue; }; let id = reference.peel_to_id().map_err(gerr)?.detach(); if is_commit(repo, id)? { return Ok(Some(id)); } } Ok(None) } fn is_commit(repo: &gix::Repository, id: gix::ObjectId) -> Result { Ok(matches!( repo.try_find_header(id).map_err(gerr)?, Some(h) if h.kind() == gix::objs::Kind::Commit )) } pub fn list_refs(repo: &Path) -> Result> { let repo = open(repo)?; let refs = repo.references().map_err(gerr)?; let mut out = Vec::new(); let groups = [ (RefKind::Branch, refs.local_branches().map_err(gerr)?), (RefKind::Tag, refs.tags().map_err(gerr)?), ]; for (kind, iter) in groups { for reference in iter { let mut reference = reference.map_err(gerr)?; let full_name = reference.name().as_bstr().to_str_lossy().into_owned(); let name = reference.name().shorten().to_str_lossy().into_owned(); let id = reference.peel_to_id().map_err(gerr)?.detach(); if !is_commit(&repo, id)? { continue; } let commit = repo.find_commit(id).map_err(gerr)?; let committer_time = commit .decode() .map_err(gerr)? .committer() .map_err(gerr)? .time() .map_err(gerr)? .seconds; out.push(RefInfo { name, full_name, target: id.to_hex().to_string(), kind, committer_time, }); } } Ok(out) } pub fn default_branch_commit(repo: &Path, branch: &str) -> Result> { if !valid_ref_name(branch) { return Ok(None); } let repo = open(repo)?; let full = format!("refs/heads/{branch}"); let Some(mut reference) = repo.try_find_reference(full.as_str()).map_err(gerr)? else { return Ok(None); }; let id = reference.peel_to_id().map_err(gerr)?.detach(); Ok(is_commit(&repo, id)?.then(|| id.to_hex().to_string())) } pub fn peel_to_commit(repo: &Path, oid: &str) -> Result> { let repo = open(repo)?; let Some(object) = repo.try_find_object(parse_oid(oid)?).map_err(gerr)? else { return Ok(None); }; Ok(object .peel_to_commit() .ok() .map(|c| c.id.to_hex().to_string())) } pub fn resolve_commit(repo: &Path, rev: &str) -> Result> { if valid_ref_name(rev) { let repo = open(repo)?; if let Some(id) = ref_commit(&repo, rev)? { return Ok(Some(id.to_hex().to_string())); } if !valid_hex_prefix(rev) { return Ok(None); } return resolve_hex(&repo, rev); } if valid_hex_prefix(rev) { let repo = open(repo)?; return resolve_hex(&repo, rev); } Ok(None) } fn resolve_hex(repo: &gix::Repository, hex: &str) -> Result> { let lower = hex.to_ascii_lowercase(); let prefix = gix::hash::Prefix::from_hex(&lower).map_err(gerr)?; let outcome = repo.objects.lookup_prefix(prefix, None).map_err(gerr)?; match outcome { Some(Ok(id)) if is_commit(repo, id)? && reachable(repo, id)? => { Ok(Some(id.to_hex().to_string())) } _ => Ok(None), } } const VISIBLE_REFS: [&str; 3] = ["refs/heads/", "refs/tags/", "refs/pull/"]; fn visible_tips(repo: &gix::Repository) -> Result> { let refs = repo.references().map_err(gerr)?; let mut tips = Vec::new(); for prefix in VISIBLE_REFS { for reference in refs.prefixed(prefix).map_err(gerr)? { let mut reference = reference.map_err(gerr)?; if let Ok(tip) = reference.peel_to_id() { let tip = tip.detach(); if is_commit(repo, tip)? { tips.push(tip); } } } } Ok(tips) } fn reachable(repo: &gix::Repository, id: gix::ObjectId) -> Result { let tips = visible_tips(repo)?; if tips.contains(&id) { return Ok(true); } if tips.is_empty() { return Ok(false); } for info in repo.rev_walk(tips).all().map_err(gerr)? { if info.map_err(gerr)?.id == id { return Ok(true); } } Ok(false) } pub fn reachable_commits(repo: &Path, oids: &[String]) -> Result> { let mut wanted: HashSet = oids.iter().filter_map(|oid| parse_oid(oid).ok()).collect(); let repo = open(repo)?; let mut found = HashSet::new(); wanted.retain(|id| matches!(repo.try_find_header(*id), Ok(Some(_)))); let tips = visible_tips(&repo)?; if wanted.is_empty() || tips.is_empty() { return Ok(found); } for info in repo.rev_walk(tips).all().map_err(gerr)? { let id = info.map_err(gerr)?.id; if wanted.remove(&id) { found.insert(id.to_hex().to_string()); if wanted.is_empty() { break; } } } Ok(found) } pub fn commit_reachable(repo: &Path, oid: &str) -> Result { let repo = open(repo)?; let id = parse_oid(oid)?; Ok(is_commit(&repo, id)? && reachable(&repo, id)?) } pub fn split_ref_and_path(repo: &Path, rest: &str) -> Result> { let segments: Vec<&str> = rest.split('/').collect(); let repo = open(repo)?; for count in (1..=segments.len()).rev() { let candidate = segments[..count].join("/"); if !valid_ref_name(&candidate) { continue; } if let Some(id) = ref_commit(&repo, &candidate)? { let path = segments[count..].join("/"); if validate_repo_path(&path).is_err() { return Ok(None); } return Ok(Some((candidate, id.to_hex().to_string(), path))); } } let first = segments[0]; if valid_hex_prefix(first) && let Some(oid) = resolve_hex(&repo, first)? { let path = segments[1..].join("/"); if validate_repo_path(&path).is_err() { return Ok(None); } return Ok(Some((first.to_string(), oid, path))); } Ok(None) } fn map_kind(kind: GixEntryKind) -> EntryKind { match kind { GixEntryKind::Tree => EntryKind::Tree, GixEntryKind::Blob => EntryKind::Blob, GixEntryKind::BlobExecutable => EntryKind::Executable, GixEntryKind::Link => EntryKind::Symlink, GixEntryKind::Commit => EntryKind::Submodule, } } fn commit_root<'r>(repo: &'r gix::Repository, commit: &str) -> Result>> { let id = parse_oid(commit)?; if !is_commit(repo, id)? { return Ok(None); } Ok(Some( repo.find_commit(id).map_err(gerr)?.tree().map_err(gerr)?, )) } fn tree_at<'r>( repo: &'r gix::Repository, commit: &str, path: &str, ) -> Result>> { let Some(mut root) = commit_root(repo, commit)? else { return Ok(None); }; if path.is_empty() { return Ok(Some(root)); } let Some(entry) = root.peel_to_entry_by_path(path).map_err(gerr)? else { return Ok(None); }; if entry.mode().kind() != gix::objs::tree::EntryKind::Tree { return Ok(None); } let tree = entry .object() .map_err(gerr)? .try_into_tree() .map_err(gerr)?; Ok(Some(tree)) } pub fn read_tree(repo: &Path, commit: &str, path: &str) -> Result>> { validate_repo_path(path)?; let repo = open(repo)?; let Some(tree) = tree_at(&repo, commit, path)? else { return Ok(None); }; let mut entries = Vec::new(); for entry in tree.iter() { let entry = entry.map_err(gerr)?; let kind = map_kind(entry.mode().kind()); let oid = entry.object_id(); let size = match kind { EntryKind::Tree | EntryKind::Submodule => None, _ => Some(repo.find_header(oid).map_err(gerr)?.size()), }; entries.push(TreeEntry { name: entry.filename().to_str_lossy().into_owned(), kind, oid: oid.to_hex().to_string(), size, }); } entries.sort_by(|a, b| { (a.kind != EntryKind::Tree, a.name.as_bytes()) .cmp(&(b.kind != EntryKind::Tree, b.name.as_bytes())) }); Ok(Some(entries)) } pub fn read_blob( repo: &Path, commit: &str, path: &str, max_bytes: usize, ) -> Result> { blob_at(repo, commit, path, max_bytes, false) } fn blob_at( repo: &Path, commit: &str, path: &str, max_bytes: usize, text_only: bool, ) -> Result> { validate_repo_path(path)?; if path.is_empty() { return Ok(None); } let repo = open(repo)?; let Some(mut root) = commit_root(&repo, commit)? else { return Ok(None); }; let Some(entry) = root.peel_to_entry_by_path(path).map_err(gerr)? else { return Ok(None); }; let kind = map_kind(entry.mode().kind()); let oid = entry.object_id(); match kind { EntryKind::Tree => Ok(None), EntryKind::Submodule => Ok(Some(BlobData { oid: oid.to_hex().to_string(), kind, size: 0, data: Vec::new(), truncated: false, is_binary: false, })), EntryKind::Blob | EntryKind::Executable | EntryKind::Symlink => { if text_only && repo.find_header(oid).map_err(gerr)?.size() > max_bytes as u64 { return Ok(None); } let mut blob = repo.find_blob(oid).map_err(gerr)?; let mut data = blob.take_data(); let size = data.len() as u64; let is_binary = data[..data.len().min(BINARY_PROBE_BYTES)].contains(&0); let truncated = data.len() > max_bytes; if truncated { data.truncate(max_bytes); data.shrink_to_fit(); } Ok(Some(BlobData { oid: oid.to_hex().to_string(), kind, size, data, truncated, is_binary, })) } } } pub fn read_text_blob( repo: &Path, commit: &str, path: &str, max_bytes: usize, ) -> Result> { let Some(blob) = blob_at(repo, commit, path, max_bytes, true)? else { return Ok(None); }; if blob.kind == EntryKind::Submodule || blob.is_binary { return Ok(None); } Ok(String::from_utf8(blob.data).ok()) } fn lossy(bytes: &[u8]) -> String { String::from_utf8_lossy(bytes).into_owned() } pub fn read_commit(repo: &Path, oid: &str) -> Result> { let repo = open(repo)?; let id = parse_oid(oid)?; if !is_commit(&repo, id)? { return Ok(None); } let commit = repo.find_commit(id).map_err(gerr)?; let decoded = commit.decode().map_err(gerr)?; let author = decoded.author().map_err(gerr)?; let committer = decoded.committer().map_err(gerr)?; let message = lossy(commit.message_raw_sloppy()); let summary = message.lines().next().unwrap_or_default().to_string(); Ok(Some(CommitInfo { oid: id.to_hex().to_string(), parents: decoded.parents().map(|p| p.to_hex().to_string()).collect(), author_name: lossy(author.name), author_email: lossy(author.email), author_time: author.time().map_err(gerr)?.seconds, committer_name: lossy(committer.name), committer_email: lossy(committer.email), committer_time: committer.time().map_err(gerr)?.seconds, summary, message, })) } pub fn validate_repo_path(path: &str) -> Result<()> { if path.len() > MAX_PATH_BYTES { return Err(Error::InvalidInput("path too long".into())); } if path.contains('\0') { return Err(Error::InvalidInput("path contains NUL".into())); } if path.is_empty() { return Ok(()); } for seg in path.split('/') { if seg.is_empty() || seg == "." || seg == ".." { return Err(Error::InvalidInput(format!( "invalid path segment in {path:?}" ))); } } Ok(()) }