Last active
August 22, 2019 20:33
-
-
Save ecampidoglio/e380abe1c2556d05d302 to your computer and use it in GitHub Desktop.
A client-side Git hook that checks the length of the commit message.
This file contains hidden or 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
#!/bin/sh | |
# | |
# A hook script that checks the length of the commit message. | |
# | |
# Called by "git commit" with one argument, the name of the file | |
# that has the commit message. The hook should exit with non-zero | |
# status after issuing an appropriate message if it wants to stop the | |
# commit. The hook is allowed to edit the commit message file. | |
DEFAULT="\033[0m" | |
YELLOW="\033[1;33m" | |
function printWarning { | |
message=$1 | |
printf >&2 "${YELLOW}$message${DEFAULT}\n" | |
} | |
function printNewline { | |
printf "\n" | |
} | |
function captureUserInput { | |
# Assigns stdin to the keyboard | |
exec < /dev/tty | |
} | |
function confirm { | |
question=$1 | |
read -p "$question [y/n]"$'\n' -n 1 -r | |
} | |
messageFilePath=$1 | |
message=$(cat $messageFilePath) | |
firstLine=$(printf "$message" | sed -n 1p) | |
firstLineLength=$(printf ${#firstLine}) | |
test $firstLineLength -lt 51 || { | |
printWarning "Tip: the first line of the commit message shouldn't be longer than 50 characters and yours was $firstLineLength." | |
captureUserInput | |
confirm "Do you want to modify the message in your editor or just commit it?" | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
$EDITOR $messageFilePath | |
fi | |
printNewline | |
exit 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment