Skip to content

Instantly share code, notes, and snippets.

@ecampidoglio ecampidoglio/
Last active Aug 22, 2019

What would you like to do?
A client-side Git hook that checks the length of the commit message.
# 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.
function printWarning {
printf >&2 "${YELLOW}$message${DEFAULT}\n"
function printNewline {
printf "\n"
function captureUserInput {
# Assigns stdin to the keyboard
exec < /dev/tty
function confirm {
read -p "$question [y/n]"$'\n' -n 1 -r
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."
confirm "Do you want to modify the message in your editor or just commit it?"
if [[ $REPLY =~ ^[Yy]$ ]]; then
$EDITOR $messageFilePath
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.