From 68d1d3b4aff40df4cd35f33cf2ecad7d9b9a56ca Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 31 Mar 2010 17:39:00 -0400 Subject: [PATCH] Prototype pre-commit and commit-msg hook scripts These hooks perform some basic commit-time checks to keep history clean. --- commit-msg | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ pre-commit | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100755 commit-msg create mode 100755 pre-commit diff --git a/commit-msg b/commit-msg new file mode 100755 index 000000000..80f497197 --- /dev/null +++ b/commit-msg @@ -0,0 +1,57 @@ +#!/bin/sh +# +# Copy or link this file as ".git/hooks/pre-commit". + +die() { + echo 'commit-msg hook failure' 1>&2 + echo '-----------------------' 1>&2 + echo '' 1>&2 + echo "$@" 1>&2 + exit 1 +} + +#----------------------------------------------------------------------------- +# Check the commit message layout with a simple state machine. + +msg_first() { + len=$(echo -n "$line" | wc -c) + if test $len -eq 0; then + # not yet first line + return + elif test $len -lt 8; then + die 'The first line must be at least 8 characters: +-------- +'"$line"' +-------- +' + elif test $len -gt 78; then + die 'The first line may be at most 78 characters: +------------------------------------------------------------------------------ +'"$line"' +------------------------------------------------------------------------------ +' + else + # first line okay + state=second + fi +} + +msg_second() { + if test "x$line" != "x"; then + die 'The second line must be empty: +'"$line" + else + state=rest + fi +} + +msg_rest() { + false +} + +# Pipe commit message into the state machine. +state=first +cat "$1" | grep -v '^#' | +while read line; do + msg_$state || break +done diff --git a/pre-commit b/pre-commit new file mode 100755 index 000000000..442fe9a9f --- /dev/null +++ b/pre-commit @@ -0,0 +1,43 @@ +#!/bin/sh +# +# Copy or link this file as ".git/hooks/pre-commit". + +die() { + echo 'pre-commit hook failure' 1>&2 + echo '-----------------------' 1>&2 + echo '' 1>&2 + echo "$@" 1>&2 + exit 1 +} + +#----------------------------------------------------------------------------- +# Check for committer identity. +git config --get user.name > /dev/null && +git config --get user.email > /dev/null || +die 'Identity not configured! Use the commands + + git config --global user.name '\''Your Name'\'' + git config --global user.email '\''you@yourdomain.com'\'' + +to introduce yourself to Git before committing.' + +#----------------------------------------------------------------------------- +# Check content that will be added by this commit. + +if git rev-parse --verify -q HEAD > /dev/null; then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +fi + +# Disallow non-ascii file names. The printable range starts at the +# space character and ends with tilde. +if test "$(git diff --cached --name-only --diff-filter=A -z $against | + LC_ALL=C tr -d '[ -~]\0')"; then + die 'Non-ascii file names may not be added: +'"$(git diff --cached --name-only --diff-filter=A $against)" +fi + +# Builtin whitespace checks. +bad=$(git diff-index --check --cached $against --) || die "$bad"