Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git hook to prepend ticket number to commit message
#!/usr/bin/env bash
# commit message file
commit_msg_file="$1"
# condition the commit is invoked; none (git commit), message (git commit -m <msg>), template, merge, squash, or commit
# commit_source=$2
# commit hash
# sha1=$3
local_branch="$(git rev-parse --abbrev-ref HEAD)"
get_ticket_nbr="^#?([0-9]{6,10}).*"
valid_ticket_nbr="^[0-9]{6,10}$"
error()
{
echo "GIT PREPARE-COMMIT HOOK ERROR:"
echo "$1"
echo ""
exit 1
}
# check if commit message already has the ticket # prepended
# if not, try to prepend
commit_msg=$(cat "$commit_msg_file")
if [[ ! $commit_msg =~ $get_ticket_nbr ]]
then
if [[ $local_branch =~ $get_ticket_nbr ]]
then
ticket="${BASH_REMATCH[1]}"
if [[ $ticket =~ $valid_ticket_nbr ]]
then
echo "#${ticket} " > "$commit_msg_file"
echo "$commit_msg" >> "$commit_msg_file"
fi
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment