Skip to content

Instantly share code, notes, and snippets.

@saturday06
Created September 7, 2012 05:51
Show Gist options
  • Save saturday06/3663564 to your computer and use it in GitHub Desktop.
Save saturday06/3663564 to your computer and use it in GitHub Desktop.
#!/bin/sh
# -*- coding: utf-8 -*-
# チケット番号付加フック。bleis-hookのパクり
#
# レポジトリの.git/hooks/commit-msgにコピって、chmod 755 .git/hooks/commit-msg としてください
#
# ticket/チケット番号
#
# とか
#
# ticket/チケット番号/適当な文字列
#
# ほげほげ/ticket/チケット番号/ふがふが
#
# とかで作業をしていると、
# コミット時に勝手に refs #チケット番号 が追加されます!!!
# ただ、同じものがすでにどっかに書いてあった場合は追加されないです
# 『ticket/』を別のに変えたい場合は、スクリプトの先頭の
#
# PREFIX=ticket/
#
# を直して下さい。たとえば
#
# PREFIX=t
#
# とすると、 t1234 ブランチで作業した場合にrefs #1234が追加されます
PREFIX=ticket/
commit_msg_path=$1
# ブランチ名を取得
branch_name="`git branch | grep -F "*" | awk '{print $2}'`"
echo $0: branch_name=$branch_name
# issue_idを取得
command="echo $branch_name | grep -E '$PREFIX[0-9]+' | sed 's%.*$PREFIX\\([0-9]\\+\\).*%\\1%'"
issue_id=`eval $command`
# issue_idが取得出来なかったら終了
if [ "$issue_id" = "" ]; then
echo $0: issue_id not found
exit 0
fi
# コミットメッセージのヘッダーを組み立て
echo $0: issue_id=$issue_id
header="refs #$issue_id"
echo $0: header=$header
# すでに$headerがコミットメッセージに含まれていたら終了
cat $commit_msg_path | grep --only-matching "refs #[0-9]+" | grep "^$header$"
if [ $? -eq 0 ]; then
echo $0: header=$header already written
exit 0
fi
# コミットメッセージを加工
echo $0: commit_msg_path=$commit_msg_path
temp_commit_msg_path=$commit_msg_path.foobar.$$.tmp
cp $commit_msg_path $temp_commit_msg_path
echo "$header " | tr -d '\n' | cat - $temp_commit_msg_path > $commit_msg_path
rm $temp_commit_msg_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment