はじめに
マスターブランチと、開発用の作業ブランチではコミットの粒度が異なるため、異なるテンプレートを用いたい。フックスクリプトを用いてテンプレートファイルを切り替えてみます。
前提
テンプレートファイル、およびその切り替えを行うフックスクリプトが、作業ディレクトリ内に以下のように配置されているものとします。
./work_directory
├── .git
│ ├── hooks
│ │ └── prepare-commit-msg
...省略
│
├── .templates
│ ├── develop_template.txt
│ └── master_template.txt
│
...省略
prepare-commit-msg
フックスクリプトの内容は、以下のようになります(実行権が必要です)。
prepare-commit-msg
#! /bin/bash
# 現在のブランチ名を取得する
branchName=$(git rev-parse --abbrev-ref HEAD)
template_content=""
if [[ "$branchName" == "master" ]]; then
template_content=$(cat .templates/master_template.txt)
elif [[ "$branchName" == "develop" ]]; then
template_content=$(cat .templates/develop_template.txt)
fi
mv $1 $1.tmp
echo "$template_content" > $1
おわりに
作業ディレクトリに対して設定をしました。global
に設定したい場合は、パスを適切に変更してください。