Lefthookの設定
LefthookはGitフックを簡単に管理できるツールです。pre-commit、pre-push、commit-msgなどのGitフックをYAMLファイルで設定でき、チーム間で共有できます。
インストール
Section titled “インストール”brew install lefthook基本的な使い方
Section titled “基本的な使い方”プロジェクトでの初期化
Section titled “プロジェクトでの初期化”# プロジェクトルートで実行lefthook installこれにより.git/hooksにLefthookのフックがインストールされます。
設定ファイル
Section titled “設定ファイル”プロジェクトルートにlefthook.ymlを作成:
基本的な設定例
Section titled “基本的な設定例”pre-commit: parallel: true commands: eslint: files: git diff --name-only --cached --diff-filter=AM glob: "*.{js,jsx,ts,tsx}" run: npx eslint {files}
prettier: files: git diff --name-only --cached --diff-filter=AM glob: "*.{js,jsx,ts,tsx,json,md}" run: npx prettier --check {files}
pre-push: commands: test: run: npm test
commit-msg: commands: commitlint: run: npx commitlint --edit実践的な設定例
Section titled “実践的な設定例”Flutter/Dartプロジェクト
Section titled “Flutter/Dartプロジェクト”pre-commit: parallel: true commands: dart-format: glob: "*.dart" run: dart format --set-exit-if-changed {staged_files}
dart-analyze: glob: "*.dart" run: dart analyze
flutter-test: run: flutter testNode.js/TypeScriptプロジェクト
Section titled “Node.js/TypeScriptプロジェクト”pre-commit: parallel: true commands: lint: glob: "*.{js,jsx,ts,tsx}" run: npx eslint --fix {staged_files} && git add {staged_files}
type-check: glob: "*.{ts,tsx}" run: npx tsc --noEmit
prettier: glob: "*.{js,jsx,ts,tsx,json,md,yml}" run: npx prettier --write {staged_files} && git add {staged_files}
pre-push: commands: test: run: npm test -- --coverage
build: run: npm run buildPythonプロジェクト
Section titled “Pythonプロジェクト”pre-commit: parallel: true commands: black: glob: "*.py" run: black --check {staged_files}
flake8: glob: "*.py" run: flake8 {staged_files}
mypy: glob: "*.py" run: mypy {staged_files}
pre-push: commands: pytest: run: pytestスキップ機能
Section titled “スキップ機能”pre-commit: commands: lint: skip: - merge - rebase run: npm run lint条件付き実行
Section titled “条件付き実行”pre-commit: commands: migration-check: only: - ref: main glob: "**/migrations/*.sql" run: ./scripts/check-migrations.shカスタムスクリプト
Section titled “カスタムスクリプト”pre-commit: scripts: "check-secrets.sh": runner: bash run: | if git diff --cached --name-only | xargs grep -E "(password|secret|key)" ; then echo "Potential secrets detected!" exit 1 fiチーム開発での活用
Section titled “チーム開発での活用”1. プロジェクトセットアップ
Section titled “1. プロジェクトセットアップ”# lefthook.ymlをリポジトリに追加git add lefthook.ymlgit commit -m "Add lefthook configuration"
# READMEに追加echo "Run 'lefthook install' after cloning" >> README.md2. 開発者向けの設定
Section titled “2. 開発者向けの設定”各開発者が実行:
# リポジトリクローン後lefthook install
# フックの無効化(一時的)LEFTHOOK=0 git commit -m "Skip hooks"
# 特定のフックをスキップLEFTHOOK_EXCLUDE=test git pushトラブルシューティング
Section titled “トラブルシューティング”フックが実行されない
Section titled “フックが実行されない”# フックの再インストールlefthook uninstalllefthook install
# デバッグモードで実行LEFTHOOK_VERBOSE=1 git commitパフォーマンスの改善
Section titled “パフォーマンスの改善”# 並列実行を有効化pre-commit: parallel: true
# ファイルのフィルタリング commands: lint: files: git diff --name-only --cached glob: "src/**/*.{js,ts}" run: npx eslint {files}ローカル設定の上書き
Section titled “ローカル設定の上書き”.lefthook-local.ymlを作成(gitignoreに追加):
# 個人用の追加設定pre-commit: commands: personal-check: run: ./my-custom-check.shベストプラクティス
Section titled “ベストプラクティス”-
高速なチェックを優先
- 時間のかかるテストはpre-pushに配置
- pre-commitは軽量なチェックのみ
-
並列実行の活用
parallel: trueで実行時間を短縮
-
適切なスキップ条件
- mergeやrebase時は不要なチェックをスキップ
-
エラーメッセージの改善
commands:check:run: ./check.sh || echo "❌ Check failed! Run 'npm run fix' to resolve"