use std::collections::BTreeMap; use std::path::Path; use crate::config::Config; use crate::db::{self, Db}; use crate::error::{Error, Result}; use crate::git; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Sync { Gone, Unchanged { head: String }, Fetched { head: String, previous: Option }, Cloned { head: String }, } pub fn sync_mirror(cfg: &Config, db: &Db, name: &str, url: &str) -> Result { let mirror = cfg.mirror_path(name); let known_head = db::get_pkgbase(db, name)?.and_then(|p| p.aur_head.clone()); if !mirror.exists() { git::clone(url, &mirror)?; let head = git::head(&mirror)?; record(db, name, &head, &mirror)?; return Ok(Sync::Cloned { head }); } let remote = match git::ls_remote_head(url)? { Some(h) => h, None => return Ok(Sync::Gone), }; let local = git::head(&mirror).ok(); if let Some(l) = &local { if *l == remote { return Ok(Sync::Unchanged { head: remote }); } } let head = git::fetch_head(&mirror)?; record(db, name, &head, &mirror)?; Ok(Sync::Fetched { head, previous: known_head }) } fn record(db: &Db, name: &str, head: &str, mirror: &Path) -> Result<()> { let pb = db::get_pkgbase(db, name)?.ok_or_else(|| Error::not_found(format!("pkgbase {name}")))?; db::set_aur_head(&db.writer, pb.id, head, &mirror.display().to_string()) } #[derive(Debug, Clone)] pub struct Tree { pub blobs: Vec<(String, String)>, pub files: BTreeMap, } impl Tree { pub fn recipe_hash(&self) -> String { crate::hash::recipe_hash(&self.blobs) } pub fn get(&self, suffix: &str) -> Option<&String> { self.files .iter() .find(|(k, _)| k.ends_with(suffix)) .map(|(_, v)| v) } pub fn json_list(&self) -> String { serde_json::to_string(&self.blobs).unwrap_or_else(|_| "[]".into()) } } pub fn read_tree(mirror: &Path, commit: &str) -> Result { // One `ls-tree` and one `cat-file --batch` for the whole tree, so the number // of files a recipe ships does not decide how many processes a read costs. // A commit that is gone from the mirror surfaces here, from `ls-tree`, rather // than from a separate `cat-file -e` probe first. let blobs = match git::ls_tree(mirror, commit) { Ok(blobs) => blobs, Err(e) => { let text = e.to_string().to_lowercase(); if text.contains("not a tree object") || text.contains("not a valid object name") { return Err(Error::fail_closed(format!( "commit {commit} is missing from the local mirror; history was rewritten" ))); } return Err(e); } }; let oids: Vec = blobs.iter().map(|(_, sha)| sha.clone()).collect(); let contents = git::cat_file_batch(mirror, &oids)?; let mut files = BTreeMap::new(); for (path, sha) in &blobs { let bytes = contents .get(sha) .ok_or_else(|| Error::not_found(format!("{path} not in mirror")))?; if bytes.len() > 4 * 1024 * 1024 || !looks_textual(bytes) { continue; } files.insert(path.clone(), String::from_utf8_lossy(bytes).into_owned()); } Ok(Tree { blobs, files }) } fn looks_textual(b: &[u8]) -> bool { !b[..b.len().min(512)].contains(&0) } pub fn read_file(mirror: &Path, commit: &str, path: &str) -> Result> { git::read_blob(mirror, commit, path) }