I'm using Android Studio (itellij) IDE and the built-in support for Git as VCS.
Is there any hook / option to predicate submitting the commit on a successful run of some gradle task. As you may suspect, I'm trying to automate running the entire unit test suite and blocking the commit locally from going through if any unit test fails.
If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook.
The pre-commit script is executed every time you run git commit before Git asks the developer for a commit message or generates a commit object. You can use this hook to inspect the snapshot that is about to be committed.
If you want enforcement, use an update hook in the central repo. If the hook is doing per-commit verification, you can still provide a pre-commit hook; developers will likely adopt it voluntarily, so that they can find out right away when they've done something wrong, rather than waiting until they try to push.
git/hooks/pre-commit) will clone the current git folder into a temporary folder, apply the staged (cached) changes and try to (go) build the project. Currently it only build go projects, but that's easily modified. If the build fails, the commit will fail as well.
still need to investigate how git commit hooks even work
As shown in this gist
create a
hooks
directory in your.git
folder and just name the file "pre-commit
"
(make it executable too, if you are not on Windows)
But as mentioned in your linked question by sschuberth:
Adding long-running tasks in a
pre-commit
hook generally is a bad idea as it blocks you from working.
Such checks should be done on a CI system that gates merges of commits that break tests
In other words, commit and push to an intermediate repo with a pre-receive hook, which will reject your push if the entire unit test suite fails at any point.
The OP Creos points out in the comments the following pre-commit hook gist example by Chad Maughan, as a good template:
#!/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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With