Skip to content

Instantly share code, notes, and snippets.

@miladvafaeifard
Last active January 28, 2019 08:35
Show Gist options
  • Save miladvafaeifard/593f82b7368c0b66257835a1f9668417 to your computer and use it in GitHub Desktop.
Save miladvafaeifard/593f82b7368c0b66257835a1f9668417 to your computer and use it in GitHub Desktop.
The hook will check if the current branch is a story branch.
#!/bin/sh
#
# The hook will check if the current branch is a story branch.
#
# If it is then it will check the commit message if it contains the correct prefix.
#
# Correct commit message form:
# [STORY-ID] A commit message
#
# If the commit message does not follow the from above the hook will fix it.
#
# case 1:
# STORY-ID A commit message > [STORY-ID] a commit message
#
# case 2:
# A commit message > [STORY-ID] a commit message
#
branch=$(git symbolic-ref --short HEAD)
id="___ID___"
prefix=$(echo $branch | sed -n 's/^\('"$id"'-[0-9]\+\).*/\1/p')
if test "$prefix" != ""; then
prefix_exists=$(grep -i "^\[$prefix\]" $1)
if test "$prefix_exists" = ""; then
temp="$1.tmp"
prefix_exists=`grep -i "^$prefix" $1`
if test "$prefix_exists" != ""; then
sed "s/^$prefix/\[$prefix\]/" $1 > $temp
mv $temp $1
echo -e "INFO: Commit message prefix corrected to [$prefix]"
else
sed "s/^/\[$prefix\] /" $1 > $temp
mv $temp $1
echo -e "INFO: Commit message prefix [$prefix] inserted."
fi
fi
else
echo -e "INFO: Not a story branch. Commit message prefix won't be applied."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment