//! The local AUR git mirror (§5.1). //! //! Clone, don't scrape. The mirror is load-bearing rather than a cache: every //! build materializes its recipe from it at the pinned commit and never asks //! the network for a recipe (§5.1a), so losing it would mean losing the ability //! to reproduce any historical release. 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; /// Result of a cheap poll. `Unchanged` means we did **not** fetch — the poll is /// `git ls-remote`, one request per pkgbase (§5.1). #[derive(Debug, Clone, PartialEq, Eq)] pub enum Sync { /// Remote is gone (deleted from AUR). Gone, /// `ls-remote` matched the local tip; no fetch performed. Unchanged { head: String }, /// The tip moved; we fetched and now have `head`. Fetched { head: String, previous: Option }, /// We had no local mirror yet. Cloned { head: String }, } /// Ensure a mirror exists and is polled. Updates `pkgbase.aur_head`. 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()) } /// A materialized snapshot tree: blob ids for hashing, text bodies for parsing /// and diffing. #[derive(Debug, Clone)] pub struct Tree { /// `(path, git blob id)` for every file in the tree — the input to /// `recipe_hash`. Covers PKGBUILD, .SRCINFO, *.install and shipped patches /// (§3): a patch file is part of the recipe. pub blobs: Vec<(String, String)>, /// Text bodies, keyed by path. Binary files are omitted from this map but /// still counted in `blobs`. 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()) } } /// Read a commit's tree out of the mirror without touching the network. pub fn read_tree(mirror: &Path, commit: &str) -> Result { // §16 mirror divergence: if a pin we are asked to read has vanished, that is // a hard failure, not a silent re-pin. if !git::has_commit(mirror, commit) { return Err(Error::fail_closed(format!( "commit {commit} is missing from the local mirror; history was rewritten" ))); } let blobs = git::ls_tree(mirror, commit)?; let mut files = BTreeMap::new(); for (path, _) in &blobs { let bytes = git::read_blob(mirror, commit, path)?; 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) } /// Raw bytes of one file at a commit — used to hash sources shipped in the AUR /// repo (§5.3 "local file shipped in the AUR repo"). pub fn read_file(mirror: &Path, commit: &str, path: &str) -> Result> { git::read_blob(mirror, commit, path) }