#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Fetch the pinned source trees into reference/src/ (never committed). ``scripts/pinned_repos.json`` lists each repository with its commit, its sparse paths and a ``fetch`` field: entries whose ``fetch`` is not ``"yes"`` are pinned but not fetched (the note says why). Each fetched tree is a shallow, blob-filtered clone checked out at the pinned commit, sparse where the entry lists paths. python -I scripts/fetch_references.py # fetch what is missing python -I scripts/fetch_references.py --verify # check commits, fetch nothing Fetched programs are data: nothing here builds or runs them. """ from __future__ import annotations import argparse import json import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] REPOS = ROOT / "scripts" / "pinned_repos.json" def git(*args: str, cwd: Path | None = None) -> str: result = subprocess.run(["git", *args], cwd=cwd, capture_output=True, text=True, check=False) if result.returncode: raise OSError(f"git {' '.join(args)}: {result.stderr.strip()}") return result.stdout.strip() def fetch(entry: dict) -> None: target = ROOT / entry["path"] target.mkdir(parents=True) git("init", "-q", cwd=target) git("remote", "add", "origin", entry["url"], cwd=target) if entry["sparse"]: git("sparse-checkout", "init", "--no-cone", cwd=target) git("sparse-checkout", "set", "--no-cone", *entry["sparse"], cwd=target) git("fetch", "-q", "--depth", "1", "--filter=blob:none", "origin", entry["rev"], cwd=target) git("checkout", "-q", "FETCH_HEAD", cwd=target) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--verify", action="store_true", help="check what is present; fetch nothing") args = parser.parse_args() problems = 0 for entry in json.loads(REPOS.read_text(encoding="utf-8")): target = ROOT / entry["path"] if entry.get("fetch") != "yes": print(f"pinned, not fetched: {entry['path']} ({entry.get('fetch')})") continue if target.exists(): head = git("rev-parse", "HEAD", cwd=target) if head != entry["rev"]: print(f"ERROR: {entry['path']} is at {head}, pinned {entry['rev']}") problems += 1 else: print(f"ok: {entry['path']} at {head[:12]}") continue if args.verify: print(f"missing: {entry['path']}") problems += 1 continue fetch(entry) print(f"fetched {entry['path']} at {entry['rev'][:12]}") print("all pins verified" if not problems else f"{problems} problem(s)") return 1 if problems else 0 if __name__ == "__main__": sys.exit(main())