"""Resolve commit-SHA references -> analysis/data/sha_refs.tsv. Added at the human's prompt during Phase 2: the 2026-08-07 history rewrite gave 251 commits new SHAs, and shared/commit-zuordnung-2026-08-07.md is the mapping list meant to keep older references resolvable. This script tests whether that actually holds. Two row kinds share one file: doc-reference a SHA cited in a management-repo doc mapping-entry an (old -> new) pair from the mapping table itself Scope note: only management-repo docs are scanned for references. The component repos are forks whose upstream docs cite thousands of upstream SHAs that were never part of this project's history; including them would bury the signal. """ import re from common import ( DOC_SUFFIXES, MGMT_REPO, cell, git, repos, tracked_files, write_tsv, ) MAPPING_DOC = "shared/commit-zuordnung-2026-08-07.md" # Section heading -> repo name used elsewhere in the analysis. SECTION_REPO = { "gitops": "axion1337.chat-gitops", "management": "management", "ThreadNet-Web": "ThreadNet-Web", "threadnet-call": "threadnet-call", } MAPPING_ROW = re.compile(r"^\|\s*`([0-9a-f]{7,40})`\s*\|\s*`([0-9a-f]{7,40})`\s*\|") SECTION = re.compile(r"^##\s+(.+?)\s+—\s+(\d+)\s+Commits\s*$") # A SHA-shaped token: 7-40 hex chars, mixing letters and digits so that # plain numbers and hex-free words do not qualify. SHA_TOKEN = re.compile(r"(?{new}", status, repo if status.startswith("ok") else "", note]) # 2. Every SHA cited in a management doc. docs = [f for f in tracked_files("management", MGMT_REPO) if any(f.endswith(s) for s in DOC_SUFFIXES)] for src in sorted(docs): text = (MGMT_REPO / src).read_text(encoding="utf-8") for lineno, line in enumerate(text.splitlines(), start=1): if src == MAPPING_DOC and MAPPING_ROW.match(line): continue # already covered as mapping-entry rows for sha in SHA_TOKEN.findall(line): hits = resolve(sha, index) if hits: status = "resolves" note = f"found in {','.join(hits)}" elif sha in old_to_new: repo, new = old_to_new[sha] if repo in known_repos and resolve(new, {repo: index[repo]}): status = "resolves-via-mapping" note = f"pre-rewrite SHA; mapping -> {new} in {repo}" hits = [repo] else: status = "mapping-stale" note = f"mapping points to {new} in {repo}, which does not exist" else: status = "orphan" note = "resolves in no analysed repo and is not in the mapping table" rows.append(["doc-reference", "management", src, lineno, sha, status, ",".join(hits), cell(line.strip())[:160]]) rows.sort(key=lambda r: (r[0], r[2], r[3] if r[3] != "" else 0, r[4])) write_tsv( "sha_refs.tsv", ["kind", "repo", "path", "line", "sha", "status", "resolved_in", "note"], rows, ) counts = {} for r in rows: counts[(r[0], r[5])] = counts.get((r[0], r[5]), 0) + 1 for key in sorted(counts): print(f" {key[0]:<14} {key[1]:<22} {counts[key]}") if __name__ == "__main__": main()