From 6df453f0571039640549e209313b28b88a277a32 Mon Sep 17 00:00:00 2001 From: wangjia <809946525@qq.com> Date: Wed, 24 Jun 2026 23:49:41 +0800 Subject: [PATCH] =?UTF-8?q?test(ci):=20=E7=AC=AC4=E5=88=80a=20pre-commit?= =?UTF-8?q?=20hook(=E9=97=B83=20=E6=8F=90=E4=BA=A4=E5=89=8D=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=8A=8A=E5=85=B3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 复用 CI 同款检查的快子集,提交前本地拦截: - .githooks/pre-commit:红线词 + 可移植SQL + codegen 漂移(成功静默/失败打详情) - ci/install-hooks.sh:一键启用(core.hooksPath=.githooks) - 不放 flutter/go test(慢,留 CI 闸4);可 --no-verify 临时跳过 Co-Authored-By: Claude Opus 4.8 --- .githooks/pre-commit | 33 +++++++++++++++++++++++++++++++++ ci/install-hooks.sh | 18 ++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 .githooks/pre-commit create mode 100755 ci/install-hooks.sh diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..f00e43c --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# .githooks/pre-commit — 闸 3:提交前本地把关(复用 CI 同款检查的快子集)。 +# +# 启用:bash ci/install-hooks.sh (设 git core.hooksPath=.githooks) +# 设计:成功静默、失败才打详情;只放秒级检查。flutter/go test 较慢,留给 CI(闸 4)。 +# +# 跑:红线词扫描 + 可移植 SQL 扫描 + codegen 漂移检查。 +set -euo pipefail + +ROOT="$(git rev-parse --show-toplevel)" +cd "$ROOT" + +# run_check <名称> <命令...> —— 成功静默,失败打输出并中止提交。 +run_check() { + local name="$1" + shift + local out + if ! out="$("$@" 2>&1)"; then + echo "[pre-commit] ✗ ${name} 未通过:" >&2 + echo "${out}" >&2 + echo "[pre-commit] 提交已中止。修复后重试,或临时 --no-verify 跳过(不推荐)。" >&2 + exit 1 + fi +} + +run_check "红线词扫描" bash ci/scan-redline.sh +run_check "可移植 SQL 扫描" bash ci/scan-portable-sql.sh + +if command -v node >/dev/null 2>&1; then + run_check "codegen 漂移检查" bash ci/check-codegen-drift.sh +fi + +echo "[pre-commit] ✓ 本地闸通过(完整测试见 CI)" diff --git a/ci/install-hooks.sh b/ci/install-hooks.sh new file mode 100755 index 0000000..39d8478 --- /dev/null +++ b/ci/install-hooks.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# install-hooks.sh — 启用本地 git hooks(闸 3:提交前把关)。 +# +# 一次性运行即可;之后每次 git commit 会自动跑 .githooks/pre-commit +# (红线词 + 可移植 SQL + codegen 漂移)。失败会中止提交。 +# +# 卸载: git config --unset core.hooksPath +set -euo pipefail + +ROOT="$(git rev-parse --show-toplevel)" +cd "$ROOT" + +git config core.hooksPath .githooks +chmod +x .githooks/* 2>/dev/null || true + +echo "✓ 已启用 git hooks(core.hooksPath=.githooks)" +echo " pre-commit 将跑:红线词扫描 · 可移植 SQL 扫描 · codegen 漂移检查" +echo " 卸载:git config --unset core.hooksPath"