LoginSignup
5
7

More than 5 years have passed since last update.

コミットログにredmineのチケット番号(refs #XXX)が入っていないとコミットできないようにする。

Last updated at Posted at 2015-04-11

やりたいこと

コミットログにredmineのチケット番号(refs #XXX)が入っていないとコミットできないようにしたい。
→ client側のhookを強制的に設定したい。(主にpre-commit, commit-msg)

手順

1 /.git/hooks/commit-msg

コミットメッセージのチェックを行うhookscriptのcommit-msgを使う。

commit-msg.sh
#!/bin/sh

exec 2>&1

function redmine_err() {
    echo -e "\x1b[0;41m ERROR : please input tichet number..\x1b[0;49m";
    echo -e "  example : refs #[0-9]";
    exit 1
}

/bin/grep "refs #[0-9]" $1 > /dev/null || redmine_err;

exit 0

課題

あくまでclient側のhookなので、複数人で開発を行っている場合は各人でhookを設定しなければならない。

うーむ。。。面倒くさい。

打開策1

今回の開発ではソースのbuildを行う際にshellを叩くので、そこに一手間加える。

  1. gitの環境に.githooksという名前のディレクトリの下にhookscript(commit-msg, pre-commit)を登録しておく
  2. 下記のシェルスクリプト(setup.sh)も.githooksの配下に一緒に登録しておく。
  3. buildのシェルでsetup.shを叩いて.git/hooksに.githooks配下のスクリプトをシンボリックリンク。
setup.sh
#!/bin/sh

GITHOOK_DIR=.git/hooks
COMMITMSG=${GITHOOK_DIR}/commit-msg
PRECOMMIT=${GITHOOK_DIR}/pre-commit

git config --list |/bin/grep "commit.template" > /dev/null || \
git config --local commit.template .githooks/git_commit_msg_template

#シンボリックリンクを設定
if [ ! -f .git/hooks/commit-msg ]; then
    echo "### Create symbolic linx ${COMMITMSG}"
    ln -s ../../.githooks/commit-msg ${COMMITMSG}
fi

if [ ! -f ${PRECOMMIT} ]; then
    echo "### Create symbolic linx ${PRECOMMIT}"
    ln -s ../../.githooks/pre-commit ${PRECOMMIT}
fi
refs #
o 

所感

結局client側のhookをcloneした段階で強制的に設定する方法はわからなかった。。
今回たまたまbuild時にshellを叩くようにしていたためclient側(cloneした人)の意思に関係なく設定できたが、完全解決とは言えないので調査続行中٩(’ω’)و init.templatedirを強制するとか。。
# あとperlとかruby、pythonとかでも書き直したい ♪(´ε` )

5
7
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
5
7