ln-742-precommit-setup by levnikolaevich/claude-code-skills
npx skills add https://github.com/levnikolaevich/claude-code-skills --skill ln-742-precommit-setup路径说明: 文件路径(
shared/、references/、../ln-*)是相对于技能仓库根目录的。如果在当前工作目录(CWD)下找不到,请定位此 SKILL.md 文件所在的目录,然后向上返回一级以找到仓库根目录。如果缺少shared/目录,请通过 WebFetch 从https://raw.githubusercontent.com/levnikolaevich/claude-code-skills/master/skills/{path}获取文件。
类型: L3 Worker 类别: 7XX 项目引导 父级: ln-740-quality-setup
设置 Git 钩子,用于在提交前自动执行代码质量检查。
功能:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 技术栈 | 钩子管理器 | 暂存文件检查 | 提交信息验证 |
|---|---|---|---|
| Node.js | Husky | lint-staged | commitlint |
| Python | pre-commit | pre-commit 钩子 | pre-commit 钩子 |
| 混合项目 | 两者(如果需要) | 按技术栈区分 | commitlint |
在安装之前,检查是否存在现有的钩子配置。
需要检查的文件:
| 工具 | 标识 |
|---|---|
| Husky | .husky/ 目录,package.json 中的 husky |
| pre-commit | .pre-commit-config.yaml |
| lint-staged | package.json 中的 lint-staged 或 .lintstagedrc* |
| commitlint | commitlint.config.*,.commitlintrc* |
决策逻辑:
npm install -D husky
npx husky init
这将创建:
.husky/ 目录.husky/pre-commit 钩子文件prepare 脚本pip install pre-commit
# 或者使用 uv:
uv add --dev pre-commit
pre-commit install
这将创建:
.git/hooks/pre-commit(由 pre-commit 管理).pre-commit-config.yaml 进行配置npm install -D lint-staged
创建配置(.lintstagedrc.mjs 或在 package.json 中):
关键规则:
关键修复: 对于 .NET 文件,使用正确的语法:
"*.cs": "dotnet format --include"是错误的。应使用:"*.cs": "dotnet format whitespace --include"或单独运行 dotnet format
在 .pre-commit-config.yaml 中配置:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
npm install -D @commitlint/cli @commitlint/config-conventional
创建 commitlint.config.mjs,包含:
添加 commit-msg 钩子:
echo 'npx --no -- commitlint --edit "$1"' > .husky/commit-msg
添加到 .pre-commit-config.yaml:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v3.4.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
验证钩子是否正常工作。
测试 1:lint-staged 触发
# 创建一个有检查问题的文件
echo "const x=1" > test-file.ts
git add test-file.ts
git commit -m "test: verify hooks"
# 预期:lint-staged 运行,修复问题或阻止提交
测试 2:提交信息验证
git commit --allow-empty -m "bad message"
# 预期:commitlint 拒绝
git commit --allow-empty -m "test: valid message format"
# 预期:提交成功
清理:
rm test-file.ts
git reset HEAD~1 # 如果测试提交已创建
规则 1: Husky 需要 Git 仓库(首先执行
git init)。
规则 2: lint-staged 必须先配置好代码检查器(在运行 ln-742 之前先运行 ln-741)。
规则 3: 记录
--no-verify作为紧急提交的逃生通道。
规则 4: pre-commit 钩子应尽可能自动修复问题(使用
--fix标志)。
git commit --no-verify)| 文件 | 用途 |
|---|---|
| husky_precommit_template.sh | Husky pre-commit 钩子模板 |
| husky_commitmsg_template.sh | Husky commit-msg 钩子模板 |
| lintstaged_template.mjs | lint-staged 配置模板 |
| commitlint_template.mjs | commitlint 配置模板 |
| precommit_config_template.yaml | Python pre-commit 配置模板 |
| hooks_guide.md | 详细的钩子指南 |
| 错误 | 原因 | 解决方案 |
|---|---|---|
| Husky 未运行 | 缺少 prepare 脚本 | 再次运行 npx husky init |
| lint-staged 失败 | 缺少代码检查器 | 先运行 ln-741 |
| pre-commit 未找到 | 不在 PATH 中 | pip install pre-commit |
| 钩子未触发 | Git 钩子被禁用 | 检查 .git/hooks/ 权限 |
| Windows 路径问题 | Shell 脚本格式 | 使用跨平台语法 |
为用户记录:
# 跳过所有钩子(请谨慎使用!)
git commit --no-verify -m "emergency: bypass hooks"
# 仅跳过 pre-commit(保留 commit-msg)
HUSKY=0 git commit -m "fix: urgent hotfix"
版本: 2.0.0 最后更新: 2026-01-10
每周安装次数
158
仓库
GitHub 星标数
245
首次出现
2026年1月24日
安全审计
已安装于
claude-code144
opencode140
cursor140
gemini-cli139
codex139
github-copilot135
Paths: File paths (
shared/,references/,../ln-*) are relative to skills repo root. If not found at CWD, locate this SKILL.md directory and go up one level for repo root. Ifshared/is missing, fetch files via WebFetch fromhttps://raw.githubusercontent.com/levnikolaevich/claude-code-skills/master/skills/{path}.
Type: L3 Worker Category: 7XX Project Bootstrap Parent: ln-740-quality-setup
Sets up Git hooks for automated code quality enforcement before commits.
Does:
Does NOT:
| Technology | Hook Manager | Staged Linting | Commit Validation |
|---|---|---|---|
| Node.js | Husky | lint-staged | commitlint |
| Python | pre-commit | pre-commit hooks | pre-commit hook |
| Mixed | Both (if needed) | Stack-specific | commitlint |
Before installing, check for existing hook configurations.
Files to Check:
| Tool | Indicators |
|---|---|
| Husky | .husky/ directory, husky in package.json |
| pre-commit | .pre-commit-config.yaml |
| lint-staged | lint-staged in package.json or .lintstagedrc* |
| commitlint | commitlint.config.*, .commitlintrc* |
Decision Logic:
npm install -D husky
npx husky init
This creates:
.husky/ directory.husky/pre-commit hook fileprepare script to package.jsonpip install pre-commit
# OR with uv:
uv add --dev pre-commit
pre-commit install
This creates:
.git/hooks/pre-commit (managed by pre-commit).pre-commit-config.yaml for configurationnpm install -D lint-staged
Create configuration (.lintstagedrc.mjs or in package.json):
Key Rules:
CRITICAL FIX: For .NET files, use correct syntax:
"*.cs": "dotnet format --include"is WRONG Use:"*.cs": "dotnet format whitespace --include"or run dotnet format separately
Configuration in .pre-commit-config.yaml:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
npm install -D @commitlint/cli @commitlint/config-conventional
Create commitlint.config.mjs with:
Add commit-msg hook:
echo 'npx --no -- commitlint --edit "$1"' > .husky/commit-msg
Add to .pre-commit-config.yaml:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v3.4.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
Verify hooks work correctly.
Test 1: Lint-staged triggers
# Create a file with lint issues
echo "const x=1" > test-file.ts
git add test-file.ts
git commit -m "test: verify hooks"
# Expected: lint-staged runs, either fixes or blocks
Test 2: Commit message validation
git commit --allow-empty -m "bad message"
# Expected: commitlint rejects
git commit --allow-empty -m "test: valid message format"
# Expected: commit succeeds
Cleanup:
rm test-file.ts
git reset HEAD~1 # If test commit was made
RULE 1: Husky requires Git repository (
git initfirst).
RULE 2: lint-staged MUST have linters configured first (run ln-741 before ln-742).
RULE 3: Document
--no-verifyescape hatch for emergency commits.
RULE 4: pre-commit hooks should auto-fix when possible (
--fixflag).
git commit --no-verify)| File | Purpose |
|---|---|
| husky_precommit_template.sh | Husky pre-commit hook |
| husky_commitmsg_template.sh | Husky commit-msg hook |
| lintstaged_template.mjs | lint-staged configuration |
| commitlint_template.mjs | commitlint configuration |
| precommit_config_template.yaml | Python pre-commit config |
| hooks_guide.md | Detailed hooks guide |
| Error | Cause | Resolution |
|---|---|---|
| Husky not running | Missing prepare script | Run npx husky init again |
| lint-staged fails | Missing linter | Run ln-741 first |
| pre-commit not found | Not in PATH | pip install pre-commit |
| Hooks not triggering | Git hooks disabled | Check .git/hooks/ permissions |
| Windows path issues | Shell script format | Use cross-platform syntax |
Document for users:
# Skip all hooks (use sparingly!)
git commit --no-verify -m "emergency: bypass hooks"
# Skip only pre-commit (keeps commit-msg)
HUSKY=0 git commit -m "fix: urgent hotfix"
Version: 2.0.0 Last Updated: 2026-01-10
Weekly Installs
158
Repository
GitHub Stars
245
First Seen
Jan 24, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
claude-code144
opencode140
cursor140
gemini-cli139
codex139
github-copilot135
CodeQL代码扫描配置指南 - GitHub Actions工作流与CLI本地运行教程
632 周安装