LoginSignup
43
47

More than 5 years have passed since last update.

GitHubのissue番号を自動でcommitに書き、issue番号がないコミットははじくようにするgit hooksまとめ!

Last updated at Posted at 2014-04-22

①issue番号がないとコミットできない

どうなるかというと。。。

[pall@pall:~](79_imgAddAltTag)$ git st
# On branch 79_imgAddAltTag
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   README.md
#
[pall@kazumasa:~](79_imgAddAltTag)$ git commit -m "test"                               
コミットにチケット情報入れてーーー

となる

やり方

レポジトリの.git/hooks/commit-msgに書く

$ cd レポジトリのトップの階層に
$ vim .git/hooks/commit-msg
$ chmod +x .git/hooks/commit-msg

※実行権を付けないとうまく動きません!!!

#!/bin/sh

#チケットに関するログが記載されているかのチェック
exp="(?:close|closed|closes|fix|fixed|fixes|addresses|references|refs|re|see)"
exp=$exp".?(#[0-9]+(?:(?:[, &]+| *and *)#[0-9]+)*)"
grep -E "$exp" $1 > /dev/null

if [ $? -ne 0 ]; then
    echo 'コミットにチケット情報入れてーーー'
    exit 1
fi

これで完了

②git commit ってするとissue番号が自動付与

どうなるかというと。。

[pall@pall:~](79_imgAddAltTag)$ git commit

      1 refs#79
      2 
      3 # Please enter the commit message for your changes. Lines starting
      4 # with '#' will be ignored, and an empty message aborts the commit.
      5 # On branch 79_imgAddAltTag
      6 # Changes to be committed:
      7 #   (use "git reset HEAD <file>..." to unstage)
      8 #
      9 #   modified:   README.md
     10 #

ってなる。
git commit -m "test"だとうごかない。。
branch名を isuue番号_branch名 にすること!!
1isuue 1branch !!

やり方

$ cd レポジトリのトップの階層に
$ vim .git/hooks/prepare-commit-msg
$ chmod +x .git/hooks/prepare-commit-msg
#!/bin/sh

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

で完了。

参考:

番外編
shではなく、rubyでもかけるって。。

43
47
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
43
47