"""Verify working-tree bytes against the requested commit, without trusting the index.""" from __future__ import annotations import hashlib import os from pathlib import Path import stat import subprocess def verify_checkout(root: Path, revision: str) -> str: tree = subprocess.check_output(["git", "ls-tree", "-r", "-z", "--full-tree", revision], cwd=root) if not tree: raise ValueError("target commit has an empty tree") tracked = set() for entry in tree.rstrip(b"\0").split(b"\0"): metadata, raw_name = entry.split(b"\t", 1) mode, kind, expected = metadata.split() name = os.fsdecode(raw_name) tracked.add(name) path = root / name if not path.is_relative_to(root) or ".." in path.parts: raise ValueError("noncanonical committed path") if any(parent.is_symlink() for parent in path.parents if parent != root and parent.is_relative_to(root)): raise ValueError(f"target tree has a substituted parent: {name}") info = path.lstat() if kind != b"blob" or mode not in {b"100644", b"100755", b"120000"}: raise ValueError(f"unsupported committed object: {name}") if mode == b"120000": if not stat.S_ISLNK(info.st_mode): raise ValueError(f"target tree file type changed: {name}") content = os.fsencode(os.readlink(path)) else: if not stat.S_ISREG(info.st_mode) or bool(info.st_mode & 0o111) != (mode == b"100755"): raise ValueError(f"target tree file type/mode changed: {name}") content = path.read_bytes() # Git SHA-1 blob IDs include the type/length header, not just file bytes. actual = hashlib.sha1(b"blob " + str(len(content)).encode() + b"\0" + content).hexdigest().encode() if actual != expected: raise ValueError(f"target tree differs from requested commit: {name}") # Do not use --exclude-standard: ignored tests/configs can also alter execution. # Include cached paths so newly staged files cannot evade the untracked check. # Execution trees are recursive, but additional root configs/modules are not: # Git's ordinary '*.toml'/'*.py' also match generated evidence/build subtrees. paths = subprocess.check_output([ "git", "ls-files", "--cached", "--others", "-z", "--", "tests", "scripts", "obliteratus", "ci", ".github", ":(top,glob)*.py", ":(top,glob)*.toml", ":(top,glob)*.ini", ":(top,glob)*.cfg", ":(top,glob)*.lock", ":(top,literal).coveragerc", ], cwd=root) for raw_name in paths.rstrip(b"\0").split(b"\0") if paths else []: name = os.fsdecode(raw_name) if name not in tracked: raise ValueError(f"uncommitted test/source/config file: {name}") return hashlib.sha256(tree).hexdigest()