pub mod gc; #[cfg(test)] mod route_tests; mod routes; use std::path::{Path, PathBuf}; use forge_core::Result; use tokio::sync::{Mutex, MutexGuard}; use crate::state::AppState; static LOCK: Mutex<()> = Mutex::const_new(()); pub async fn lock() -> MutexGuard<'static, ()> { LOCK.lock().await } pub fn is_sha256_hex(oid: &str) -> bool { oid.len() == 64 && oid .bytes() .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) } pub fn lfs_root(data_dir: &Path) -> PathBuf { data_dir.join("lfs") } pub fn tmp_dir(data_dir: &Path) -> PathBuf { lfs_root(data_dir).join("tmp") } pub fn object_path(data_dir: &Path, oid: &str) -> PathBuf { match (oid.get(0..2), oid.get(2..4)) { (Some(first), Some(second)) => lfs_root(data_dir).join(first).join(second).join(oid), _ => lfs_root(data_dir).join(oid), } } pub async fn has_object(state: &AppState, repo_id: &str, oid: &str) -> Result> { state.store.lfs_object_size(repo_id, oid).await } #[cfg(test)] mod tests { use super::*; #[test] fn oid_must_be_64_lowercase_hex() { let good = "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393"; assert!(is_sha256_hex(good)); assert!(!is_sha256_hex(&good[..63])); assert!(!is_sha256_hex(&format!("{good}0"))); assert!(!is_sha256_hex(&good.to_uppercase())); assert!(!is_sha256_hex(&format!("{}g", &good[..63]))); assert!(!is_sha256_hex("")); } #[test] fn object_path_fans_out_by_prefix() { let oid = "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393"; let path = object_path(Path::new("/data"), oid); assert_eq!( path, Path::new("/data/lfs/4d/7a").join(oid), "two-level fan-out under lfs/" ); assert_eq!( object_path(Path::new("/data"), "x"), Path::new("/data/lfs/x") ); } }