Skip to content

Commit a8c3d0e

Browse files
chore: migrate hooks to pre-commit
1 parent 6f2e090 commit a8c3d0e

6 files changed

Lines changed: 81 additions & 117 deletions

File tree

.githooks/pre-commit

Lines changed: 0 additions & 62 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,48 @@
1-
# Optional pre-commit framework checks.
2-
# The default one-step commit automation lives in .githooks/pre-commit.
3-
# Install the native Git hook with: pnpm hooks:install
4-
5-
repos:
6-
# Markdown linting
7-
- repo: https://github.com/igorshubovych/markdownlint-cli
8-
rev: v0.38.0
9-
hooks:
10-
- id: markdownlint
11-
args: ['--config', '.markdownlint.json']
12-
files: '^documents/.*\.md$'
13-
exclude: '^documents/.*\bindex\.md$'
14-
15-
# Frontmatter validation (local hook)
16-
- repo: local
17-
hooks:
18-
- id: validate-frontmatter
19-
name: Validate article frontmatter
20-
entry: scripts/validate_frontmatter.py
21-
language: python
22-
additional_dependencies: ['pyyaml']
23-
files: '^documents/core-embedded-cpp/.*\.md$'
24-
exclude: '(index\.md|tags\.md)$'
25-
pass_filenames: false
26-
27-
# Check for added large files
28-
- repo: https://github.com/pre-commit/pre-commit-hooks
29-
rev: v4.5.0
30-
hooks:
31-
- id: check-added-large-files
32-
args: ['--maxkb=1500']
33-
- id: end-of-file-fixer
34-
- id: trailing-whitespace
35-
files: '^documents/.*\.md$'
36-
- id: check-yaml
37-
files: '^\.github/.*\.ya?ml$'
1+
# Install with: pnpm hooks:install
2+
3+
repos:
4+
# Markdown linting
5+
- repo: https://github.com/igorshubovych/markdownlint-cli
6+
rev: v0.38.0
7+
hooks:
8+
- id: markdownlint
9+
args: ['--config', '.markdownlint.json']
10+
files: '^documents/.*\.md$'
11+
exclude: '^documents/.*\bindex\.md$'
12+
13+
# Frontmatter validation (local hook)
14+
- repo: local
15+
hooks:
16+
- id: validate-frontmatter
17+
name: Validate article frontmatter
18+
entry: scripts/validate_frontmatter.py
19+
language: python
20+
additional_dependencies: ['pyyaml']
21+
files: '^documents/core-embedded-cpp/.*\.md$'
22+
exclude: '(index\.md|tags\.md)$'
23+
pass_filenames: false
24+
25+
- id: clang-format
26+
name: clang-format C/C++ sources
27+
entry: clang-format -i
28+
language: system
29+
files: '\.(c|cc|cpp|cxx|h|hh|hpp|hxx)$'
30+
31+
- id: update-translation-coverage
32+
name: Update translation coverage
33+
entry: python3 scripts/coverage.py --update
34+
language: system
35+
always_run: true
36+
pass_filenames: false
37+
38+
# Check for added large files
39+
- repo: https://github.com/pre-commit/pre-commit-hooks
40+
rev: v4.5.0
41+
hooks:
42+
- id: check-added-large-files
43+
args: ['--maxkb=1500']
44+
- id: end-of-file-fixer
45+
- id: trailing-whitespace
46+
files: '^documents/.*\.md$'
47+
- id: check-yaml
48+
files: '^\.github/.*\.ya?ml$'

CONTRIBUTING.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66

77
1. Fork 本仓库
88
2. 创建你的特性分支 (`git switch -c feature/amazing-feature`)
9-
3. 安装本地 Git hook (`pnpm hooks:install``scripts/setup_precommit.sh`)
9+
3. 安装 pre-commit 提交前检查 (`pnpm hooks:install``scripts/setup_precommit.sh`)
1010
4. 提交更改 (`git commit -m '添加某功能'`)
1111
5. 推送到分支 (`git push origin feature/amazing-feature`)
1212
6. 创建 Pull Request
1313

1414
## 提交前自动化
1515

16-
本仓库使用可追踪的原生 Git hook,配置位于 `.githooks/pre-commit`。安装后,每次 `git commit` 前会自动执行:
16+
本仓库使用 [pre-commit](https://pre-commit.com/) 管理提交前检查,配置位于 `.pre-commit-config.yaml`。安装后,每次 `git commit` 前会自动执行:
1717

18+
- Markdown lint、frontmatter 校验、大文件和基础空白检查
1819
- 对已暂存的 C/C++ 源文件和头文件运行 `clang-format -i`
1920
- 运行 `python3 scripts/coverage.py --update` 检查英文翻译覆盖率并更新 `README.md`
20-
- 对 hook 自动修改的文件执行 `git add`,因此通常不需要因为格式化或覆盖率徽章变化再次手动提交
21+
22+
如果 hook 修改了文件,pre-commit 会停止本次提交并提示重新暂存。请检查变更后重新执行 `git add``git commit`
2123

2224
首次克隆仓库后运行:
2325

@@ -34,10 +36,13 @@ scripts/setup_precommit.sh
3436

3537
hook 依赖本机已安装:
3638

39+
- `pre-commit`:用于运行提交前检查,可通过 `pipx install pre-commit` 安装
3740
- `python3`:用于翻译覆盖率统计
3841
- `clang-format`:用于 C/C++ 代码格式化
3942

40-
为了避免把未准备提交的内容一起带入 commit,如果已暂存的 C/C++ 文件或 `README.md` 同时还有未暂存改动,hook 会停止并提示先整理工作区。确实需要临时跳过 hook 时,可以使用 `git commit --no-verify`,但不建议在 PR 提交中长期绕过。
43+
pre-commit 会在运行检查前临时隔离未暂存改动,避免把未准备提交的内容混入本次提交。确实需要临时跳过 hook 时,可以使用 `git commit --no-verify`,但不建议在 PR 提交中长期绕过。
44+
45+
日常验证请使用 `pre-commit run` 检查已暂存文件。`pre-commit run --all-files` 会对全仓文件运行自动修复型 hook,包括对所有 C/C++ 文件执行 `clang-format -i`
4146

4247
## 文章规范
4348

README.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ flowchart TD
127127
| `pnpm build` | Production build with per-volume parallel build and search-index merge |
128128
| `pnpm build:single` | Run the regular single VitePress build |
129129
| `pnpm preview` | Preview the production build |
130-
| `pnpm hooks:install` / `scripts/setup_precommit.sh` | Install the pre-commit Git hook |
130+
| `pnpm hooks:install` / `scripts/setup_precommit.sh` | Install pre-commit checks |
131131
| `pnpm coverage` | Show English translation coverage |
132132
| `pnpm coverage:update` | Update the English coverage badge in `README.md` |
133133
| `python3 scripts/validate_frontmatter.py` | Validate article frontmatter |

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ flowchart TD
135135
| `pnpm build:single` | 使用 VitePress 单体构建 |
136136
| `pnpm check:links` | 检查 Markdown 与组件内部链接有效性 |
137137
| `pnpm preview` | 预览生产构建结果 |
138-
| `pnpm hooks:install` / `scripts/setup_precommit.sh` | 安装提交前 Git hook |
138+
| `pnpm hooks:install` / `scripts/setup_precommit.sh` | 安装 pre-commit 提交前检查 |
139139
| `pnpm coverage` | 查看英文翻译覆盖率 |
140140
| `pnpm coverage:update` | 更新 `README.md` 中的英文翻译覆盖率徽章 |
141141
| `python3 scripts/validate_frontmatter.py` | 验证文章 frontmatter |

scripts/setup_precommit.sh

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ set -euo pipefail
44

55
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
66
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
7-
HOOKS_DIR="$PROJECT_ROOT/.githooks"
8-
PRE_COMMIT_HOOK="$HOOKS_DIR/pre-commit"
97

108
RED='\033[0;31m'
119
GREEN='\033[0;32m'
1210
YELLOW='\033[1;33m'
1311
NC='\033[0m'
1412

1513
echo "=============================="
16-
echo " Git Hook Setup"
14+
echo " Pre-commit Hook Setup"
1715
echo "=============================="
1816
echo ""
1917

@@ -22,15 +20,27 @@ if ! git -C "$PROJECT_ROOT" rev-parse --git-dir >/dev/null 2>&1; then
2220
exit 1
2321
fi
2422

25-
if [ ! -f "$PRE_COMMIT_HOOK" ]; then
26-
echo -e "${RED}ERROR: missing $PRE_COMMIT_HOOK${NC}"
23+
if ! command -v pre-commit >/dev/null 2>&1; then
24+
echo -e "${RED}ERROR: pre-commit is required.${NC}"
25+
echo ""
26+
echo "Install it first, for example:"
27+
echo " pipx install pre-commit"
28+
echo " # or: python3 -m pip install --user pre-commit"
2729
exit 1
2830
fi
2931

30-
chmod +x "$PRE_COMMIT_HOOK"
31-
git -C "$PROJECT_ROOT" config core.hooksPath .githooks
32+
cd "$PROJECT_ROOT"
33+
34+
current_hooks_path="$(git -C "$PROJECT_ROOT" config --get core.hooksPath || true)"
35+
if [ -n "$current_hooks_path" ]; then
36+
echo -e "${YELLOW}Removing core.hooksPath=$current_hooks_path so .git/hooks/pre-commit is used.${NC}"
37+
git -C "$PROJECT_ROOT" config --unset core.hooksPath
38+
fi
3239

33-
echo -e "${GREEN}Installed native Git hooks via core.hooksPath=.githooks${NC}"
40+
pre-commit install --config "$PROJECT_ROOT/.pre-commit-config.yaml"
41+
42+
echo ""
43+
echo -e "${GREEN}Installed pre-commit hooks.${NC}"
3444
echo ""
3545

3646
missing_tools=()
@@ -41,24 +51,24 @@ for tool in python3 clang-format; do
4151
done
4252

4353
if [ "${#missing_tools[@]}" -gt 0 ]; then
44-
echo -e "${YELLOW}Missing optional/required hook tools:${NC}"
54+
echo -e "${YELLOW}Missing hook tools:${NC}"
4555
printf ' - %s\n' "${missing_tools[@]}"
4656
echo ""
4757
echo "Install them before committing files that need the corresponding checks."
4858
else
49-
echo -e "${GREEN}Required hook tools detected:${NC}"
59+
echo -e "${GREEN}Hook tools detected:${NC}"
5060
echo " - python3: $(python3 --version 2>&1)"
5161
echo " - clang-format: $(clang-format --version 2>&1)"
5262
fi
5363

5464
echo ""
5565
echo "What runs before each commit:"
66+
echo " - markdownlint/frontmatter/basic file checks from .pre-commit-config.yaml"
5667
echo " - clang-format on staged C/C++ source and header files"
5768
echo " - python3 scripts/coverage.py --update"
58-
echo " - git add for files updated by the hook, so one git commit is enough"
5969
echo ""
6070
echo "Run manually:"
61-
echo " .githooks/pre-commit"
71+
echo " pre-commit run --all-files"
6272
echo ""
6373
echo "Bypass only when necessary:"
6474
echo " git commit --no-verify -m 'message'"

0 commit comments

Comments
 (0)