Files
gplan-2/tools/hooks/pre-commit

40 lines
2.0 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 直改车道闸(git pre-commit,2026-10-26 车道铁律)——staged 不含代码面的提交,提交点现场跑
# 文档域闸(tools/run_all_gates.sh --docs,秒级文本扫描):绿=落地,红=中止。
# 代码面=game/ 下非 .md 的行为载体(判据与改动域划分同一)。staged 含代码面时:
# 主工作区=拦(代码面唯一合法路径=worktree 批合回);任务 worktree=跳过(归批末闸+合回闸管,
# 不打扰迭代批中间提交)。--no-verify 绕过仅属用户(agent 侧由 worktree-guard 拦截)。
set -u
REPO="$(git rev-parse --show-toplevel)"
# staged 路径集(首提交无 HEAD 时对 --root 比);空集放行,交回 git 自身报「无改动可提」
if git rev-parse --verify -q HEAD >/dev/null 2>&1; then
STAGED="$(git diff --cached --name-only)"
else
STAGED="$(git diff --cached --name-only --root)"
fi
[ -z "$STAGED" ] && exit 0
# 代码面判定:staged 里任一路径落在 game/ 且非 .md
if printf '%s\n' "$STAGED" | grep '^game/' | grep -viE '\.md$' | grep -q .; then
# 主区判定:git-dir 与 common-dir 同一=主工作区(非链接 worktree)
_GD="$(cd "$(git rev-parse --git-dir)" && pwd)"
_CD="$(cd "$(git rev-parse --git-common-dir)" && pwd)"
if [ "$_GD" = "$_CD" ]; then
echo "[直改闸] 主工作区禁止直提代码面(game/ 下非 .md)——代码面改动走迭代批独立 worktree(git worktree add ../g-plan2-<任务名> -b <任务分支>),收尾在主工作区 git merge --no-ff 合回。" >&2
exit 1
fi
exit 0
fi
RC=0
bash "$REPO/tools/run_all_gates.sh" --docs || RC=$?
if [ "$RC" -eq 0 ]; then
exit 0
elif [ "$RC" -eq 1 ]; then
echo "[直改闸] 文档域闸有新增红,提交已拦。修上方红项后重新 commit,钩子会自动重跑闸;禁跑代码域/游戏类测试,与本次红项无关。" >&2
else
echo "[直改闸] 闸脚本自身故障(退出码 $RC),不是文档红——把本输出原样呈报用户,勿改文档、勿跑替代测试。" >&2
fi
exit 1