Last active
October 19, 2021 08:33
-
-
Save agnivade/67b42d664ece2d4210c7 to your computer and use it in GitHub Desktop.
Bash file to check commit message structure
This file contains hidden or 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 | |
#http://chris.beams.io/posts/git-commit/#seven-rules | |
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 "Your subject line exceeds 50 characters." | |
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 "Last character of the subject line must not have punctuation." | |
exit 1 | |
fi | |
elif [ $cnt -eq 2 ]; then | |
# Subject must be followed by a blank line | |
if [ $length -ne 0 ]; then | |
echo "Your subject line follows a non-empty line. Subject lines should always be followed by a blank line." | |
exit 1 | |
fi | |
else | |
# Any line in body must not exceed 72 characters | |
if [ $length -gt 72 ]; then | |
echo "The line \"$line\" exceeds 72 characters." | |
exit 1 | |
fi | |
fi | |
done < "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment