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, pub body: String, } pub fn panels(cfg: &Config, db: &Db, c: &CandidateRow, base: Option<&ReleaseRow>) -> Result> { 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)); let old_recipe = recipe_of(db, base.candidate_id.and_then(|cid| { candidate_snapshot_id(db, cid) })); let new_recipe = recipe_of(db, Some(c.snapshot_id)); if let (Some(old), Some(new)) = (&old_recipe, &new_recipe) { out.push(source_delta(old, new)); out.push(dependency_delta(old, new)); out.push(build_env_delta(old, new)); out.push(install_delta(old, new)); } } else { 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 recipe_of(db: &Db, snapshot_id: Option) -> Option { let id = snapshot_id?; let snap = db::snapshot_by_id(db, id).ok().flatten()?; serde_json::from_str(&snap.srcinfo_json).ok() } fn candidate_snapshot_id(db: &Db, candidate_id: i64) -> Option { db::candidate_snapshot_id(db, candidate_id).ok().flatten() } fn source_delta(old: &Recipe, new: &Recipe) -> Panel { let mut body = String::new(); for s in &new.source { if !old.source.contains(s) { body.push_str(&format!("+ {s}\n")); } } for s in &old.source { if !new.source.contains(s) { body.push_str(&format!("- {s}\n")); } } let old_hosts = crate::urlutil::hosts(&old.source); let new_hosts = crate::urlutil::hosts(&new.source); for h in new_hosts.difference(&old_hosts) { body.push_str(&format!("! new source host: {h}\n")); } for h in old_hosts.difference(&new_hosts) { body.push_str(&format!("! source host gone: {h}\n")); } for (i, sum) in new.sha256sums.iter().enumerate() { if sum.eq_ignore_ascii_case("SKIP") { match old.sha256sums.get(i) { Some(prev) if prev != "SKIP" && !prev.is_empty() => { body.push_str(&format!("! [{i}] checksum removed (was {prev}, now SKIP)\n")) } _ => {} } } } if !old.validpgpkeys.is_empty() && new.validpgpkeys.is_empty() { body.push_str("! validpgpkeys removed\n"); } if new.skipgpcheck && !old.skipgpcheck { body.push_str("! --skippgpcheck added\n"); } Panel { name: "sources", title: "Source delta", body } } fn dependency_delta(old: &Recipe, new: &Recipe) -> Panel { let mut body = String::new(); for (label, o, n) in [ ("depends", &old.depends, &new.depends), ("makedepends", &old.makedepends, &new.makedepends), ("checkdepends", &old.checkdepends, &new.checkdepends), ("optdepends", &old.optdepends, &new.optdepends), ("provides", &old.provides, &new.provides), ("conflicts", &old.conflicts, &new.conflicts), ] { let oset: std::collections::BTreeSet = crate::pkgver::dep_names(o).into_iter().collect(); let nset: std::collections::BTreeSet = crate::pkgver::dep_names(n).into_iter().collect(); for d in nset.difference(&oset) { body.push_str(&format!("+ {label}: {d}\n")); } for d in oset.difference(&nset) { body.push_str(&format!("- {label}: {d}\n")); } } if !old.checkdepends.is_empty() && new.checkdepends.is_empty() { body.push_str("! tests will no longer run\n"); } Panel { name: "deps", title: "Dependency delta", body } } fn build_env_delta(old: &Recipe, new: &Recipe) -> Panel { let mut body = String::new(); for o in &new.options { if !old.options.contains(o) { body.push_str(&format!("+ options: {o}\n")); } } for o in &old.options { if !new.options.contains(o) { body.push_str(&format!("- options: {o}\n")); } } for a in &new.arch { if !old.arch.contains(a) { body.push_str(&format!("+ arch: {a}\n")); } } for a in &old.arch { if !new.arch.contains(a) { body.push_str(&format!("- arch: {a}\n")); } } if old.pkgbase != new.pkgbase { body.push_str(&format!("! pkgbase renamed: {} -> {}\n", old.pkgbase, new.pkgbase)); } let old_set: std::collections::BTreeSet = old.pkgnames.iter().cloned().collect(); let new_set: std::collections::BTreeSet = new.pkgnames.iter().cloned().collect(); for p in new_set.difference(&old_set) { body.push_str(&format!("+ split package: {p}\n")); } for p in old_set.difference(&new_set) { body.push_str(&format!("- split package: {p}\n")); } Panel { name: "buildenv", title: "Build-env delta", body } } fn install_delta(old: &Recipe, new: &Recipe) -> Panel { let mut body = String::new(); match (&old.install, &new.install) { (None, Some(f)) => body.push_str(&format!("+ install script added: {f}\n")), (Some(f), None) => body.push_str(&format!("- install script removed: {f}\n")), (Some(a), Some(b)) if a != b => body.push_str(&format!("! install script changed: {a} -> {b}\n")), _ => {} } if crate::recipe::bodies_changed(&old.pkgbuild, &new.pkgbuild) { body.push_str("! build()/package() bodies changed — this is code that runs here and on every installer\n"); } Panel { name: "install", title: "Install-script delta", body } } 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")); 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"); } } 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 = base.digests(); let new: Vec = 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 && !n.is_vcs() { ", sha256sums=SKIP" } else if n.is_vcs() { ", pinned commit" } 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 = base.pins(); let new: Vec = 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() } } 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 } pub fn diff_digest(ps: &[Panel]) -> String { crate::hash::hex_str(&render_text(ps)) } pub fn full_recipe_text(db: &Db, c: &CandidateRow) -> Result { 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) } pub fn render_and_store(db: &Db, cfg: &Config, c: &CandidateRow) -> Result { let base = match c.base_release_id { Some(id) => db::release_by_id(db, id)?, None => None, }; let ps = panels(cfg, db, c, base.as_ref())?; let digest = diff_digest(&ps); crate::review::candidates::set_diff_digest(db, c.id, &digest)?; Ok(digest) } pub fn render_current(db: &Db, cfg: &Config, c: &CandidateRow) -> Result<(String, String)> { let base = match c.base_release_id { Some(id) => db::release_by_id(db, id)?, None => None, }; let ps = panels(cfg, db, c, base.as_ref())?; Ok((render_text(&ps), diff_digest(&ps))) }