hooksとカスタマイズ
hooksとカスタマイズ
Section titled “hooksとカスタマイズ”Gitの動作をカスタマイズするためのhooksとその他のカスタマイズ方法について詳しく解説します。
なぜhooksとカスタマイズが必要なのか
Section titled “なぜhooksとカスタマイズが必要なのか”カスタマイズなしの問題
Section titled “カスタマイズなしの問題”問題のある状況:
開発者: 「コミット前にlintを実行したい...」開発者: 「コミットメッセージの形式を統一したい...」開発者: 「プッシュ前にテストを実行したい...」
// 問題点:// - 手動でのチェックが必要// - ルールが守られない// - 品質が低下する可能性影響:
- 手動チェックの負担
- ルール違反の発生
- 品質の低下
- 開発効率の低下
hooksとカスタマイズによる解決
Section titled “hooksとカスタマイズによる解決”改善された状況:
自動化されたチェック:- コミット前に自動的にlintを実行- コミットメッセージの形式を自動チェック- プッシュ前に自動的にテストを実行メリット:
- 手動チェックの削減
- ルールの自動適用
- 品質の向上
- 開発効率の向上
Git Hooks
Section titled “Git Hooks”hooksとは
Section titled “hooksとは”定義:
hooksは、Gitの特定のイベント(コミット、プッシュなど)の前後に実行されるスクリプトです。
hooksの種類:
.git/hooks/├── pre-commit # コミット前├── prepare-commit-msg # コミットメッセージ準備時├── commit-msg # コミットメッセージ検証時├── post-commit # コミット後├── pre-push # プッシュ前└── post-receive # リモートで受信後pre-commitフック
Section titled “pre-commitフック”実践例:
#!/bin/sh# lintを実行npm run lintif [ $? -ne 0 ]; then echo "Lintエラーがあります。コミットを中止します。" exit 1fi
# テストを実行npm testif [ $? -ne 0 ]; then echo "テストが失敗しました。コミットを中止します。" exit 1fi
# ファイルサイズをチェックfor file in $(git diff --cached --name-only); do if [ -f "$file" ]; then size=$(wc -c < "$file") if [ $size -gt 10485760 ]; then # 10MB echo "ファイル $file が大きすぎます(10MB以上)。" exit 1 fi fidone
exit 0実行権限の付与:
chmod +x .git/hooks/pre-commitcommit-msgフック
Section titled “commit-msgフック”実践例:
#!/bin/sh# コミットメッセージの形式をチェックcommit_msg=$(cat "$1")
# 形式: "type: subject"# type: feat, fix, docs, style, refactor, test, chore# subject: 50文字以内
if ! echo "$commit_msg" | grep -qE "^(feat|fix|docs|style|refactor|test|chore): .{1,50}$"; then echo "コミットメッセージの形式が正しくありません。" echo "形式: type: subject" echo "例: feat: ユーザー認証機能を追加" exit 1fi
exit 0pre-pushフック
Section titled “pre-pushフック”実践例:
#!/bin/sh# プッシュ前にテストを実行echo "プッシュ前にテストを実行します..."npm test
if [ $? -ne 0 ]; then echo "テストが失敗しました。プッシュを中止します。" exit 1fi
# ビルドを実行echo "プッシュ前にビルドを実行します..."npm run build
if [ $? -ne 0 ]; then echo "ビルドが失敗しました。プッシュを中止します。" exit 1fi
exit 0Huskyを使用したhooks管理
Section titled “Huskyを使用したhooks管理”Huskyとは
Section titled “Huskyとは”定義: Huskyは、Git hooksを簡単に管理するためのNode.jsパッケージです。
インストール:
npm install --save-dev husky設定:
{ "scripts": { "prepare": "husky install" }}hooksの設定:
# pre-commitフックを設定npx husky add .husky/pre-commit "npm run lint"
# commit-msgフックを設定npx husky add .husky/commit-msg "npx --no-install commitlint --edit \$1"
# pre-pushフックを設定npx husky add .husky/pre-push "npm test"実践例:
#!/bin/sh. "$(dirname "$0")/_/husky.sh"
npm run lintnpm run testGit設定のカスタマイズ
Section titled “Git設定のカスタマイズ”エイリアスの設定
Section titled “エイリアスの設定”実践例:
# よく使うコマンドのエイリアスgit config --global alias.st statusgit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.ci commitgit config --global alias.unstage 'reset HEAD --'
# カスタムエイリアスgit config --global alias.lg "log --oneline --graph --decorate --all"git config --global alias.last "log -1 HEAD"git config --global alias.visual "!gitk"使用例:
# git status → git stgit st
# git checkout → git cogit co main
# git log --oneline --graph --decorate --all → git lggit lgエディタの設定
Section titled “エディタの設定”実践例:
# デフォルトエディタを設定git config --global core.editor "code --wait" # VS Codegit config --global core.editor "vim" # Vimgit config --global core.editor "nano" # Nanoマージツールの設定
Section titled “マージツールの設定”実践例:
# マージツールを設定git config --global merge.tool vimdiffgit config --global mergetool.vimdiff.cmd 'vimdiff "$LOCAL" "$REMOTE" "$MERGED"'
# マージツールを使用git mergetoolhooksとカスタマイズのポイント:
- Git Hooks: コミット、プッシュなどのイベントで自動実行
- pre-commitフック: コミット前のチェック(lint、テストなど)
- commit-msgフック: コミットメッセージの形式チェック
- pre-pushフック: プッシュ前のチェック(テスト、ビルドなど)
- Husky: Git hooksを簡単に管理
- エイリアス: よく使うコマンドの短縮
- 設定のカスタマイズ: エディタ、マージツールなどの設定
適切にhooksとカスタマイズを使用することで、開発効率と品質を向上させることができます。