diff options
| author | aur-build <aur-build@localhost> | 2026-11-17 21:19:35 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-17 21:19:35 +0000 |
| commit | f586d11f6a1711145ba4804787217a1bab58751d (patch) | |
| tree | 3a8cd7d4a3d4097e315907c63e687bd243e8e686 /src/review/diff.rs | |
| parent | fd3ecf91686b11feea078531baeddb60e6941116 (diff) | |
| download | aur-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/review/diff.rs')
| -rw-r--r-- | src/review/diff.rs | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/review/diff.rs b/src/review/diff.rs new file mode 100644 index 0000000..cfce3fe --- /dev/null +++ b/src/review/diff.rs @@ -0,0 +1,156 @@ +//! Diff rendering (§6). +//! +//! A review is not one diff, it is a panel set, and **every** panel compares what +//! the accepted release recorded against what the candidate records now — never +//! "last seen". The rendered text is hashed into `diff_digest`, which is what +//! makes "we build exactly what was displayed, or nothing" (§7.1a) provable. + +use crate::config::Config; +use crate::db::{self, *}; +use crate::error::Result; +use crate::git; + +#[derive(Debug, Clone)] +pub struct Panel { + pub name: &'static str, + pub title: &'static str, + /// Unified-diff text, or a readable rendering for panels that are not diffs. + pub body: String, +} + +/// Everything a reviewer needs for one candidate. +pub fn panels(cfg: &Config, db: &Db, c: &CandidateRow, base: Option<&ReleaseRow>) -> Result<Vec<Panel>> { + let mut out = Vec::new(); + let snap = db::snapshot_by_id(db, c.snapshot_id)?; + let mirror = std::path::PathBuf::from( + db::get_pkgbase_by_id(db, c.pkgbase_id)? + .and_then(|p| p.mirror_path) + .unwrap_or_else(|| cfg.mirror_path(&c.pkgbase).display().to_string()), + ); + + out.push(version_delta(base, c)); + + if let Some(base) = base { + if let Some(s) = &snap { + let range = format!("{}..{}", base.aur_commit, s.aur_commit); + let body = git::diff(&mirror, &range, &[]).unwrap_or_else(|e| { + format!("(diff unavailable: {e})\n\n--- base {}\n+++ candidate {}", base.aur_commit, s.aur_commit) + }); + out.push(Panel { name: "recipe", title: "Recipe diff", body }); + } + out.push(digest_delta(base, c)); + out.push(pin_delta(base, c)); + } else { + // §7.1b first_introduction: no diff pane. The full recipe is rendered + // readably instead, by the template. + out.push(Panel { + name: "recipe_full", + title: "Full recipe", + body: snap + .map(|s| s.srcinfo_json.clone()) + .unwrap_or_else(|| "(no snapshot)".into()), + }); + } + Ok(out) +} + +fn version_delta(base: Option<&ReleaseRow>, c: &CandidateRow) -> Panel { + let mut body = String::new(); + match base { + Some(b) => { + let (bv, cv) = (b.version(), c.version()); + body.push_str(&format!("{bv} -> {cv}\n")); + let ord = crate::pkgver::vercmp(&bv, &cv); + if b.epoch != c.epoch { + body.push_str(&format!("epoch bump: {} -> {}\n", b.epoch, c.epoch)); + } + if b.pkgrel != c.pkgrel && b.pkgver == c.pkgver { + body.push_str(&format!("pkgrel change: {} -> {}\n", b.pkgrel, c.pkgrel)); + } + if crate::pkgver::is_downgrade(&bv, &cv) { + body.push_str("DOWNGRADE: AUR now offers a lower version than we serve\n"); + } + let _ = ord; + } + None => body.push_str(&format!("first introduction: {}\n", c.version())), + } + Panel { name: "version", title: "Version delta", body } +} + +fn digest_delta(base: &ReleaseRow, c: &CandidateRow) -> Panel { + let mut body = String::new(); + let old: Vec<SourceDigest> = base.digests(); + let new: Vec<SourceDigest> = c.digests(); + for n in &new { + match old.iter().find(|o| o.index == n.index) { + Some(o) if o.digest != n.digest => { + body.push_str(&format!( + "! [{}] {} {} -> {}\n", + n.index, n.url, short(&o.digest), short(&n.digest) + )); + } + Some(_) => body.push_str(&format!(" [{}] {} unchanged\n", n.index, n.url)), + None => body.push_str(&format!( + "+ [{}] {} NEW ({}{})\n", + n.index, + n.url, + n.kind, + if n.skip_declared { ", sha256sums=SKIP" } else { "" } + )), + } + } + for o in &old { + if !new.iter().any(|n| n.index == o.index) { + body.push_str(&format!("- [{}] {} REMOVED\n", o.index, o.url)); + } + } + Panel { name: "digests", title: "Source digest delta", body } +} + +fn pin_delta(base: &ReleaseRow, c: &CandidateRow) -> Panel { + let mut body = String::new(); + let old: Vec<UpstreamPin> = base.pins(); + let new: Vec<UpstreamPin> = c.pins(); + if old.is_empty() && new.is_empty() { + body.push_str("no VCS sources\n"); + } + for n in &new { + match old.iter().find(|o| o.index == n.index) { + Some(o) if o.commit != n.commit => { + body.push_str(&format!( + "! [{}] {} {} -> {} (branch {})\n", + n.index, n.url, short(&o.commit), short(&n.commit), n.branch.as_deref().unwrap_or("?") + )); + } + Some(_) => body.push_str(&format!(" [{}] {} unchanged\n", n.index, n.url)), + None => body.push_str(&format!("+ [{}] {} at {}\n", n.index, n.url, short(&n.commit))), + } + } + Panel { name: "upstream", title: "Upstream delta", body } +} + +fn short(s: &str) -> String { + if s.len() > 12 { s[..12].to_string() } else { s.to_string() } +} + +/// Flatten the rendered panels into one canonical string. +pub fn render_text(ps: &[Panel]) -> String { + let mut s = String::new(); + for p in ps { + s.push_str(&format!("### {}\n{}\n", p.title, p.body)); + } + s +} + +/// `diff_digest`: hash of the exact rendered diff the human was shown (§7.2). +pub fn diff_digest(ps: &[Panel]) -> String { + crate::hash::hex_str(&render_text(ps)) +} + +/// The recipe text a reviewer reads for a first introduction (§7.1b). +pub fn full_recipe_text(db: &Db, c: &CandidateRow) -> Result<String> { + let snap = db::snapshot_by_id(db, c.snapshot_id)?; + let snap = snap.ok_or_else(|| crate::error::Error::not_found("snapshot missing"))?; + let r: Recipe = serde_json::from_str(&snap.srcinfo_json).unwrap_or_default(); + Ok(r.pkgbuild) +} |
