Skip to content

Instantly share code, notes, and snippets.

@agnivade
Last active October 19, 2021 08:33
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save agnivade/67b42d664ece2d4210c7 to your computer and use it in GitHub Desktop.
Save agnivade/67b42d664ece2d4210c7 to your computer and use it in GitHub Desktop.
Bash file to check commit message structure
#!/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