summaryrefslogtreecommitdiffstats
path: root/src/review/diff.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/review/diff.rs')
-rw-r--r--src/review/diff.rs156
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)
+}