tools/pre-commit: Don't use stashes
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
This commit is contained in:
parent
302612003d
commit
fe6bedfb5d
@ -3,11 +3,30 @@
|
|||||||
# install me this way:
|
# install me this way:
|
||||||
# cp pre-commit "$(git rev-parse --git-dir)/hooks/"
|
# cp pre-commit "$(git rev-parse --git-dir)/hooks/"
|
||||||
|
|
||||||
git stash -q --keep-index
|
|
||||||
trap 'git stash pop -q' EXIT
|
|
||||||
|
|
||||||
STATUS=0
|
gitdir="$(readlink -f $(git rev-parse --git-dir))"
|
||||||
FILES="$(git diff --cached --name-only --diff-filter=AM | grep -E '\.py$')"
|
|
||||||
|
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
|
if [ -n "$FILES" ]; then
|
||||||
pylint $FILES || STATUS=1
|
pylint $FILES || STATUS=1
|
||||||
if grep -nEH --color '(import pdb|pdb.set_trace)' $FILES; then
|
if grep -nEH --color '(import pdb|pdb.set_trace)' $FILES; then
|
||||||
@ -16,7 +35,7 @@ if [ -n "$FILES" ]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
FILES="$(git diff --cached --name-only --diff-filter=AM | grep -E '\.(py|html|js)$')"
|
FILES="$(indexed_files | grep -E '\.(py|html|js)$')"
|
||||||
if [ -n "$FILES" ]; then
|
if [ -n "$FILES" ]; then
|
||||||
if grep -nEH --color '\s+$' $FILES; then
|
if grep -nEH --color '\s+$' $FILES; then
|
||||||
echo "Please remove trailing spaces"
|
echo "Please remove trailing spaces"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user