summaryrefslogtreecommitdiffstats
path: root/src/watch/mirror.rs
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-17 21:19:35 +0000
committeraur-build <aur-build@localhost>2026-11-17 21:19:35 +0000
commitf586d11f6a1711145ba4804787217a1bab58751d (patch)
tree3a8cd7d4a3d4097e315907c63e687bd243e8e686 /src/watch/mirror.rs
parentfd3ecf91686b11feea078531baeddb60e6941116 (diff)
downloadaur-build-f586d11f6a1711145ba4804787217a1bab58751d.tar.gz
aur-build-f586d11f6a1711145ba4804787217a1bab58751d.zip
feat: AUR mirror watcher, recipe parsing, candidate creation
The detection path (§5.1, §5.1a, §5.3, §7.1): * git helpers, all subprocess git in one auditable place. Mirrors are cloned with --mirror and polled with git ls-remote, fetching only when the tip actually moved; --no-prune keeps history we may need forever. * .SRCINFO parsing (authoritative) with a PKGBUILD fallback, and source=() parsing that keeps VCS kind, #commit/#tag/#branch refs and the filename:: form. * recipe_hash over the whole snapshot tree, so a shipped patch is part of the recipe and cannot change silently. * source digest resolution: declared checksums are trusted-as-declared (makepkg enforces them, no bandwidth), local files are hashed from the mirror, and SKIP sources are downloaded and stream-hashed into the content-addressed source cache. Detection always re-fetches SKIP sources — answering from our own cache would defeat drift detection. * candidate creation with base = current_accepted_release, collapse in place while unviewed, supersede only once a human has looked, and a previously rejected identity is not re-raised. * non-publishable change handling (I11): unchanged version string means no review, but never silence — recipe drift and pin drift become notices, and a changed digest on a remote source with an unchanged version becomes a high-severity integrity_alert. Also vercmp + dep-string parsing with the ALPM test vectors, and the approval record with optimistic concurrency (§7.1a) that the CLI and web layers will both call. 16 detection tests against real file:// git fixtures, incl. I2 across three unattended detections and I11 for all three drift kinds. Part of P0 (DESIGN.md §17).
Diffstat (limited to 'src/watch/mirror.rs')
-rw-r--r--src/watch/mirror.rs119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/watch/mirror.rs b/src/watch/mirror.rs
new file mode 100644
index 0000000..989b968
--- /dev/null
+++ b/src/watch/mirror.rs
@@ -0,0 +1,119 @@
+//! 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<String> },
+ /// 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<Sync> {
+ 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<String, String>,
+}
+
+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<Tree> {
+ // §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<Vec<u8>> {
+ git::read_blob(mirror, commit, path)
+}