LoginSignup
62
51

More than 5 years have passed since last update.

【Github時代のgit hook】issue番号を勝手にcommit commentに入れてくれるやつ

Last updated at Posted at 2013-11-13

$ git commit -m "#0001 mendokusai"

コミットメッセージに#[issue num]やんの面倒ですよね、
たまに何のブランチだか、なんのissueのコミットだかわからなくなって
思わずオクトキャットに涙するなんてことよくあるはず。

おもわずenter押しちゃってうっかりコメント入れなかったりして

amend
$ git commit --amend -m "#1507 やべ、間違えた"

と打ち込む事も少なくないはず

そんな時のために自動化しましょう。
やるべきことは簡単。

1, prepare-commit-msgの用意
2, issue番号付きbranchの作成

prepare-commit-msgの用意

作業フォルダに.git/hooks/prepare-commit-msgを用意

prepare-commit-msgの中身は以下

prepare-commit-msg
#!/bin/sh
#

if [ "$2" == "" ] ; then
mv $1 $1.tmp
echo "issue #`git branch | grep "*" | awk '{print $2}' | sed -e "s/^\([0-9]*\).*/\1/g"`" > $1
cat $1.tmp >> $1
fi

$ chmod +x prepare-commit-msg

issue番号付きbranchの作成

$ git checkout -b 1234_branch

これで大丈夫。

使う

$ git commit

コミットすると

commitMessage

issue  #1234

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch 1_test_branch
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   index.html
#          

こんな感じでできている。
やってることは、コミット前にブランチ名内の数字を読み出して
付与して上げているだけ、下記を参考にさせていただきました。

git branch名に、チケット番号仕込むと、良い事あった。 - 鼻メガネ会長

62
51
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
62
51