#!/usr/bin/env python3 """diff-detail.py -- the actual changed lines behind a revert row (UTF-8 safe, written to a file). `reversion-audit.py` reports counts; the route's central uncertainty is whether the flags matter, so the changes themselves have to be read, not counted. Two normalisations are printed side by side: raw -- line-for-line unified diff of the served bytes against the discarded blob; ws -- the same after collapsing whitespace runs and dropping blank lines; ws+link -- additionally collapsing a markdown link `[text](target)` to `text`, which is what a link-stripping mirror pass would change (return #1402 read corner-correlation.md as pure reflow for exactly this reason). usage: python3 diff-detail.py --state [ ...] """ import argparse import difflib import hashlib import json import os import re import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import sahget # noqa: E402 BASE = "https://solveathome.org/projects/twin-primes" LINK = re.compile(r"\[([^\]]*)\]\([^)]*\)") def ws(text): out = [] for ln in (text or "").splitlines(): s = re.sub(r"\s+", " ", ln).strip() if s: out.append(s) return out def ws_link(text): return [LINK.sub(r"\1", s) for s in ws(text)] def diffcount(x, y): return sum(1 for l in difflib.unified_diff(x, y, n=0, lineterm="") if l[:1] in "+-" and not l.startswith(("+++", "---"))) def main(): ap = argparse.ArgumentParser() ap.add_argument("--state", required=True) ap.add_argument("paths", nargs="+") ap.add_argument("--out", default=None) ap.add_argument("--show", type=int, default=40) a = ap.parse_args() g = sahget.Get(a.state) lines = [] for path in a.paths: code, d, _ = g.get("%s/history/%s" % (BASE, path)) h = json.loads(d) vs = h.get("versions") or [] scode, sdata, _ = g.get("%s/docs/%s" % (BASE, path)) served = sdata.decode("utf-8", "replace") lines.append("=" * 100) lines.append("%s served sha %s versions %d" % (path, hashlib.sha256(sdata).hexdigest()[:12], len(vs))) for i, v in enumerate(vs): lines.append(" v%d sha=%-12s return=%-6s created=%s verified=%s" % (v.get("version"), (v.get("content_sha") or "")[:12], v.get("return_id"), v.get("created_at"), v.get("verified_by"))) for i, v in enumerate(vs): if i == 0 or not v.get("return_id"): continue # only the discarded (return-bearing) revisions matter bcode, bdata, _ = g.get("https://solveathome.org/files/%s" % v["content_sha"]) if bcode != 200: lines.append(" v%s blob HTTP %s" % (v.get("version"), bcode)) continue disc = bdata.decode("utf-8", "replace") lines.append("-" * 90) lines.append(" served vs v%s (return %s): raw %d, ws %d, ws+link %d" % (v.get("version"), v.get("return_id"), diffcount(served.splitlines(), disc.splitlines()), diffcount(ws(served), ws(disc)), diffcount(ws_link(served), ws_link(disc)))) lines.append(" summaries: served-v%s=%r" % (vs[i].get("version"), (v.get("summary") or "")[:80])) shown = 0 for l in difflib.unified_diff(ws(served), ws(disc), n=0, lineterm="", fromfile="served(ws)", tofile="discarded(ws)"): if l[:1] in "+-" and not l.startswith(("+++", "---")): lines.append(" " + l[:200]) shown += 1 if shown >= a.show: lines.append(" ... (truncated)") break out = a.out or os.path.join(HERE, "diff-detail.txt") with open(out, "w", encoding="utf-8", newline="\n") as fh: fh.write("\n".join(lines) + "\n") print(json.dumps({"wrote": out, "lines": len(lines)}, indent=1)) if __name__ == "__main__": sys.exit(main())