-
-
Save rage-shadowman/33014d517172f690188f6b7332c2c625 to your computer and use it in GitHub Desktop.
A pre-push git hook which runs a gradle test task
This file contains hidden or 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
#!/bin/sh -e | |
# | |
# to install this hook, create a symbolic link in the projects .git/hooks folder | |
# | |
# i.e. - from the .git/hooks directory, run | |
# $ ln -s /location/of/git-hooks/pre-push.sh pre-push | |
# | |
# to skip the tests, run with the --no-verify argument | |
# i.e. - $ 'git push --no-verify' | |
# stash any unstaged changes | |
echo "Stashing non-commited changes now" | |
if git status | grep -q "working tree clean" | |
then | |
export STASHED="false" | |
else | |
export STASHED="true" | |
git stash push --include-untracked --message "prePush $(date)" | |
fi | |
# unstash the unstashed changes (if any) on exit or interrupt | |
unstash() { | |
RETVAL="$?" | |
echo | |
echo "Unstashing any non-commited changes now" | |
if [ "$STASHED" = "true" ] | |
then | |
git stash pop --index | |
fi | |
echo | |
echo | |
if [ "$RETVAL" -ne 0 ] | |
then | |
echo "Git pre-push hook failed verification. Aborting push." | |
else | |
echo "Git pre-push hook succeeded. Pushing changes now." | |
fi | |
echo | |
} | |
trap unstash EXIT | |
# run the tests with the gradle wrapper | |
if ./gradlew tasks --all | grep -qw '^integrationTest' | |
then | |
echo "Running integration tests" | |
./gradlew clean integrationTest | |
echo "All good" | |
else | |
echo "Running build" | |
./gradlew clean build | |
echo "All good" | |
fi |
Updated per my personal use-case. Run integration tests if they exist, otherwise run build. Don't pollute the stash (drop the stashed changes after unstashing them).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Corrected comments and moved
set -e
to the magic header#/bin/sh -e
.