1
0

pre-commit 923 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env bash
  2. # pre-commit 钩子:对暂存文件执行 autocorrect --lint 排版检查
  3. # 启用:git config core.hooksPath .githooks
  4. # 可绕过:git commit --no-verify(远程 CI 门禁不可绕过)
  5. # 未安装 autocorrect 时跳过
  6. if ! command -v autocorrect > /dev/null 2>&1; then
  7. exit 0
  8. fi
  9. # 暂存文件(新增、修改、复制和重命名,排除删除;尊重 .autocorrectignore)
  10. # 逐文件检查便于定位错误;autocorrect 自动跳过二进制与不支持的类型。
  11. staged="$(git diff --cached --name-only --diff-filter=ACMR)"
  12. [ -z "$staged" ] && exit 0
  13. failed=0
  14. while IFS= read -r file; do
  15. [ -f "$file" ] || continue
  16. autocorrect --lint "$file" || failed=1
  17. done <<< "$staged"
  18. if [ "$failed" -ne 0 ]; then
  19. echo "错误:暂存文件排版不符合 AutoCorrect 规则"
  20. echo "提示:运行 autocorrect --fix <文件> 或手动调整后重新 git add"
  21. exit 1
  22. fi
  23. exit 0