| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# Called by "git commit". The hook will run several checks on the |
| 4 |
# source-code. If it exits with a non-zero status, the commit will |
| 5 |
# be halted. |
| 6 |
# |
| 7 |
# To enable this hook, link this file to ".git/hooks/pre-commit": |
| 8 |
# ln -s ../../pre-commit.sh .git/hooks/pre-commit |
| 9 |
# |
| 10 |
RED='\033[1;31m' |
| 11 |
GREEN='\033[1;32m' |
| 12 |
NC='\033[0m' |
| 13 |
|
| 14 |
if git rev-parse --verify HEAD >/dev/null 2>&1 |
| 15 |
then |
| 16 |
against=HEAD |
| 17 |
else |
| 18 |
# Initial commit: diff against an empty tree object |
| 19 |
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 20 |
fi |
| 21 |
|
| 22 |
# If you want to allow non-ASCII filenames set this variable to true. |
| 23 |
allownonascii=$(git config --bool hooks.allownonascii) |
| 24 |
|
| 25 |
# Redirect output to stderr. |
| 26 |
exec 1>&2 |
| 27 |
|
| 28 |
# Cross platform projects tend to avoid non-ASCII filenames; prevent |
| 29 |
# them from being added to the repository. We exploit the fact that the |
| 30 |
# printable range starts at the space character and ends with tilde. |
| 31 |
if [ "$allownonascii" != "true" ] && |
| 32 |
# Note that the use of brackets around a tr range is ok here, (it's |
| 33 |
# even required, for portability to Solaris 10's /usr/bin/tr), since |
| 34 |
# the square bracket bytes happen to fall in the designated range. |
| 35 |
test $(git diff --cached --name-only --diff-filter=A -z ${against} | |
| 36 |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 |
| 37 |
then |
| 38 |
cat <<\EOF |
| 39 |
Error: Attempt to add a non-ASCII file name. |
| 40 |
|
| 41 |
This can cause problems if you want to work with people on other platforms. |
| 42 |
|
| 43 |
To be portable it is advisable to rename the file. |
| 44 |
|
| 45 |
If you know what you are doing you can disable this check using: |
| 46 |
|
| 47 |
git config hooks.allownonascii true |
| 48 |
EOF |
| 49 |
exit 1 |
| 50 |
fi |
| 51 |
|
| 52 |
# If there are whitespace errors, print the offending file names and fail. |
| 53 |
git diff-index --check --cached $against -- |
| 54 |
|
| 55 |
err=0 |
| 56 |
msg='' |
| 57 |
|
| 58 |
run(){ |
| 59 |
tmp=$(mktemp -t cv-precommit) |
| 60 |
echo -n "Running '${*}': " |
| 61 |
"$@" >"$tmp" 2>&1 |
| 62 |
ret=$? |
| 63 |
if [ $ret != 0 ]; then |
| 64 |
let "err = $err + $ret" |
| 65 |
msg="${msg}\n\t'${*}' failed" |
| 66 |
echo -e "${RED}FAILED${NC}" |
| 67 |
echo "Tried: ${*}" |
| 68 |
cat "$tmp" |
| 69 |
rm -f "$tmp" |
| 70 |
else |
| 71 |
echo -e "${GREEN}PASSED${NC}" |
| 72 |
fi |
| 73 |
return $ret |
| 74 |
} |
| 75 |
|
| 76 |
START_TIME=$SECONDS |
| 77 |
run make check |
| 78 |
run make pep8 |
| 79 |
run make pyflakes |
| 80 |
run make pylint |
| 81 |
|
| 82 |
DURATION=$((SECONDS - START_TIME)) |
| 83 |
echo "pre-commit checks took ${DURATION} seconds." |
| 84 |
echo |
| 85 |
|
| 86 |
if [ ${err} != 0 ]; then |
| 87 |
echo -e "${RED}ERROR: Some checks failed. Commit halted. [${err}]${NC}" |
| 88 |
echo -e "${RED}Failed Tests: ${msg}${NC}" |
| 89 |
fi |
| 90 |
exit ${err} |