1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#!/bin/bash -eu
#
# An example 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, rename this file to "pre-commit".
function exit_print_error(){
if [[ -z $Indent ]];then
echo "Please install 'indent' and put it in system PATH," > /dev/stderr
echo "or specify it in 'git config hooks.indent'" > /dev/stderr
exit 1
fi
}
trap exit_print_error EXIT
Indent="$(git config hooks.indent)" || Indent=$(which indent 2>/dev/null)
if git rev-parse --verify HEAD >/dev/null 2>&1; then
Against=HEAD
else
# Initial commit: diff against an empty tree object
Against=$(git hash-object -t tree /dev/null)
fi
## Loop on modified files
while read File; do
if [[ ! -r $File ]];then
continue
fi
if [[ ! -f $File ]];then
continue
fi
## Assume we do not have files named "c" or "h"
Ext=${File##*.}
case $Ext in
c|h)
echo "Indenting " $File > /dev/stderr
"$Indent" -kr --no-tabs\
--line-length 80\
--blank-lines-after-declarations\
--preprocessor-indentation 4\
"$File"
;;
esac
echo "Chopping " $File > /dev/stderr
sed -r -i -e 's/[[:blank:]]+$//' $File
git add "$File"
done < <(git diff --cached --name-only $Against)
# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached $Against -- > /dev/stderr
|