Created
March 9, 2017 20:28
-
-
Save benesch/8896950afffef09d721ab60c029c8c6f to your computer and use it in GitHub Desktop.
cockroach pre-push hook to run `make check`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
[[ "$(git config --bool hooks.checkBeforePush)" = true ]] || exit 0 | |
# Redirect output to stderr. | |
exec >&2 | |
z40=0000000000000000000000000000000000000000 | |
checked_trees="$(git rev-parse --git-dir)/checked_trees" || { | |
echo "unable to determine git directory; aborting push" | |
exit 1 | |
} | |
original_ref="$(git symbolic-ref --quiet --short HEAD)" || original_ref="$(git rev-parse HEAD)" || { | |
echo "unable to determine current ref; aborting push" | |
exit 1 | |
} | |
old_stash="$(git rev-parse --quiet --verify refs/stash)" || { | |
echo "unable to retrieve current stash sha; aborting push" | |
exit 1 | |
} | |
git stash save --include-untracked --quiet "pre-push temporary stash" || { | |
echo "unable to stash current changes; aborting push" | |
exit 1 | |
} | |
new_stash=$(git rev-parse --quiet --verify refs/stash) || { | |
echo "unable to retrieve stashed sha; aborting push" | |
exit 1 | |
} | |
cleanup() { | |
git checkout --quiet "$original_ref" || { | |
echo "unable to return to original ref; aborting push" | |
exit 1 | |
} | |
[[ "$old_stash" == "$new_stash" ]] || git stash pop --index --quiet > /dev/null || { | |
echo "unable to pop original changes; aborting push" | |
exit 1 | |
} | |
} | |
trap cleanup INT EXIT | |
while read local_ref local_sha remote_ref remote_sha | |
do | |
[[ "$local_sha" = $z40 ]] && exit 0 | |
tree="$(git rev-parse "$local_sha^{tree}")" || { | |
echo "unable to determine tree for pushed ref $local_ref ($local_sha); aborting push" | |
exit 1 | |
} | |
grep --quiet -F "$tree" "$checked_trees" && exit 0 | |
git checkout --quiet "$local_ref" || { | |
echo "unable to checkout $local_ref ($local_sha); aborting push" | |
exit 1 | |
} | |
make check || { | |
echo "make check failed; aborting push" | |
exit 1 | |
} | |
echo "$tree" >> "$checked_trees" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment