Created
May 12, 2021 15:33
-
-
Save mbrowniebytes/8ce98b21cf57d87955fa5ca596228c4a to your computer and use it in GitHub Desktop.
Git hook to prepend ticket number to commit message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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