Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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