Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758"""Record the public Git 2.54.0 prepared-rejection/reentry behavior."""import csvimport osfrom pathlib import Pathimport reimport shleximport subprocessimport sysimport tempfile
out = Path(sys.argv[1]).resolve()env = {**os.environ, "GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_NOSYSTEM": "1"}for key in ("GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY", "GIT_COMMON_DIR", "GIT_ALTERNATE_OBJECT_DIRECTORIES"): env.pop(key, None)version = subprocess.check_output(["git", "--version"], env=env, text=True).strip()if not re.fullmatch(r"git version 2\.54\.0(?: \([^\n]*\))?", version): raise SystemExit("reference-transaction oracle requires Git 2.54.0: " + version)with tempfile.TemporaryDirectory(prefix="git-hook-oracle-") as tmp: repo = Path(tmp) env["HOME"] = tmp def git(*args, data=None, check=True): return subprocess.run(["git", "-C", tmp, *args], input=data, env=env, text=True, capture_output=True, timeout=3, check=check) git("init", "--quiet", "--object-format=sha1") old = git("hash-object", "-w", "--stdin", data="old").stdout.strip() new = git("hash-object", "-w", "--stdin", data="next").stdout.strip() name = "refs/tags/reentry" git("update-ref", name, old) trace, marker = repo / "phases", repo / "reentered" peer = ("git -C " + shlex.quote(tmp) + " -c core.hooksPath=/dev/null -c core.filesRefLockTimeout=0 update-ref " + name + " ") hook = repo / ".git/hooks/reference-transaction" hook.write_text("#!/bin/sh\nset -eu\ncat >/dev/null\nprintf '%s\\n' \"$1\" >> " + shlex.quote(str(trace)) + "\ncase \"$1\" in\nprepared) exit 1;;\naborted)\n" + peer + new + " " + old + "\n" + peer + old + " " + new + "\nprintf 'reentered\\n' >> " + shlex.quote(str(marker)) + "\n;;\nesac\n") hook.chmod(0o755) result = git("update-ref", name, new, old, check=False) phases = trace.read_text() reentered = marker.read_text() if marker.exists() else "" final = git("rev-parse", name).stdout.strip() locks = sum(1 for _ in (repo / ".git/refs").rglob("*.lock")) # Refuse a semantic change instead of silently promoting a different oracle. if (result.returncode, phases, reentered, final, locks) != ( 128, "preparing\nprepared\naborted\n", "reentered\n", old, 0): raise SystemExit("Git 2.54.0 prepared-rejection semantics changed: " + repr((result.returncode, phases, reentered, final, locks))) with (out / "rejection.csv").open("w", newline="") as file: writer = csv.writer(file, lineterminator="\n") writer.writerow(["exit_status", "phases", "reentry", "old_tip", "new_tip", "final_tip", "owned_locks"]) writer.writerow([result.returncode, phases, reentered, old, new, final, locks]) (out / "git-version").write_text(version + "\n")