Last active
January 20, 2022 12:29
-
-
Save aasanchez/8e7d38ba1380fd3e8797e5600b2def18 to your computer and use it in GitHub Desktop.
Making Commit Messages Greate Again
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/env bash | |
# Making Commit Messages Great Again | |
# Following https://www.conventionalcommits.org/ and | |
# http://chris.beams.io/posts/git-commit/#seven-rules | |
# https://regex101.com/r/Up1Y06/5 | |
COMMIT_MSG_FILE=$1 | |
START_LINE=$(head -n1 "$COMMIT_MSG_FILE") | |
PATTERN="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)+(\([a-zA-Z0-9 -_\/]+\))?!?:\s[a-zA-Z0-9 -_\/]+$" | |
if ! [[ "$START_LINE" =~ $PATTERN ]]; then | |
echo -e "\n\033[1;92mmessage:\033[0m $START_LINE\033[1;91m => does not obey convention\033[0m" | |
echo "Please follow https://www.conventionalcommits.org/ convention" | |
exit 1 | |
fi | |
# Separate subject from body with a blank line | |
if [ "$(< "$COMMIT_MSG_FILE" wc -l)" -gt 1 ]; then | |
SECONDLINE=$(sed -n '2p' < "$COMMIT_MSG_FILE") | |
if [[ -n $SECONDLINE ]]; then | |
echo "Separate subject from body with a blank line" | |
echo "Visit https://chris.beams.io/posts/git-commit/#separate for more information" | |
exit 1 | |
fi | |
fi | |
# Limit the subject line to 60 characters | |
if [ "$(echo -n "$START_LINE" | wc -c)" -gt 60 ]; then | |
echo "Limit the subject line to 50 characters" | |
echo "Visit https://chris.beams.io/posts/git-commit/#limit-50 for more information" | |
exit 1 | |
fi | |
# Capitalize the subject line | |
## First we must guaranty that the subject line only have one ":" | |
if [ "$(tr -dc ':' <<<"$START_LINE" | wc -c)" -gt 1 ]; then | |
echo "Only one ':' is allow" | |
exit 1 | |
fi | |
SUBJECT=$(cut -d':' -f2 <<<"$START_LINE" | xargs) | |
if [[ "${SUBJECT:0:1}" =~ ^[[:lower:]]+$ ]]; then | |
echo "Subject must be capitalize: $SUBJECT" | |
echo "Visit https://chris.beams.io/posts/git-commit/#capitalize for more information" | |
exit 1 | |
fi | |
# Do not end the subject line with a period | |
if [[ "${START_LINE: -1}" == "." ]]; then | |
echo "Do not end the subject line with a period" | |
echo "Visit https://chris.beams.io/posts/git-commit/#end for more information" | |
exit 1 | |
fi | |
# Use the imperative mood in the subject line | |
## This is cover by conventionalcommits.org | |
# Wrap the body at 72 characters | |
i=0 | |
while IFS= read -r line; do | |
((i=i+1)) | |
if [ "$(echo -n "$line" | wc -c)" -gt 72 ]; then | |
CHARECTERS=$(echo -n "$line" | wc -c) | |
echo "Wrap the body at 72 characters" | |
echo "Found $CHARECTERS on line $i" | |
echo "Visit https://chris.beams.io/posts/git-commit/#wrap-72 for more information" | |
exit 1 | |
fi | |
done < "$COMMIT_MSG_FILE" | |
# Use the body to explain what and why vs. how | |
## Imposible to validate via bash script. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment