Last active
August 10, 2021 07:49
-
-
Save Brandon-Gui123/eb73eee0049bc73d3be9d511a4487edd to your computer and use it in GitHub Desktop.
A Bash script that you can run to check if your commit message conforms to a specific format.
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
#!/usr/bin/bash | |
# How to use this Bash script: | |
# You can simply copy and paste the contents of this Bash script | |
# to your repository's .git/hooks/commit-msg file. | |
# Alternatively, copy and paste this file in your repository's .git/hooks | |
# directory and in commit-msg, type the following to execute this script: | |
# ./.git/hooks/check-commit-message.sh "$1" | |
# Note that the script exits with 0 when no issues are found, and 1 | |
# when issues are found. | |
# debugging options (uncomment set command to enable) | |
# x: Show what the lines looked like after evalution | |
# u: Warn us if we access variables that aren't set with any value | |
# set -xu | |
# check if we have the valid number of arguments | |
if [[ $# -eq 0 ]]; then | |
echo "Missing argument for filename" >> /dev/stderr | |
echo "Usage: $0 <file>" >> /dev/stderr | |
exit 1 | |
fi | |
if [[ $# -gt 1 ]]; then | |
echo "Too many arguments" >> /dev/stderr | |
echo "Usage: $0 <file>" >> /dev/stderr | |
exit 1 | |
fi | |
echo "Running a check on your message..." | |
# commit subject line issues | |
commitSubjectNotProvided=0 | |
commitSubjectExceedLimit=0 | |
commitSubjectHasTrailingPeriod=0 | |
# second line issues | |
secondLinePresent=0 | |
secondLineBlank=0 | |
# commit description issues | |
hasExceedingLines=0 | |
linesExceedingLimit=() | |
linesRead=0 | |
# in order to handle files with no newline at the end, we need to add the additional | |
# check for a non-empty line since read returns a false if a line doesn't end with a newline (?) | |
while read -r currentLine || [[ -n "$currentLine" ]]; do | |
linesRead=$((linesRead + 1)) | |
# ignore lines starting with a pound symbol (#) | |
if [[ "$currentLine" =~ ^\# ]]; then | |
continue; | |
fi | |
# assume first line is commit subject | |
if [[ $linesRead -eq 1 ]]; then | |
# a commit subject must be provided | |
if [[ "$currentLine" =~ ^[[:space:]]+$ ]] || | |
[[ -z "$currentLine" ]]; then | |
commitSubjectNotProvided=1 | |
fi | |
# commit subject shall not exceed 50 characters | |
if [[ ${#currentLine} -gt 50 ]]; then | |
commitSubjectExceedLimit=1 | |
fi | |
# commit subject shall not have a trailing period | |
if [[ "$currentLine" =~ \.[[:space:]]+$ ]]; then | |
commitSubjectHasTrailingPeriod=1 | |
fi | |
# second line of commit message | |
elif [[ $linesRead -eq 2 ]]; then | |
secondLinePresent=1 | |
# the second line shall contain just whitespace characters | |
if [[ "$currentLine" =~ ^[[:space:]]+$ ]] || | |
[[ -z "$currentLine" ]]; then | |
secondLineBlank=1 | |
fi | |
# third line ownwards will be the commit description | |
else | |
# the current line in the commit description should | |
# not exceed 72 characters | |
if [[ ${#currentLine} -gt 72 ]]; then | |
# record all the lines that exceed the 72-character limit | |
hasExceedingLines=1 | |
linesExceedingLimit+=("$currentLine") | |
fi | |
fi | |
done < "$1" | |
commitMessageContainsIssues=0 | |
# issues in the subject of the commit message | |
if [[ $commitSubjectExceedLimit -eq 1 ]] || | |
[[ $commitSubjectHasTrailingPeriod -eq 1 ]] || | |
[[ $commitSubjectNotProvided -eq 1 ]] || | |
[[ $hasExceedingLines -eq 1 ]]; then | |
commitMessageContainsIssues=1 | |
fi | |
# issues in second line of the commit message | |
if [[ $secondLinePresent -eq 1 ]] && [[ $secondLineBlank -ne 1 ]]; then | |
commitMessageContainsIssues=1 | |
fi | |
if [[ $commitMessageContainsIssues -eq 1 ]]; then | |
echo "Commit aborted due to issues with your message." | |
fi | |
if [[ $commitSubjectNotProvided -eq 1 ]]; then | |
echo "- Commit subject not provided" | |
fi | |
if [[ $commitSubjectExceedLimit -eq 1 ]]; then | |
echo "- Commit subject longer than 50 characters" | |
fi | |
if [[ $commitSubjectHasTrailingPeriod -eq 1 ]]; then | |
echo "- Commit subject has a trailing period" | |
fi | |
if [[ $secondLinePresent -eq 1 ]] && [[ $secondLineBlank -ne 1 ]]; then | |
echo "- Second line not blank" | |
fi | |
if [[ $hasExceedingLines -eq 1 ]]; then | |
echo "- Description line(s) longer than 72 chars" | |
echo "" | |
echo "The following line(s) are longer than 72 chars:" | |
for descLine in "${linesExceedingLimit[@]}"; do | |
echo " - $descLine" | |
done | |
fi | |
if [[ $commitMessageContainsIssues -eq 1 ]]; then | |
echo "" | |
echo "Resolve the issues above to continue." | |
exit 1 | |
fi | |
echo "No issues found." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment