Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one configure Android Studio to run a gradle task before submitting a Git commit?

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.

like image 465
Creos Avatar asked Jul 16 '16 22:07

Creos


People also ask

How do I run pre-commit automatically?

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.

What is git pre-commit 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.

How do you enforce a pre-commit hook?

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.

What is pre-commit build?

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.


1 Answers

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
like image 198
VonC Avatar answered Sep 19 '22 23:09

VonC