Skip to content

Instantly share code, notes, and snippets.

@sanscore
Last active July 10, 2017 18:26
Show Gist options
  • Save sanscore/095d5fff2fb5c7bd4a71d447c92540fe to your computer and use it in GitHub Desktop.
Save sanscore/095d5fff2fb5c7bd4a71d447c92540fe to your computer and use it in GitHub Desktop.
git commit-msg hook to verify the line lengths of the message
#!/usr/bin/env bash
#
# Usage:
# Copy to your /usr/share/git-core/templates/hooks directory,
# and to any existing git repos.
#
# Copy to existing git repos (except those with existing 'commit-msg'):
# find $HOME -name '.git' -type d -exec test '!' -e "{}/hooks/commit-msg" ';' \
# -exec cp /usr/share/git-core/templates/hooks/commit-msg "{}/hooks" ';'
#
# Copy to ALL existing git repos (overwrite existing 'commit-msg'):
# find $HOME -name '.git' -type d \
# -exec cp /usr/share/git-core/templates/hooks/commit-msg "{}/hooks" ';'
#
# Read commit message,
# print line number and line
# put hold each line number, check the length of each line, and print
# a message if the line is too long.
len_check() {
local long_lines="$(cat "$1" \
| sed = \
| sed -nEe '
h;n;
2 { /.{51,}/b toolong
}
4 { /.{1,}/b toolong
}
6,/# ------/ { /.{73,}/b toolong
}
b
:toolong
/^#/d
2i\
Line too long (limit=50)
4i\
Line too long (limit=0)
6,$ {
i\
Line too long (limit=72)
}
H
x
s/\n/: /
s/^/ /
p')"
if [[ -n "${long_lines}" ]]
then
echo "${long_lines}"
exit 1
fi
}
len_check "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment