Git Hooks Integration by doanchienthangdev/omgkit
npx skills add https://github.com/doanchienthangdev/omgkit --skill 'Git Hooks Integration'Git Hooks 集成通过 Git 钩子提供工作流规则的自动化执行。钩子在 .omgkit/workflow.yaml 中配置,并自动安装到 .git/hooks/ 目录下。这确保了在代码到达远程仓库之前,代码质量、提交规范和测试的一致性。
| 钩子 | 触发时机 | 目的 |
|---|---|---|
pre-commit | 提交前 | 代码检查、格式化、类型检查 |
commit-msg | 提交消息后 | 验证规范提交 |
pre-push | 推送前 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 运行测试、安全扫描 |
post-merge | 合并/拉取后 | 安装依赖、运行迁移 |
post-checkout | 检出后 | 环境设置 |
# .omgkit/workflow.yaml
hooks:
pre_commit:
enabled: true
actions:
- lint
- format
commit_msg:
enabled: true
validate_conventional: true
pre_push:
enabled: true
actions:
- test
hooks:
# Pre-commit: 每次提交前运行
pre_commit:
enabled: true
actions:
- lint # 运行 ESLint/Pylint/等
- type-check # TypeScript/mypy/Flow
- format # Prettier/Black/gofmt
- secrets-check # 检测泄露的密钥
fail_fast: true # 首次失败即停止
staged_only: true # 仅检查暂存文件
bypass_key: "SKIP_HOOKS" # 用于绕过的环境变量
# 提交消息验证
commit_msg:
enabled: true
validate_conventional: true
allowed_types:
- feat
- fix
- docs
- style
- refactor
- perf
- test
- chore
- ci
- build
require_scope: false
max_subject_length: 72
require_body: false
require_issue: false
issue_pattern: "#[0-9]+"
# Pre-push: 推送前运行
pre_push:
enabled: true
actions:
- test # 运行测试套件
- security-scan # npm audit / safety check
- build # 验证构建是否正常
skip_on_ci: true # 在 CI 环境中跳过
protected_branches:
- main
- develop
require_review_for:
- main
# Post-merge: git pull/merge 后运行
post_merge:
enabled: false
actions:
- install # npm install / pip install
- migrate # 数据库迁移
- generate # 代码生成
notify: false
# Post-checkout: git checkout 后运行
post_checkout:
enabled: false
actions:
- install # 安装依赖
- env-check # 验证环境
| 操作 | 描述 | 命令 |
|---|---|---|
lint | 运行代码检查器 | 自动检测 |
format | 格式化代码 | 自动检测 |
type-check | 类型检查 | 自动检测 |
secrets-check | 检测密钥 | git-secrets |
test-staged | 测试暂存文件 | vitest related |
自动检测逻辑:
# JavaScript/TypeScript
lint: npx eslint --fix
format: npx prettier --write
type-check: npx tsc --noEmit
# Python
lint: ruff check --fix
format: black
type-check: mypy
# Go
lint: golangci-lint run
format: gofmt -w
type-check: go vet
# Rust
lint: cargo clippy
format: cargo fmt
type-check: cargo check
| 操作 | 描述 | 命令 |
|---|---|---|
test | 运行测试 | 自动检测 |
test-coverage | 带覆盖率测试 | 自动检测 |
security-scan | 安全审计 | 自动检测 |
build | 验证构建 | 自动检测 |
review | Claude 审查 | /dev:review |
自动检测逻辑:
# JavaScript/TypeScript
test: npm test
security-scan: npm audit
build: npm run build
# Python
test: pytest
security-scan: safety check
build: python -m build
# Go
test: go test ./...
security-scan: gosec ./...
build: go build
# 从 workflow.yaml 设置钩子
/hooks:setup
# 输出:
# 安装 Git Hooks
# --------------------
# 从 .omgkit/workflow.yaml 读取配置
# 创建 pre-commit 钩子...
# 创建 commit-msg 钩子...
# 创建 pre-push 钩子...
# 完成!已安装 3 个钩子。
运行 /hooks:setup,它会在 .git/hooks/ 中创建可执行脚本:
.git/hooks/
├── pre-commit # 代码检查、格式化、类型检查
├── commit-msg # 验证提交消息
├── pre-push # 测试、安全扫描
└── post-merge # 安装、迁移
#!/bin/bash
# Generated by OMGKIT - Do not edit manually
# Regenerate with: /hooks:setup
set -e
# 检查绕过
if [ -n "$SKIP_HOOKS" ]; then
echo "Skipping pre-commit hooks (SKIP_HOOKS is set)"
exit 0
fi
echo "Running pre-commit checks..."
# 获取暂存文件
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
if [ -z "$STAGED_FILES" ]; then
echo "No staged files to check"
exit 0
fi
# 操作: lint
echo " Running lint..."
if command -v npx &> /dev/null && [ -f "package.json" ]; then
npx eslint --fix $STAGED_FILES
elif command -v ruff &> /dev/null; then
ruff check --fix $STAGED_FILES
fi
# 操作: format
echo " Running format..."
if command -v npx &> /dev/null && [ -f "package.json" ]; then
npx prettier --write $STAGED_FILES
git add $STAGED_FILES
fi
# 操作: type-check
echo " Running type-check..."
if [ -f "tsconfig.json" ]; then
npx tsc --noEmit
fi
echo "Pre-commit checks passed!"
#!/bin/bash
# Generated by OMGKIT - Do not edit manually
set -e
COMMIT_MSG_FILE=$1
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
# 规范提交模式
PATTERN="^(feat|fix|docs|style|refactor|perf|test|chore|ci|build)(\(.+\))?: .{1,72}$"
# 检查第一行
FIRST_LINE=$(echo "$COMMIT_MSG" | head -n1)
if ! echo "$FIRST_LINE" | grep -qE "$PATTERN"; then
echo "ERROR: Invalid commit message format"
echo ""
echo "Expected format: type(scope): subject"
echo " type: feat|fix|docs|style|refactor|perf|test|chore|ci|build"
echo " scope: optional, in parentheses"
echo " subject: max 72 characters"
echo ""
echo "Examples:"
echo " feat: add user authentication"
echo " fix(api): resolve null pointer exception"
echo " docs(readme): update installation guide"
echo ""
echo "Your message: $FIRST_LINE"
exit 1
fi
# 检查主题长度
SUBJECT_LENGTH=${#FIRST_LINE}
MAX_LENGTH=72
if [ $SUBJECT_LENGTH -gt $MAX_LENGTH ]; then
echo "ERROR: Commit subject too long ($SUBJECT_LENGTH > $MAX_LENGTH)"
exit 1
fi
echo "Commit message validated!"
#!/bin/bash
# Generated by OMGKIT - Do not edit manually
set -e
# 在 CI 中跳过
if [ -n "$CI" ] || [ -n "$GITHUB_ACTIONS" ]; then
echo "Skipping pre-push hooks (CI environment)"
exit 0
fi
# 检查绕过
if [ -n "$SKIP_HOOKS" ]; then
echo "Skipping pre-push hooks (SKIP_HOOKS is set)"
exit 0
fi
echo "Running pre-push checks..."
# 操作: test
echo " Running tests..."
if [ -f "package.json" ]; then
npm test
elif [ -f "pytest.ini" ] || [ -f "pyproject.toml" ]; then
pytest
elif [ -f "go.mod" ]; then
go test ./...
fi
# 操作: security-scan
echo " Running security scan..."
if [ -f "package.json" ]; then
npm audit --audit-level=high || true
elif command -v safety &> /dev/null; then
safety check || true
fi
echo "Pre-push checks passed!"
根据工作流配置安装或更新 Git 钩子:
/hooks:setup
# 选项:
/hooks:setup --force # 覆盖现有钩子
/hooks:setup --dry-run # 显示将要创建的内容
/hooks:setup --hook=pre-commit # 仅设置特定钩子
手动运行钩子操作:
/hooks:run pre-commit # 运行 pre-commit 操作
/hooks:run commit-msg # 验证最后一次提交消息
/hooks:run pre-push # 运行 pre-push 操作
# 运行特定操作
/hooks:run pre-commit --action=lint
/hooks:run pre-commit --action=format
# 为一个命令跳过所有钩子
SKIP_HOOKS=1 git commit -m "wip: temp commit"
# Git 原生跳过(不推荐)
git commit --no-verify -m "emergency fix"
hooks:
pre_commit:
exclude_paths:
- "vendor/**"
- "node_modules/**"
- "*.generated.*"
检查钩子是否可执行:
ls -la .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
重新生成钩子:
/hooks:setup --force
检查配置:
hooks:
pre_commit:
enabled: true # 必须为 true
手动运行以查看错误:
/hooks:run pre-commit
检查暂存文件:
git diff --cached --name-only
临时绕过:
SKIP_HOOKS=1 git commit -m "message"
使用仅检查暂存文件模式:
hooks:
pre_commit:
staged_only: true
在本地跳过缓慢的检查:
hooks:
pre_push:
skip_on_ci: true # 仅在 CI 中运行完整检查
钩子是对 CI/CD 的补充,但不能替代它:
| 检查项 | 本地钩子 | CI/CD |
|---|---|---|
| 代码检查 | pre-commit | 是 |
| 格式化 | pre-commit | 是 |
| 类型检查 | pre-commit | 是 |
| 单元测试 | pre-push | 是 |
| 集成测试 | 否 | 是 |
| 端到端测试 | 否 | 是 |
| 安全扫描 | pre-push | 是 |
| 构建 | pre-push | 是 |
| 部署 | 否 | 是 |
保持 pre-commit 在 5 秒以内:
hooks:
pre_commit:
actions:
- lint # 快速
- format # 快速
staged_only: true
# 将缓慢的检查移到 pre-push
在推送前运行彻底的检查:
hooks:
pre_push:
actions:
- test
- type-check
- security-scan
- build
提交 .omgkit/workflow.yaml 以确保所有团队成员拥有相同的钩子:
git add .omgkit/workflow.yaml
git commit -m "chore: add workflow config"
devops/workflow-config - 中央配置系统devops/github-actions - CI/CD 工作流testing/comprehensive-testing - 测试策略methodology/test-driven-development - TDD 实践每周安装数
0
代码仓库
GitHub 星标数
3
首次出现
1970年1月1日
安全审计
Git Hooks Integration provides automated enforcement of workflow rules through Git hooks. Hooks are configured in .omgkit/workflow.yaml and automatically installed to .git/hooks/. This ensures consistent code quality, commit conventions, and testing before code reaches the remote repository.
| Hook | Trigger | Purpose |
|---|---|---|
pre-commit | Before commit | Lint, format, type-check |
commit-msg | After commit message | Validate conventional commits |
pre-push | Before push | Run tests, security scan |
post-merge | After merge/pull | Install deps, run migrations |
post-checkout | After checkout | Environment setup |
# .omgkit/workflow.yaml
hooks:
pre_commit:
enabled: true
actions:
- lint
- format
commit_msg:
enabled: true
validate_conventional: true
pre_push:
enabled: true
actions:
- test
hooks:
# Pre-commit: runs before each commit
pre_commit:
enabled: true
actions:
- lint # Run ESLint/Pylint/etc
- type-check # TypeScript/mypy/Flow
- format # Prettier/Black/gofmt
- secrets-check # Detect leaked secrets
fail_fast: true # Stop on first failure
staged_only: true # Only check staged files
bypass_key: "SKIP_HOOKS" # Env var to bypass
# Commit message validation
commit_msg:
enabled: true
validate_conventional: true
allowed_types:
- feat
- fix
- docs
- style
- refactor
- perf
- test
- chore
- ci
- build
require_scope: false
max_subject_length: 72
require_body: false
require_issue: false
issue_pattern: "#[0-9]+"
# Pre-push: runs before pushing
pre_push:
enabled: true
actions:
- test # Run test suite
- security-scan # npm audit / safety check
- build # Verify build works
skip_on_ci: true # Skip in CI environment
protected_branches:
- main
- develop
require_review_for:
- main
# Post-merge: runs after git pull/merge
post_merge:
enabled: false
actions:
- install # npm install / pip install
- migrate # Database migrations
- generate # Code generation
notify: false
# Post-checkout: runs after git checkout
post_checkout:
enabled: false
actions:
- install # Install dependencies
- env-check # Verify environment
| Action | Description | Command |
|---|---|---|
lint | Run linter | Auto-detected |
format | Format code | Auto-detected |
type-check | Type checking | Auto-detected |
secrets-check | Detect secrets | git-secrets |
test-staged | Test staged files | vitest related |
Auto-detection Logic:
# JavaScript/TypeScript
lint: npx eslint --fix
format: npx prettier --write
type-check: npx tsc --noEmit
# Python
lint: ruff check --fix
format: black
type-check: mypy
# Go
lint: golangci-lint run
format: gofmt -w
type-check: go vet
# Rust
lint: cargo clippy
format: cargo fmt
type-check: cargo check
| Action | Description | Command |
|---|---|---|
test | Run tests | Auto-detected |
test-coverage | With coverage | Auto-detected |
security-scan | Security audit | Auto-detected |
build | Verify build | Auto-detected |
review | Claude review | /dev:review |
Auto-detection Logic:
# JavaScript/TypeScript
test: npm test
security-scan: npm audit
build: npm run build
# Python
test: pytest
security-scan: safety check
build: python -m build
# Go
test: go test ./...
security-scan: gosec ./...
build: go build
# Setup hooks from workflow.yaml
/hooks:setup
# Output:
# Installing Git Hooks
# --------------------
# Reading config from .omgkit/workflow.yaml
# Creating pre-commit hook...
# Creating commit-msg hook...
# Creating pre-push hook...
# Done! 3 hooks installed.
Run /hooks:setup which creates executable scripts in .git/hooks/:
.git/hooks/
├── pre-commit # Lint, format, type-check
├── commit-msg # Validate commit message
├── pre-push # Test, security scan
└── post-merge # Install, migrate
#!/bin/bash
# Generated by OMGKIT - Do not edit manually
# Regenerate with: /hooks:setup
set -e
# Check for bypass
if [ -n "$SKIP_HOOKS" ]; then
echo "Skipping pre-commit hooks (SKIP_HOOKS is set)"
exit 0
fi
echo "Running pre-commit checks..."
# Get staged files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
if [ -z "$STAGED_FILES" ]; then
echo "No staged files to check"
exit 0
fi
# Action: lint
echo " Running lint..."
if command -v npx &> /dev/null && [ -f "package.json" ]; then
npx eslint --fix $STAGED_FILES
elif command -v ruff &> /dev/null; then
ruff check --fix $STAGED_FILES
fi
# Action: format
echo " Running format..."
if command -v npx &> /dev/null && [ -f "package.json" ]; then
npx prettier --write $STAGED_FILES
git add $STAGED_FILES
fi
# Action: type-check
echo " Running type-check..."
if [ -f "tsconfig.json" ]; then
npx tsc --noEmit
fi
echo "Pre-commit checks passed!"
#!/bin/bash
# Generated by OMGKIT - Do not edit manually
set -e
COMMIT_MSG_FILE=$1
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
# Conventional commit pattern
PATTERN="^(feat|fix|docs|style|refactor|perf|test|chore|ci|build)(\(.+\))?: .{1,72}$"
# Check first line
FIRST_LINE=$(echo "$COMMIT_MSG" | head -n1)
if ! echo "$FIRST_LINE" | grep -qE "$PATTERN"; then
echo "ERROR: Invalid commit message format"
echo ""
echo "Expected format: type(scope): subject"
echo " type: feat|fix|docs|style|refactor|perf|test|chore|ci|build"
echo " scope: optional, in parentheses"
echo " subject: max 72 characters"
echo ""
echo "Examples:"
echo " feat: add user authentication"
echo " fix(api): resolve null pointer exception"
echo " docs(readme): update installation guide"
echo ""
echo "Your message: $FIRST_LINE"
exit 1
fi
# Check subject length
SUBJECT_LENGTH=${#FIRST_LINE}
MAX_LENGTH=72
if [ $SUBJECT_LENGTH -gt $MAX_LENGTH ]; then
echo "ERROR: Commit subject too long ($SUBJECT_LENGTH > $MAX_LENGTH)"
exit 1
fi
echo "Commit message validated!"
#!/bin/bash
# Generated by OMGKIT - Do not edit manually
set -e
# Skip in CI
if [ -n "$CI" ] || [ -n "$GITHUB_ACTIONS" ]; then
echo "Skipping pre-push hooks (CI environment)"
exit 0
fi
# Check for bypass
if [ -n "$SKIP_HOOKS" ]; then
echo "Skipping pre-push hooks (SKIP_HOOKS is set)"
exit 0
fi
echo "Running pre-push checks..."
# Action: test
echo " Running tests..."
if [ -f "package.json" ]; then
npm test
elif [ -f "pytest.ini" ] || [ -f "pyproject.toml" ]; then
pytest
elif [ -f "go.mod" ]; then
go test ./...
fi
# Action: security-scan
echo " Running security scan..."
if [ -f "package.json" ]; then
npm audit --audit-level=high || true
elif command -v safety &> /dev/null; then
safety check || true
fi
echo "Pre-push checks passed!"
Install or update Git hooks based on workflow config:
/hooks:setup
# Options:
/hooks:setup --force # Overwrite existing hooks
/hooks:setup --dry-run # Show what would be created
/hooks:setup --hook=pre-commit # Setup specific hook only
Manually run hook actions:
/hooks:run pre-commit # Run pre-commit actions
/hooks:run commit-msg # Validate last commit message
/hooks:run pre-push # Run pre-push actions
# Run specific action
/hooks:run pre-commit --action=lint
/hooks:run pre-commit --action=format
# Skip all hooks for one command
SKIP_HOOKS=1 git commit -m "wip: temp commit"
# Git native skip (not recommended)
git commit --no-verify -m "emergency fix"
hooks:
pre_commit:
exclude_paths:
- "vendor/**"
- "node_modules/**"
- "*.generated.*"
Check hook is executable:
ls -la .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
Regenerate hooks:
/hooks:setup --force
Check config:
hooks:
pre_commit:
enabled: true # Must be true
Run manually to see errors:
/hooks:run pre-commit
Check staged files:
git diff --cached --name-only
Bypass temporarily:
SKIP_HOOKS=1 git commit -m "message"
Use staged-only checking:
hooks:
pre_commit:
staged_only: true
Skip slow checks locally:
hooks:
pre_push:
skip_on_ci: true # Run full checks in CI only
Hooks complement CI/CD but don't replace it:
| Check | Local Hook | CI/CD |
|---|---|---|
| Lint | pre-commit | Yes |
| Format | pre-commit | Yes |
| Type-check | pre-commit | Yes |
| Unit tests | pre-push | Yes |
| Integration tests | No | Yes |
| E2E tests | No | Yes |
| Security scan | pre-push | Yes |
| Build | pre-push | Yes |
| Deploy | No | Yes |
Keep pre-commit under 5 seconds:
hooks:
pre_commit:
actions:
- lint # Fast
- format # Fast
staged_only: true
# Move slow checks to pre-push
Run thorough checks before pushing:
hooks:
pre_push:
actions:
- test
- type-check
- security-scan
- build
Commit .omgkit/workflow.yaml to ensure all team members have same hooks:
git add .omgkit/workflow.yaml
git commit -m "chore: add workflow config"
devops/workflow-config - Central configuration systemdevops/github-actions - CI/CD workflowstesting/comprehensive-testing - Test strategiesmethodology/test-driven-development - TDD practicesWeekly Installs
0
Repository
GitHub Stars
3
First Seen
Jan 1, 1970
Security Audits
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
147,400 周安装