Prototype pre-commit and commit-msg hook scripts
These hooks perform some basic commit-time checks to keep history clean.
This commit is contained in:
commit
68d1d3b4af
57
commit-msg
Executable file
57
commit-msg
Executable file
@ -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
|
43
pre-commit
Executable file
43
pre-commit
Executable file
@ -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"
|
Loading…
Reference in New Issue
Block a user