Skip to content

Instantly share code, notes, and snippets.

@mbrowniebytes
Created May 12, 2021 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbrowniebytes/99802029f3f4f550ca7825ed95ccdc96 to your computer and use it in GitHub Desktop.
Save mbrowniebytes/99802029f3f4f550ca7825ed95ccdc96 to your computer and use it in GitHub Desktop.
Git hook to ensure commit starts with ticket number
#!/usr/bin/env bash
# commit message file
commit_msg_file="$1"
valid_comment_prefix="^#?[0-9]{6,10} .+"
error()
{
cp "$commit_msg_file" "_gitmsg.saved.txt"
echo "GIT COMMIT-MSG HOOK ERROR:"
echo "$1"
echo ""
echo "Original checkin message has been stored in '_gitmsg.saved.txt'"
echo "Your commit will be rejected."
echo "You should update your commit and try again."
exit 1
}
commit_msg=$(cat "$commit_msg_file")
if [[ ! $commit_msg =~ $valid_comment_prefix ]]
then
error "Comment must match: $valid_comment_prefix"
fi
# additional checks based on comment lines
#lineno=0
#while read -r line
#do
# # Ignore comment lines (don't count line number either)
# [[ "$line" =~ ^#.* ]] && continue
#
# let lineno+=1
# length=${#line}
#
# if [[ $lineno -eq 1 ]]
# then
# # Subject line tests
# [[ ! "$line" =~ $valid_comment_prefix ]] && error "Comment must match: $valid_comment_prefix"
# # else
# # Rules related to the commit message body
# [[ $length -gt 100 ]] && error "Wrap each line at 100 characters"
# fi
#
#done < "$commit_msg_file"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment