diff --git a/.githooks/commit-msg b/.githooks/commit-msg new file mode 100755 index 0000000..4696af4 --- /dev/null +++ b/.githooks/commit-msg @@ -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" + +