Skip to content

Instantly share code, notes, and snippets.

@rage-shadowman
Forked from fwielstra/pre-push
Last active May 6, 2019 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rage-shadowman/33014d517172f690188f6b7332c2c625 to your computer and use it in GitHub Desktop.
Save rage-shadowman/33014d517172f690188f6b7332c2c625 to your computer and use it in GitHub Desktop.
A pre-push git hook which runs a gradle test task
#!/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
@rage-shadowman
Copy link
Author

Corrected comments and moved set -e to the magic header #/bin/sh -e.

@rage-shadowman
Copy link
Author

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