Gitワークフロー強制ツール
一貫したGitワークフロー(コミットメッセージ、ブランチ命名、PRプロセス)を強制します。
使用時
- コミットまたはブランチを作成するとき
- コードレビューまたはPR作成時
- ユーザーが「git workflow」「commit message」「branch naming」「pull request」に言及したとき
手順
1. 既存の規則を検出する
以下を確認してください:
.github/または.gitlab/テンプレートCONTRIBUTING.md- 既存のコミットメッセージパターン
- ブランチ命名パターン
2. Conventional Commits
フォーマット: type(scope): description
タイプ:
feat: 新機能fix: バグ修正docs: ドキュメントstyle: 書式設定、セミコロンの欠落refactor: コードの再構築perf: パフォーマンス改善test: テストの追加chore: メンテナンス、依存関係
例:
feat(auth): add OAuth2 login support
fix(api): handle null response in user endpoint
docs(readme): update installation instructions
refactor(utils): simplify date formatting logic
3. ブランチ命名
一般的なパターン:
feature/user-authentication
bugfix/login-error-handling
hotfix/critical-security-patch
release/v1.2.0
chore/update-dependencies
検証項目:
- 小文字とハイフンを使用
- タイプを接頭辞として付加
- 説明的な名前
- 該当する場合は課題番号:
feature/123-add-dark-mode
4. コミットメッセージの検証
良いコミットの例:
feat(payments): integrate Stripe payment gateway
- Add Stripe SDK configuration
- Implement payment intent creation
- Add webhook handler for payment events
- Update tests for payment flow
Closes #456
チェック項目:
- 件名は50文字以内
- 本文は72文字で折り返し
- 件名と本文の間に空行
- 命令形で記述("added"ではなく"add")
- 課題/チケットへの参照
5. PRテンプレート
.github/PULL_REQUEST_TEMPLATE.md を作成:
## Description
<!-- What does this PR do? -->
## Type of Change
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update
## Testing
<!-- How was this tested? -->
## Checklist
- [ ] Code follows project style guidelines
- [ ] Self-review completed
- [ ] Comments added for complex code
- [ ] Documentation updated
- [ ] Tests added/updated
- [ ] All tests passing
- [ ] No new warnings
## Related Issues
Closes #
6. コミットフック
pre-commit:
#!/bin/sh
# .git/hooks/pre-commit
# Run linter
npm run lint
# Run tests
npm test
# Check for sensitive data
if git diff --cached | grep -i "password\|api_key\|secret"; then
echo "Error: Possible sensitive data detected"
exit 1
fi
commit-msg:
#!/bin/sh
# .git/hooks/commit-msg
commit_msg=$(cat "$1")
# Check conventional commit format
if ! echo "$commit_msg" | grep -qE "^(feat|fix|docs|style|refactor|perf|test|chore)(\(.+\))?: .+"; then
echo "Error: Commit message must follow Conventional Commits format"
echo "Example: feat(auth): add login feature"
exit 1
fi
7. 既存のコミットを検証
最近のコミットがパターンに従っているか確認します:
git log --oneline -20 | grep -v "^[a-f0-9]\{7\} (feat|fix|docs|style|refactor|perf|test|chore)"
8. 変更ログの生成
Conventional Commitsから生成:
# Using standard-version
npx standard-version
# Or manually group by type
git log --pretty=format:"%s" | grep "^feat" > features.txt
git log --pretty=format:"%s" | grep "^fix" > fixes.txt
9. 保護ブランチ
GitHub設定:
- PRレビューを必須にする
- ステータスチェックを必須にする
- 署名付きコミットを必須にする
- プッシュできる人を制限する
- 線形履歴を必須にする
10. ベストプラクティス
- アトミックなコミット: 1コミットで1つの論理的な変更
- 説明的なメッセージ: 「何を」ではなく「なぜ」を説明する
- 頻繁なコミット: 大きく稀なコミットよりも、小さく頻繁なコミット
- クリーンな履歴: マージ前にsquash/rebaseを行う
- コミットへの署名: セキュリティのためにGPG署名
- 課題の参照: トラッキングシステムにリンクする
Gitコミットテンプレート
.gitmessage を作成:
<type>(<scope>): <subject>
<body>
<footer>
# Type: feat, fix, docs, style, refactor, perf, test, chore
# Scope: component or file affected
# Subject: imperative, lowercase, no period, ≤50 chars
# Body: explain what and why, not how, wrapped at 72 chars
# Footer: breaking changes, issue references
テンプレートとして設定:
git config --global commit.template ~/.gitmessage
サポートファイル
templates/PULL_REQUEST_TEMPLATE.mdtemplates/commit-msg-hook.shtemplates/.gitmessage