#!/usr/bin/env python3 # -*- coding: utf-8 -*- """verify_comment_only_change.py — 纯注释批次零行为变更机器证明。 对 git 工作区相对 HEAD 的每个改动 .gd(game/core、game/data、game/presentation): 剥离文件头注释区(首个非注释代码行前的全部空行/注释行)后,新旧剩余内容必须逐行一致—— 证明改动只发生在文件头注释区,代码行为零变更。头注清偿/载体换代一类纯注释批的交付验收 配套(code-conventions §1.4.1)。 退出码:0=全部通过,1=存在头注区外的改动。用法:python tools/verify_comment_only_change.py """ import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent def strip_header(lines): """剥掉头注区(首个非注释非空行前的全部空行/注释行),返回剩余行列表。""" i = 0 while i < len(lines): s = lines[i].strip() if s == "" or s.startswith("#"): i += 1 else: break return lines[i:] def main(): out = subprocess.run( ["git", "diff", "--name-only", "HEAD", "--", "game/core", "game/data", "game/presentation"], cwd=ROOT, capture_output=True, text=True, check=True).stdout.split() if not out: print("[verify_comment_only] 无改动文件") return 0 bad = [] for rel in out: rel = rel.replace("\\", "/") old = subprocess.run(["git", "show", "HEAD:" + rel], cwd=ROOT, capture_output=True, text=True, check=True).stdout new = (ROOT / rel).read_text(encoding="utf-8") if strip_header(old.splitlines()) != strip_header(new.splitlines()): bad.append(rel) if bad: for rel in bad: print("[verify_comment_only] FAIL 头注区外内容被改动: " + rel) return 1 print("[verify_comment_only] ok: %d 个改动文件头注区外逐行一致(零行为变更)" % len(out)) return 0 if __name__ == "__main__": sys.exit(main())