Skip to content

Instantly share code, notes, and snippets.

@strootman
Forked from chadmaughan/pre-commit.sh
Last active May 20, 2019 03:41
Show Gist options
  • Save strootman/39c46a178de639f62f74 to your computer and use it in GitHub Desktop.
Save strootman/39c46a178de639f62f74 to your computer and use it in GitHub Desktop.
A pre-push git hook which runs a gradle test task
#!/bin/sh
# this hook is in SCM so that it can be shared
# to install it, create a symbolic link in the projects .git/hooks folder
#
# i.e. - from the .git/hooks directory, run
# $ ln -s ../../git-hooks/pre-commit.sh pre-commit
#
# to skip the tests, run with the --no-verify argument
# i.e. - $ 'git commit --no-verify'
# stash any unstaged changes
git stash -q --keep-index
# run the tests with the gradle wrapper
./gradlew test -q
# Another possibility could be to run an aggregate test report task and automatically open the generated html
# store the last exit code in a variable
RESULT=$?
# unstash the unstashed changes
git stash pop -q
# return the './gradlew test' exit code
exit $RESULT
@fwielstra
Copy link

I've found this script on the Google; forked and made a few changes:

  • use a named stash specifically for pre-push, to avoid popping any other stashes that might be there
  • trap exits and properly restore the stash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment