JIRAとの連携用にブランチ名(≒Issue ID)をコミットメッセージに入れるようにする方法メモ
SourceTreeでも動くように commit-msg
を使用しています。
実装例はRubyが多かったですが、自分はほぼRubyユーザーではないのでbashスクリプトでの実装になります。
実装例
・Git Flow利用時には feature/AAA-123 のようになるので、後半のみ利用
・hotfix, feature のみ対象にする。(release, develop, masterは対象外)
・Amend用にすでにコミットメッセージに含まれているときには追加しない
.git/hooks/commit-msg
#!/bin/bash -u
current_branch=$(git branch | grep '*' | sed -e 's/\*//')
# feature or hotfix branch only
if [ ! `echo ${current_branch} | grep -E '(feature/|hotfix/)'` ]; then
exit 0
fi
# e.g. feature/AAA-123 -> AAA-123
issue_id=${current_branch##*/}
# Insert Issue ID at the beginning of the commit message if it doesn't exist
if [ ! `grep "$issue_id" $1` ]; then
sed -i -e '1 s@\(.*\)@'"${issue_id}"' \1@' $1
fi
参考
コミットメッセージにgit issueのIDを入れるgit hookをつくった(Source Tree対応) - IO#blog