build(git): .githooks补一个 commit-msg + commitlint
This commit is contained in:
1 parent
c7f25ec8a1
commit
6057069c1c
9 files changed
+157
-12
No files matched your search
@@ -43,3 +43,27 @@ run_frontend_build() {
|
||||
npm run build
|
||||
)
|
||||
}
|
||||
|
||||
ensure_commitlint_deps() {
|
||||
local root_dir="${1}"
|
||||
if [[ ! -d "${root_dir}/node_modules/@commitlint/cli" ]]; then
|
||||
echo "[commit-msg] 根目录 node_modules 不存在,请先执行:"
|
||||
echo " cd ${root_dir}"
|
||||
echo " npm install"
|
||||
echo " 或重新运行: ./scripts/install_git_hooks.sh"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
run_commitlint() {
|
||||
local root_dir="${1}"
|
||||
local msg_file="${2}"
|
||||
if ! ensure_commitlint_deps "${root_dir}"; then
|
||||
return 1
|
||||
fi
|
||||
(
|
||||
cd "${root_dir}"
|
||||
npx --no-install commitlint --edit "${msg_file}"
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ROOT_DIR="$(git rev-parse --show-toplevel)"
|
||||
cd "${ROOT_DIR}"
|
||||
# shellcheck source=_common.sh
|
||||
source "${ROOT_DIR}/.githooks/_common.sh"
|
||||
|
||||
COMMIT_MSG_FILE="${1:-}"
|
||||
|
||||
if [[ -z "${COMMIT_MSG_FILE}" ]]; then
|
||||
echo "[commit-msg] missing commit message file path."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -f "${ROOT_DIR}/.git/MERGE_HEAD" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if ! run_commitlint "${ROOT_DIR}" "${COMMIT_MSG_FILE}"; then
|
||||
echo "[commit-msg] 提交信息不符合规范,详见 docs/convention/COMMIT_CONVENTION.md"
|
||||
echo "[commit-msg] 示例: feat(auth): add SMS login support"
|
||||
echo "[commit-msg] 跳过校验: git commit --no-verify"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -31,6 +31,10 @@ __pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# Root tooling (commitlint)
|
||||
/node_modules/
|
||||
package-lock.json
|
||||
|
||||
# Frontend
|
||||
frontend/node_modules/
|
||||
frontend/dist/
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/** @see docs/convention/COMMIT_CONVENTION.md */
|
||||
module.exports = {
|
||||
extends: ['@commitlint/config-conventional'],
|
||||
rules: {
|
||||
'type-enum': [
|
||||
2,
|
||||
'always',
|
||||
[
|
||||
'feat',
|
||||
'fix',
|
||||
'docs',
|
||||
'style',
|
||||
'refactor',
|
||||
'perf',
|
||||
'test',
|
||||
'build',
|
||||
'ci',
|
||||
'chore',
|
||||
'revert',
|
||||
],
|
||||
],
|
||||
'header-max-length': [2, 'always', 100],
|
||||
'subject-full-stop': [2, 'never', '.'],
|
||||
},
|
||||
};
|
||||
+40
-6
@@ -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)` 阶段 |
|
||||
|
||||
@@ -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
@@ -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 操作示例
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "hxops",
|
||||
"private": true,
|
||||
"description": "Repository-level tooling (Git hooks, commitlint)",
|
||||
"scripts": {
|
||||
"prepare:hooks": "npm install",
|
||||
"commitlint": "commitlint"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "^19.8.1",
|
||||
"@commitlint/config-conventional": "^19.8.1"
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ if ! git -C "${ROOT_DIR}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
fi
|
||||
|
||||
HOOKS_DIR="${ROOT_DIR}/.githooks"
|
||||
REQUIRED_HOOKS=(pre-commit pre-push _common.sh)
|
||||
REQUIRED_HOOKS=(pre-commit pre-push commit-msg _common.sh)
|
||||
|
||||
for hook_name in "${REQUIRED_HOOKS[@]}"; do
|
||||
hook_path="${HOOKS_DIR}/${hook_name}"
|
||||
@@ -28,8 +28,18 @@ if ! git -C "${ROOT_DIR}" config core.hooksPath .githooks; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -f "${ROOT_DIR}/package.json" ]]; then
|
||||
echo "[hook-install] Installing commitlint dependencies (npm install)..."
|
||||
if ! (cd "${ROOT_DIR}" && npm install); then
|
||||
echo "[hook-install] npm install failed. commit-msg hook requires root node_modules."
|
||||
echo "[hook-install] please run manually: cd ${ROOT_DIR} && npm install"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "[hook-install] Installed Git hooks successfully."
|
||||
echo "[hook-install] pre-commit: models 变更需带 migration;frontend 暂存时跑 vue-tsc"
|
||||
echo "[hook-install] commit-msg: 校验提交信息格式(Conventional Commits)"
|
||||
echo "[hook-install] pre-push: 推送含 frontend 变更时跑 npm run build"
|
||||
echo "[hook-install] core.hooksPath=$(git -C "${ROOT_DIR}" config --get core.hooksPath)"
|
||||
echo "[hook-install] 跳过前端检查: SKIP_FRONTEND_HOOKS=1"
|
||||
|
||||
Reference in new issue
Block a user