Skip to content
Permalink
Browse files
Add sample shell script to commit-msg script
To ensure commit messages are written with a clear description, a
sample shell script is added to the commit-msg script. The script
checks if the commit message follows the rules set on
https://chris.beams.io/posts/git-commit/#seven-rules and aborts the
commit if the rules are broken.

The script should not check git commit messages that has the '-m' tag,
since the tag can be used for commits that does not require
extensive explanation.
  • Loading branch information
jeea2 committed Nov 15, 2019
1 parent e4d3212 commit 78ccf8675df1ac6f3d1bd1c76671c23fb5a044f2
Showing 1 changed file with 39 additions and 0 deletions.
@@ -0,0 +1,39 @@
#!/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"


0 comments on commit 78ccf86

Please sign in to comment.