From fe6bedfb5dc360ce601af0f4a049d620b5992e77 Mon Sep 17 00:00:00 2001 From: "Ivan A. Melnikov" Date: Mon, 24 Jun 2013 12:34:20 +0400 Subject: [PATCH] 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 --- tools/pre-commit | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/tools/pre-commit b/tools/pre-commit index 4201b88a..c6410c7c 100755 --- a/tools/pre-commit +++ b/tools/pre-commit @@ -3,11 +3,30 @@ # install me this way: # cp pre-commit "$(git rev-parse --git-dir)/hooks/" -git stash -q --keep-index -trap 'git stash pop -q' EXIT -STATUS=0 -FILES="$(git diff --cached --name-only --diff-filter=AM | grep -E '\.py$')" +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 @@ -16,7 +35,7 @@ if [ -n "$FILES" ]; then 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 grep -nEH --color '\s+$' $FILES; then echo "Please remove trailing spaces"