build(git): .githooks补一个 commit-msg + commitlint

This commit is contained in:
sophie committed 2026-06-08 14:07:40 +08:00
1 parent c7f25ec8a1
commit 6057069c1c
9 files changed
+157 -12

No files matched your search

+40 -6
View File
@@ -15,6 +15,7 @@
| 层级 | 时机 | 谁生效 | 检查内容 |
|------|------|--------|----------|
| **pre-commit** | `git commit` | 已安装 Hook 的本机 | models 须带 migration;暂存 `frontend/` 时跑 `vue-tsc` |
| **commit-msg** | `git commit` | 已安装 Hook 的本机 | 提交信息须符合 [COMMIT_CONVENTION](./convention/COMMIT_CONVENTION.md)(commitlint) |
| **pre-push** | `git push` | 已安装 Hook 的本机 | 本次推送含 `frontend/` 变更时跑 `npm run build` |
| **Jenkins CI** | push 触发流水线 | 全员(不装 Hook 也跑) | `npm ci && npm run build`(`scripts/ci_frontend_build.sh`) |
@@ -47,6 +48,7 @@ ls -la .githooks
应能看到:
- `pre-commit`
- `commit-msg`
- `pre-push`
- `_common.sh`
@@ -71,10 +73,18 @@ chmod +x scripts/install_git_hooks.sh
```text
[hook-install] Installed Git hooks successfully.
[hook-install] pre-commit: models 变更需带 migration;frontend 暂存时跑 vue-tsc
[hook-install] commit-msg: 校验提交信息格式(Conventional Commits)
[hook-install] pre-push: 推送含 frontend 变更时跑 npm run build
[hook-install] core.hooksPath=.githooks
```
安装脚本会自动在仓库根目录执行 `npm install`(安装 commitlint 依赖)。若跳过脚本、仅用 **§3.2** 配置 Hook 路径,需手动:
```powershell
cd D:\path\to\hxops
npm install
```
### 3.2 方式 B:PowerShell / CMD
不跑 bash 脚本时,在仓库根执行:
@@ -87,7 +97,7 @@ git config core.hooksPath .githooks
若 commit/push 时提示无执行权限,在 Git Bash 中补充:
```bash
chmod +x .githooks/pre-commit .githooks/pre-push
chmod +x .githooks/pre-commit .githooks/commit-msg .githooks/pre-push
```
---
@@ -105,9 +115,13 @@ git config --get core.hooksPath
```powershell
cd frontend
npm install
cd ..
npm install
```
可选自测:暂存一个 `frontend/` 下的小改动后 `git commit`(会触发 `vue-tsc`),测完 `git reset` 即可。
根目录 `npm install` 用于 commitlint(`commit-msg` Hook);若已运行 `./scripts/install_git_hooks.sh` 会自动安装。
可选自测:暂存一个 `frontend/` 下的小改动后 `git commit -m "feat: test hook"`(会触发 vue-tsc 与 commitlint),测完 `git reset` 即可。
---
@@ -121,16 +135,32 @@ npm install
| 暂存路径含 `frontend/` | 运行 `npx vue-tsc -b`(需已有 `node_modules`) |
| 未暂存 `frontend/` | 不跑前端检查 |
`SKIP_FRONTEND_HOOKS` **不能**跳过 models/migration 检查。
`SKIP_FRONTEND_HOOKS` **不能**跳过 models/migration 检查,也**不能**跳过 commit-msg 校验。
### 5.2 pre-push
### 5.2 commit-msg
| 条件 | 行为 |
|------|------|
| 任意 `git commit`(非 merge) | 运行 commitlint,校验 Header 格式 |
| merge commit | 跳过 |
| 根目录无 `node_modules/@commitlint/cli` | **拒绝 commit**,提示 `npm install` |
格式要求见 [COMMIT_CONVENTION.md](./convention/COMMIT_CONVENTION.md),示例:`feat(auth): add SMS login support`。
本地手动校验(不提交):
```bash
echo "feat(auth): add SMS login support" | npx commitlint
```
### 5.3 pre-push
| 条件 | 行为 |
|------|------|
| 本次 push 的提交 diff 含 `frontend/` | 运行 `npm run build`(`vue-tsc -b && vite build`) |
| 无 `frontend/` 变更 | 直接通过 |
### 5.3 Jenkins CI
### 5.4 Jenkins CI
- 在 **Detect Changes** 之后、**Deploy** 之前执行。
- 仅当 `FRONTEND_CHANGED=true` 时在 `node:22-alpine` 容器内执行 `scripts/ci_frontend_build.sh`。
@@ -139,7 +169,7 @@ npm install
## 6. 团队落地清单(可转发)
1. **新同事**:`git clone` → `cd frontend && npm install` → 按本文 **§3** 安装 Hook → **§4** 验证。
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 路径配置。
@@ -194,6 +224,7 @@ git push --no-verify
| `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` |
@@ -205,8 +236,11 @@ git push --no-verify
| 路径 | 说明 |
|------|------|
| `.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 |
| `Jenkinsfile` | `Frontend Build (CI)` 阶段 |
+12 -4
View File
@@ -167,11 +167,19 @@ revert: feat(payment): support WeChat QR checkout
- 是否存在破坏性变更,若有是否标注 `BREAKING CHANGE`?
- 是否关联了任务编号(`Refs/Closes #id`)?
## 12. 推荐工具(落地执行)
## 12. 自动化校验(已启用)
- `commitlint`:校验提交信息格式。
- `husky`:在 `commit-msg` 钩子自动校验。
- 可选:`commitizen` 或 `cz-git` 提供交互式提交,降低学习成本。
仓库已通过 Git Hook 自动校验提交信息,配置如下:
- **commitlint**:根目录 `commitlint.config.cjs`,规则与本文档对齐。
- **commit-msg Hook**:`.githooks/commit-msg`,在 `git commit` 时自动运行。
- **安装**:`./scripts/install_git_hooks.sh`(会设置 `core.hooksPath` 并执行根目录 `npm install`)。
详见 [Git Hooks 团队启用指南](../GIT_HOOKS.md)。
可选工具(尚未配置,可按需引入):
- `commitizen` 或 `cz-git`:交互式提交,降低学习成本。
- 参考链接:https://www.conventionalcommits.org/en/v1.0.0/
---
+1 -1
View File
@@ -83,7 +83,7 @@ cd hxops
git config core.hooksPath .githooks
```
验证:`git config --get core.hooksPath` → `.githooks`;前置:`cd frontend && npm install`。
验证:`git config --get core.hooksPath` → `.githooks`;前置:`cd frontend && npm install`,以及仓库根目录 `npm install`(安装 commitlint;运行 `./scripts/install_git_hooks.sh` 时会自动执行)。
### 4.3 常用 Git 操作示例