LoginSignup
4
3

More than 5 years have passed since last update.

[Git][Bash][SourceTree] コミット時に自動でコミットメッセージの先頭にブランチ名(≒Issue ID)を挿入する

Last updated at Posted at 2017-09-10

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

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3