Last active
November 25, 2022 00:38
-
-
Save chadmaughan/5889802 to your computer and use it in GitHub Desktop.
A git pre commit hook that runs the test task with the gradle wrapper
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
#!/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 | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is working on the changes you have staged, right after you hit enter on "git commit" and right before the commit is created. It runs
./gradlew test
on the code from the latest commit + these staged changes.The working directory is what's in your project directory, independent from git. You could say that the working directory is "the green (staged) stuff, the red (unstaged) stuff and all unchanged files".
Now, when you have some staged change that makes the tests fail and you fix it, but do not add the change to the staging area, then running
/.gradlew test
would still work because Gradle runs against the working directory. But both changes have to be committed together. That's why this pre-commit hook stashes "the red stuff" first, so:working directory == unchanged files + staged files aka "the green stuff"