pre-commit 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/bin/sh
  2. # 提交前钩子:跑 L1 单元测试(229 项,约 4 秒),失败则拒绝提交。
  3. #
  4. # 启用(每人一次,克隆后执行):git config core.hooksPath .githooks
  5. # 临时跳过:git commit --no-verify
  6. # 关闭: git config --unset core.hooksPath
  7. #
  8. # 约定:钩子只做「快速、离线、无硬件依赖」的检查;
  9. # ctest / video_eval / 浸泡属于发布前检查(见 docs/git_workflow.md §5)。
  10. PY=""
  11. for c in python python3 py; do
  12. if command -v "$c" >/dev/null 2>&1; then
  13. PY="$c"
  14. break
  15. fi
  16. done
  17. if [ -z "$PY" ]; then
  18. echo "[pre-commit] 未找到 python,跳过单元测试"
  19. exit 0
  20. fi
  21. if [ ! -d tests ] || [ ! -f pyproject.toml ]; then
  22. echo "[pre-commit] 不在仓库根目录,跳过单元测试"
  23. exit 0
  24. fi
  25. LOG="$(mktemp 2>/dev/null || echo /tmp/portcv_precommit.log)"
  26. echo "[pre-commit] 运行 tests/ 单元测试 ..."
  27. if ! "$PY" -m unittest discover -s tests -q >"$LOG" 2>&1; then
  28. echo "[pre-commit] 单元测试失败,提交被拒绝:"
  29. tail -n 40 "$LOG"
  30. echo "[pre-commit] 修复后重试;确认要绕过:git commit --no-verify"
  31. exit 1
  32. fi
  33. echo "[pre-commit] 单元测试通过"
  34. exit 0