fix(gates): close the BOM gate hole that let new files skip the check
The BOM gate claimed to stop BOMs from being stripped, but it only ever inspected files that were already tracked AND already had a BOM. A new file under a BOM-listed directory could be born without one and never be noticed, because the scan never returned it and, even if it had, the "no HEAD version" branch skipped it. Editing a listed file and losing its BOM is a real accident that already happened once; creating one without a BOM was the same accident with no safety net at all. Measured before and after on the same untracked file with no BOM: before: checked 101 files, no drift, exit 0 after: FAIL, names the file, exit 1 The count of 101 was itself the symptom: with one more file present the honest number is 102. The old code was not "silently passing a clean tree", it was not looking. The scan now includes untracked files, and a listed file with no HEAD version must carry a BOM. Files that are tracked without a BOM keep their historical pass so this does not flood existing code with failures; instead the two that were listed but had no BOM were given one so the list means what it says. That exposed a conflict nobody had hit yet. The font file is generated, and the generator rewrites it from scratch on every run, so a hand-added BOM survived until the next regeneration and then vanished. The BOM gate and the generated-fresh gate could not both pass. The generator now puts the BOM back as its last step, idempotently, so the two gates agree and the whole UI directory keeps its uniform encoding. Font content is untouched: everything after the first three bytes is byte-identical. The menu generator gains a real read-only mode. It previously had no options at all and always rewrote its outputs; the arrows it printed were accurate. That made it easy to mistake for a checker, since an unchanged tree produces identical bytes and looks untouched. --check now reports up to date or stale without writing anything, and the registration tables are checked the same way. Default behaviour and output are unchanged. The telemetry page header no longer restates the board's input hardware facts. They live in the IOBOARD README, which is where the board's electrical behaviour belongs; the page keeps only what it alone can say: that the output rows show the shadow register rather than contact feedback, why an invalid sample draws -- instead of 分, and that these twelve rows are not in the point table. One fact, one place. The team rule for experiments now requires the file hash, the located line number, and both runs. A report once cited a line number that did not exist in the final file, which left no way to confirm which version had actually been tested.
This commit is contained in:
+63
-14
@@ -21,8 +21,8 @@
|
||||
这正是目的:名单散在三处而无人同步,等于没有名单。
|
||||
用法: .venv/Scripts/python.exe scripts/check_bom_drift.py [文件名...] [--all] [--selftest]
|
||||
不带文件名也不带 --all 时(pre-commit 的常规形态)只核传进来的文件;--all 核全仓
|
||||
跟踪文件(人工审计/排查用,比 HEAD 全量比一遍慢,但不依赖"改没改过");--selftest
|
||||
在临时 git 仓库里自证四条判定(含票面要求的两条边界)。
|
||||
跟踪**及未跟踪**文件(人工审计/排查用,比 HEAD 全量比一遍慢,但不依赖"改没改过");--selftest
|
||||
在临时 git 仓库里自证六条判定(含新增文件必须带 BOM、名单外不越权、存量两方向)。
|
||||
退出码: 0 = 通过;1 = 有 BOM 漂移或名单不一致;2 = 用法或环境错误(不在 git 仓库内等)
|
||||
创建日期:2026-10-25(道E task-20)
|
||||
"""
|
||||
@@ -36,6 +36,11 @@ import tempfile
|
||||
BOM = bytes((0xEF, 0xBB, 0xBF))
|
||||
|
||||
# 名单项:(路径, 匹配方式),prefix = 目录内全部文件,eq = 恰好这一个文件
|
||||
# 名单的语义是"这些文件**必须**带 BOM",不是"这些文件曾经带过 BOM":新增文件按此判
|
||||
# (见 check_paths 的 hp is None 分支),存量文件若在 HEAD 里就没有 BOM 仍按历史状态放行,
|
||||
# 但那属于名单登记不实,应逐个补齐后让名实相符——2026-11-16 补的是 App/cfg/cfg_file.c
|
||||
# (ARMCC 编译的手写源文件、含数百行中文注释,正是 BOM 铁律要保护的对象,HEAD 无 BOM 纯属
|
||||
# 历史疏漏)。补完后本名单内不再存在"登记了却没 BOM"的存量文件,故此处不设豁免名单。
|
||||
BOM_LIST = [
|
||||
("Drivers/BSP/UI/", "prefix"),
|
||||
("App/task/ui_task.c", "eq"),
|
||||
@@ -104,7 +109,17 @@ def check_paths(paths):
|
||||
continue
|
||||
hp = head_prefix(p)
|
||||
if hp is None:
|
||||
continue # 新增文件:HEAD 无 BOM 可谈,不误报(票面边界①)
|
||||
# 新增文件(HEAD 里没有):名单内的就该有 BOM。这一支原先直接 continue,
|
||||
# 于是"新建文件忘 BOM"永远拦不住——Scripts 自查实测:在 Drivers/BSP/UI/
|
||||
# 造一个无 BOM 的未跟踪文件,--all 照样报"无漂移"。那正是本门禁最贵的一类
|
||||
# 漏检:新建时没人改到它,等下一次有人编辑才炸,且炸在 ARMCC 的假语法错上。
|
||||
n += 1
|
||||
wp = work_prefix(p)
|
||||
if wp is None:
|
||||
continue # 已在工作区删除:不是本门禁的事
|
||||
if wp != BOM:
|
||||
bad.append(p)
|
||||
continue
|
||||
n += 1
|
||||
if hp != BOM:
|
||||
continue # HEAD 本来就没 BOM:不报(票面边界②)
|
||||
@@ -117,10 +132,21 @@ def check_paths(paths):
|
||||
|
||||
|
||||
def tracked_files():
|
||||
data = git_run(["ls-files", "-z"])
|
||||
"""扫描全集 = 已跟踪 + 未跟踪(不含 gitignore)。
|
||||
|
||||
只用 ls-files 会漏掉"新建还没 add"的文件,而那恰恰是 BOM 门禁最该看住的时刻:
|
||||
新建时忘 BOM 没人会被绊到,等下一次有人编辑该文件才炸,且炸在 ARMCC 的假语法错上
|
||||
(错的位置和内容都不指向真因)。--others --exclude-standard 把未跟踪但非 gitignore
|
||||
的文件一并纳入,使 --all 与"提交后会被跟踪的集合"一致。
|
||||
"""
|
||||
data = git_run(["ls-files", "-z", "--cached", "--others", "--exclude-standard"])
|
||||
if data is None:
|
||||
return []
|
||||
return [x for x in data.decode("utf-8", "replace").split(chr(0)) if x]
|
||||
seen = []
|
||||
for x in data.decode("utf-8", "replace").split(chr(0)):
|
||||
if x and x not in seen:
|
||||
seen.append(x)
|
||||
return seen
|
||||
|
||||
|
||||
def list_crosscheck():
|
||||
@@ -170,9 +196,11 @@ def list_crosscheck():
|
||||
|
||||
|
||||
def report(bad):
|
||||
print("[FAIL] 以下文件的 BOM 被剥掉了(HEAD 有 EF BB BF,工作区没有):")
|
||||
print("[FAIL] 以下名单内文件的 BOM 缺失或被剥掉(工程约定要求 EF BB BF 开头):")
|
||||
for p in bad:
|
||||
print(" " + p)
|
||||
print(" 两种情况都算:① 已入库带 BOM 的文件被编辑工具剥掉了;② 名单内**新建**文件")
|
||||
print(" 压根没带 BOM(HEAD 里没有它,但进了名单目录就必须带)。")
|
||||
print(" 补救:只把 3 个字节 EF BB BF 补回文件最前面;**不要**整文件重写——")
|
||||
print(" 用 PowerShell/Set-Content 之类整文件重写会按系统默认编码重打中文,")
|
||||
print(" 那会把可修的丢 BOM 变成不可修的 U+FFFD(AGENTS 铁律第 1/2 条)。")
|
||||
@@ -181,7 +209,7 @@ def report(bad):
|
||||
|
||||
|
||||
def selftest():
|
||||
"""在临时 git 仓库里跑四条判定(含票面要求的两条边界),任一不符返回 1。"""
|
||||
"""在临时 git 仓库里跑六条判定(含新增文件、名单外、存量两方向边界),任一不符返回 1。"""
|
||||
ok = True
|
||||
tmp = tempfile.mkdtemp(prefix="bomdrift_")
|
||||
old = os.getcwd()
|
||||
@@ -222,12 +250,19 @@ def selftest():
|
||||
with open(bomf, "wb") as f:
|
||||
f.write(body)
|
||||
|
||||
# 边界①:HEAD 里根本没有的新文件(哪怕在名单目录内)不得误报
|
||||
# 边界①:HEAD 里根本没有的新文件**在名单内**就必须带 BOM(票面边界①,2026-11-16 收紧)
|
||||
# 旧行为是直接放行,正是"新建文件忘 BOM 永远拦不住"的那层漏报。
|
||||
with open("Drivers/BSP/UI/new.c", "wb") as f:
|
||||
f.write(b"/* brand new, no BOM */")
|
||||
bad, n = check_paths(["Drivers/BSP/UI/new.c"])
|
||||
if bad or n != 0:
|
||||
print("[SELFTEST FAIL] 新增无 BOM 文件被误报")
|
||||
if bad != ["Drivers/BSP/UI/new.c"] or n != 1:
|
||||
print("[SELFTEST FAIL] 名单内新增无 BOM 文件没被拦住(本次修复的主用例失守)")
|
||||
ok = False
|
||||
with open("Drivers/BSP/UI/new.c", "wb") as f:
|
||||
f.write(BOM + b"/* brand new, with BOM */")
|
||||
bad, n = check_paths(["Drivers/BSP/UI/new.c"])
|
||||
if bad or n != 1:
|
||||
print("[SELFTEST FAIL] 名单内新增文件**带** BOM 被误报")
|
||||
ok = False
|
||||
os.remove("Drivers/BSP/UI/new.c")
|
||||
|
||||
@@ -241,24 +276,38 @@ def selftest():
|
||||
# 名单外文件即使 BOM 丢了也不报(门禁不越权管名单外)
|
||||
with open("Lib/lv_conf.h", "wb") as f:
|
||||
f.write(b"x")
|
||||
with open("App/cfg/cfg_file.c", "wb") as f:
|
||||
f.write(b"/* listed but never committed, no BOM */")
|
||||
bad, n = check_paths(["App/cfg/cfg_file.c"])
|
||||
if bad or n != 0:
|
||||
print("[SELFTEST FAIL] 名单内但不在 HEAD 的文件被误报")
|
||||
if bad != ["App/cfg/cfg_file.c"] or n != 1:
|
||||
print("[SELFTEST FAIL] 名单内新增无 BOM 文件(eq 项)没被拦住")
|
||||
ok = False
|
||||
bad, n = check_paths(["README.md"])
|
||||
if n != 0:
|
||||
print("[SELFTEST FAIL] 名单外文件参与了检查")
|
||||
ok = False
|
||||
with open("Lib/lv_conf.h", "wb") as f:
|
||||
f.write(BOM + b"/* lv_conf, already has BOM in HEAD */")
|
||||
git_run(["add", "Lib/lv_conf.h"])
|
||||
git_run(["commit", "-q", "-m", "lv_conf"])
|
||||
with open("Lib/lv_conf.h", "wb") as f:
|
||||
f.write(b"/* BOM stripped by an editor */")
|
||||
bad, n = check_paths(["Lib/lv_conf.h"])
|
||||
if bad != ["Lib/lv_conf.h"] or n != 1:
|
||||
print("[SELFTEST FAIL] 已入库带 BOM 的文件被剥掉后没报(原有主用例失守)")
|
||||
ok = False
|
||||
finally:
|
||||
os.chdir(old)
|
||||
print("[SELFTEST] " + ("4 条判定全过(含两条票面要求的边界)" if ok else "有判定未过"))
|
||||
print("[SELFTEST] " + ("6 条判定全过(含新增文件必须带 BOM、名单外不越权等边界)"
|
||||
if ok else "有判定未过"))
|
||||
return 0 if ok else 1
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="BOM 漂移检查(bom-guard 钩子实现)")
|
||||
ap.add_argument("files", nargs="*", help="待核文件(仓库根相对路径)")
|
||||
ap.add_argument("--all", action="store_true", help="核 git 跟踪的全部文件")
|
||||
ap.add_argument("--all", action="store_true",
|
||||
help="核 git 跟踪的与未跟踪(不含 gitignore)的全部文件")
|
||||
ap.add_argument("--selftest", action="store_true", help="临时仓库自证判定")
|
||||
args = ap.parse_args()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user