[git-tools] Improvements on prepare-commt-msg hook

prepare-commit-hook now uses `git symbolic-ref HEAD` instead of reading
$GIT_DIR/HEAD directly. This seems to be a more portable solution.

Thanks to Chris (cs278/ToonArmy) for the suggestion.
This commit is contained in:
Igor Wiedler 2010-03-13 13:04:44 +01:00
parent f9192bed79
commit 6a9304021f

View file

@ -10,15 +10,25 @@
# #
# ln -s ../../git-tools/hooks/prepare-commit-msg \\ # ln -s ../../git-tools/hooks/prepare-commit-msg \\
# .git/hooks/prepare-commit-msg # .git/hooks/prepare-commit-msg
#
# Make sure it is executable.
# strip off ref: refs/heads/ # get branch name
branch="$(cat $GIT_DIR/HEAD | sed 's/ref: refs\/heads\///g')" branch="$(git symbolic-ref HEAD)"
# exit if no branch name is present
# (eg. detached HEAD)
if [ $? -ne 0 ]
then
exit
fi
# strip off refs/heads/
branch="$(echo "$branch" | sed "s/refs\/heads\///g")"
# add [branchname] to commit message
# * only run when normal commit is made (without -m or -F; # * only run when normal commit is made (without -m or -F;
# not a merge, etc.) # not a merge, etc.)
# * also make sure the branch name begins with bug/ or feature/ # * also make sure the branch name begins with bug/ or feature/
if [ "$2" = "" ] && [ $(echo "$branch" | grep -e '^\(bug\|feature\)/') ]; then if [ "$2" = "" ] && [ $(echo "$branch" | grep -e "^\(bug\|feature\)/") ]
echo "[$branch] $(cat $1)" > "$1" then
echo "[$branch] $(cat "$1")" > "$1"
fi fi