The function `die` was redundantly implemented in various files. Move the function into .local/bin/helpers.sh and source that where previously implemented. Also prepend the program's name to the message and always terminate the message with a newline. The newline was previously needed for a small but unnecessary hack that prevented the need of the `[ -z "$1" ]` test.
57 lines
1.7 KiB
Bash
Executable File
57 lines
1.7 KiB
Bash
Executable File
#!/bin/sh
|
|
#
|
|
# A hook script to verify what is about to be committed.
|
|
# Called by "git commit" with no arguments. The hook should exit with non-zero
|
|
# status after issuing an appropriate message if it wants to stop the commit.
|
|
#
|
|
# To enable this hook, save this file in ".git/hooks/pre-commit".
|
|
|
|
# Source die()
|
|
. "$HOME"/.local/bin/helpers.sh
|
|
|
|
if git rev-parse --verify HEAD >/dev/null 2>&1; then
|
|
against=HEAD
|
|
else
|
|
# Initial commit: diff against an empty tree object
|
|
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
|
|
fi
|
|
|
|
# Redirect output to stderr.
|
|
exec 1>&2
|
|
|
|
# Check that all filenames include only ASCII characters.
|
|
allownonascii=$(git config --bool hooks.allownonascii)
|
|
if [ "$allownonascii" != "true" ]; then
|
|
# We exploit the fact that the printable range starts at the space character
|
|
# and ends with tilde.
|
|
# Note that the use of brackets around a tr range is ok here, (it's
|
|
# even required, for portability to Solaris 10's /usr/bin/tr), since
|
|
# the square bracket bytes happen to fall in the designated range.
|
|
num_nonascii=$(
|
|
git diff --cached --name-only --diff-filter=AR -z $against \
|
|
| LC_ALL=C tr -d '[ -~]\0' \
|
|
| wc -c
|
|
)
|
|
if [ $num_nonascii != 0 ]; then
|
|
die "Rename files with ASCII characters only, or enable hooks.allownonascii"
|
|
fi
|
|
fi
|
|
|
|
# Check for whitespace errors.
|
|
if ! git diff-index --check --cached $against --; then
|
|
die
|
|
fi
|
|
|
|
# Check that added symlinks are not broken
|
|
git diff --staged --name-only --diff-filter=AR $against \
|
|
| {
|
|
broken=0
|
|
while read -r line; do
|
|
if [ -h "$line" ] && [ ! -e "$line" ]; then
|
|
broken=1
|
|
printf "%s\n" "$line: Broken symlink" >&2
|
|
fi
|
|
done
|
|
(( ! broken )) || die
|
|
}
|