Files
syops/docs/GIT_HOOKS.md
T

8.1 KiB
Raw Blame History

Git Hooks 团队启用指南

更新时间:2026-05-21
适用对象:全体开发同学
相关脚本:.githooks/、scripts/install_git_hooks.sh、scripts/ci_frontend_build.sh

团队启用 Git Hook 分两步:仓库里已有 .githooks/(git pull 即有),每人本机执行一次安装。


1. 策略概览

本地两层 Hook + 远程 Jenkins CI,避免「日常只跑 npm run dev、上线 Jenkins npm run build 才报错」。

层级 时机 谁生效 检查内容
pre-commit git commit 已安装 Hook 的本机 models 须带 migration;migration 须单 head;暂存 frontend/ 时跑 vue-tsc
commit-msg git commit 已安装 Hook 的本机 提交信息须符合 COMMIT_CONVENTION(commitlint)
pre-push git push 已安装 Hook 的本机 本次推送含 migration 变更时校验单 head;含 frontend/ 变更时跑 npm run build
Jenkins CI push 触发流水线 全员(不装 Hook 也跑) npm ci && npm run build(scripts/ci_frontend_build.sh)
  • Hook 不会随 git clone 自动启用,需每人配置 core.hooksPath=.githooks。
  • 没装 Hook 时,主要靠 Jenkins Frontend Build (CI) 阶段兜底。
  • 日常开发仍用 npm run dev;Hook 只在 commit / push 时额外检查。

2. 确认仓库里已有钩子文件

在项目根目录(与 frontend、backend 同级):

PowerShell:

cd D:\path\to\hxops
git pull
dir .githooks

Git Bash:

cd /path/to/hxops
git pull
ls -la .githooks

应能看到:

  • pre-commit
  • commit-msg
  • pre-push
  • _common.sh

若目录不存在,先 git pull 到包含这些文件的版本。


3. 本机启用(每人、每个 clone 做一次)

3.1 方式 A:安装脚本(推荐)

在 Git Bash 或 WSL 中:

cd /path/to/hxops
chmod +x scripts/install_git_hooks.sh
./scripts/install_git_hooks.sh

成功输出示例:

[hook-install] Installed Git hooks successfully.
[hook-install]   pre-commit: models 变更需带 migration;migration 须单 head;frontend 暂存时跑 vue-tsc
[hook-install]   commit-msg: 校验提交信息格式(Conventional Commits)
[hook-install]   pre-push:   推送含 migration 变更时校验单 head;推送含 frontend 变更时跑 npm run build
[hook-install] core.hooksPath=.githooks

安装脚本会自动在仓库根目录执行 npm install(安装 commitlint 依赖)。若跳过脚本、仅用 §3.2 配置 Hook 路径,需手动:

cd D:\path\to\hxops
npm install

3.2 方式 B:PowerShell / CMD

不跑 bash 脚本时,在仓库根执行:

cd D:\path\to\hxops
git config core.hooksPath .githooks

若 commit/push 时提示无执行权限,在 Git Bash 中补充:

chmod +x .githooks/pre-commit .githooks/commit-msg .githooks/pre-push

4. 验证是否生效

git config --get core.hooksPath

应输出:.githooks

安装前端依赖(Hook 跑检查前必需):

cd frontend
npm install
cd ..
npm install

根目录 npm install 用于 commitlint(commit-msg Hook);若已运行 ./scripts/install_git_hooks.sh 会自动安装。

可选自测:暂存一个 frontend/ 下的小改动后 git commit -m "feat: test hook"(会触发 vue-tsc 与 commitlint),测完 git reset 即可。


5. 装好后会自动做什么

5.1 pre-commit

条件 行为
暂存了 backend/app/models.py,但未暂存 backend/migrations/versions/*.py 拒绝 commit
暂存了 backend/migrations/versions/*.py 运行 scripts/check_migration_heads.py:Alembic 必须仅 1 个 head;新增 migration 的 down_revision 须指向已存在的 revision
暂存路径含 frontend/ 运行 npx vue-tsc -b(需已有 node_modules)
未暂存 frontend/ 不跑前端检查

多 head 时 hook 会提示执行 flask --app manage.py db merge heads 后再提交。

SKIP_FRONTEND_HOOKS 不能跳过 models/migration 检查,也不能跳过 commit-msg 校验。

5.2 commit-msg

条件 行为
任意 git commit(非 merge) 运行 commitlint,校验 Header 格式
merge commit 跳过
根目录无 node_modules/@commitlint/cli 拒绝 commit,提示 npm install

格式要求见 COMMIT_CONVENTION.md,示例:feat(auth): add SMS login support。

本地手动校验(不提交):

echo "feat(auth): add SMS login support" | npx commitlint

5.3 pre-push

条件 行为
本次 push 的提交 diff 含 backend/migrations/versions/*.py 运行 scripts/check_migration_heads.py,Alembic 必须仅 1 个 head
本次 push 的提交 diff 含 frontend/ 运行 npm run build(vue-tsc -b && vite build)
无 migration / frontend/ 变更 直接通过

5.4 Jenkins CI

  • 在 Detect Changes 之后、Deploy 之前执行。
  • 仅当 FRONTEND_CHANGED=true 时在 node:22-alpine 容器内执行 scripts/ci_frontend_build.sh。

6. 团队落地清单(可转发)

  1. 新同事:git clone → cd frontend && npm install → 按本文 §3 安装 Hook(含根目录 npm install)→ §4 验证。
  2. 老同事:git pull 后若从未装过,补做一次安装。
  3. 换电脑 / 新 clone:每个新目录都要再装一次(core.hooksPath 写在本地 .git/config,不随 push 同步)。
  4. Windows:建议用 Git for Windows 自带的 Git Bash 执行 commit/push;PowerShell 可用 §3.2 仅完成 Hook 路径配置。

7. 应急跳过(不要常态化)

7.1 只跳过前端检查

migration 规则在 pre-commit / pre-push 中仍会执行。

Git Bash:

SKIP_FRONTEND_HOOKS=1 git commit -m "your message"
SKIP_FRONTEND_HOOKS=1 git push

PowerShell(当前终端会话):

$env:SKIP_FRONTEND_HOOKS = "1"
git commit -m "your message"
git push

7.2 跳过全部 Hook

git commit --no-verify
git push --no-verify

同时跳过 migration、vue-tsc、build 等所有 Hook。

7.3 环境变量说明

变量 默认值 生效条件
SKIP_FRONTEND_HOOKS 未设置 设为 1 时仅跳过前端 vue-tsc / build;若本次 push 含 migration,仍会检查 migration
SKIP_GIT_HOOKS 未设置 设为 1 时跳过前端检查(与上一项在前端逻辑等价)

仓库内没有预置这些变量的配置文件,仅在单次命令前临时设置。


8. 常见问题

现象 处理
dir .githooks 找不到 git pull;确认在仓库根目录
missing hook file 同上,确保 .githooks 已拉取
frontend/node_modules 不存在 cd frontend && npm install
根目录 node_modules 不存在 / commitlint 报错 仓库根目录执行 npm install
PowerShell 无法执行 .sh 用 §3.2 的 git config,或改用 Git Bash
装了 Hook 仍不拦截 确认 git config --get core.hooksPath;是否用了 --no-verify
想取消 Hook git config --unset core.hooksPath

9. 相关文件

路径 说明
.githooks/pre-commit 提交时检查
.githooks/commit-msg 提交信息格式校验
.githooks/pre-push 推送时检查
.githooks/_common.sh 共用逻辑
commitlint.config.cjs commitlint 规则(对齐 COMMIT_CONVENTION)
package.json 根目录 tooling 依赖(commitlint)
scripts/install_git_hooks.sh 一键安装
scripts/ci_frontend_build.sh Jenkins / 本地 CI 同款 build
scripts/check_migration_heads.py 离线校验 Alembic 仅 1 个 head(pre-commit / pre-push 调用)
Jenkinsfile Frontend Build (CI) 阶段
docs/新人环境搭建与配置文档.md 新人环境 §4.2 简要说明

维护建议:调整 Hook 规则或 Jenkins 构建步骤后,请同步更新本文档。