Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
WebsiteProject/.githooks/commit-msg
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
39 lines (35 sloc)
1.24 KB
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
#!/bin/bash | |
#found in https://www.reddit.com/r/bash/comments/8inq6i/validate_git_commit_message/, contributed by user frakman1 | |
#rules based on https://chris.beams.io/posts/git-commit/#seven-rules | |
echo -e "\e[32m--- Checking Commit Message ---\e[0m"; | |
cnt=0 | |
while IFS='' read -r line || [[ -n "$line" ]]; do | |
cnt=$((cnt+1)) | |
length=${#line} | |
if [ $cnt -eq 1 ]; then | |
# Checking if subject exceeds 50 characters | |
if [ $length -gt 50 ]; then | |
echo -e "\e[31m*** Subject line exceeds 50 characters ***\e[0m"; | |
exit 1 | |
fi | |
i=$(($length-1)) | |
last_char=${line:$i:1} | |
# Last character must not have a punctuation | |
if [[ ! $last_char =~ [0-9a-zA-Z] ]]; then | |
echo -e "\e[31m*** Last character of subject line must not contain punctuation ***\e[0m"; | |
exit 1 | |
fi | |
elif [ $cnt -eq 2 ]; then | |
# Subject must be followed by a blank line | |
if [ $length -ne 0 ]; then | |
echo -e "\e[31m*** Current subject line follows a non-empty line. Subject lines should always be followed by a blank line ***\e[0m"; | |
exit 1 | |
fi | |
else | |
# Any line in body must not exceed 72 characters | |
if [ $length -gt 72 ]; then | |
echo -e "\e[31m*** Line \"$line\" exceeds 72 characters ***\e[0m"; | |
exit 1 | |
fi | |
fi | |
done < "$1" | |