fe6bedfb5d
Using stashes in pre-commit hook had the following demerits: - when stash was not needed, it was not created, so some other stash created by user was poped on exit -- this was a source of great confusion; - files with unstaged changes were touched, which caused unwanted side effects with some tools like editors or build systems. This commit changes the way we get the clean source by checking out index tree to temporary dir without modifying working tree. This might be a bit slower for large repositories, but in my tests for this repo it is within 5%. Change-Id: Ic32fc2f52958b63131fcbe363ea47fca13099ea3
47 lines
943 B
Bash
Executable File
47 lines
943 B
Bash
Executable File
#!/bin/bash
|
|
|
|
# install me this way:
|
|
# cp pre-commit "$(git rev-parse --git-dir)/hooks/"
|
|
|
|
|
|
gitdir="$(readlink -f $(git rev-parse --git-dir))"
|
|
|
|
tmpdir=
|
|
cleanup_tmpdir()
|
|
{
|
|
[ -z "$tmpdir" ] || rm -rf -- "$tmpdir"
|
|
exit "$@"
|
|
}
|
|
tmpdir=$(mktemp -dt "${0##*/}.XXXXXXXX")
|
|
trap 'cleanup_tmpdir $?' EXIT
|
|
trap 'clenaup_tmpdir 143' HUP INT QUIT PIPE TERM
|
|
|
|
|
|
|
|
git checkout-index -a --prefix="$tmpdir/"
|
|
cd "$tmpdir"
|
|
|
|
indexed_files()
|
|
{
|
|
git --git-dir="$gitdir" diff --cached --name-only --diff-filter=AM
|
|
}
|
|
|
|
FILES="$(indexed_files | grep -E '\.py$')"
|
|
if [ -n "$FILES" ]; then
|
|
pylint $FILES || STATUS=1
|
|
if grep -nEH --color '(import pdb|pdb.set_trace)' $FILES; then
|
|
echo "Please remove pdb"
|
|
STATUS=1
|
|
fi
|
|
fi
|
|
|
|
FILES="$(indexed_files | grep -E '\.(py|html|js)$')"
|
|
if [ -n "$FILES" ]; then
|
|
if grep -nEH --color '\s+$' $FILES; then
|
|
echo "Please remove trailing spaces"
|
|
STATUS=1
|
|
fi
|
|
fi
|
|
|
|
exit $STATUS
|