7

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.

Creos
  • 2,445
  • 3
  • 27
  • 45
  • Have you tried a pre-commit hook? – intboolstring Jul 17 '16 at 00:07
  • thanks, i actually just found this http://stackoverflow.com/questions/10234192/how-to-write-a-git-pre-commit-hook-that-prevents-committing-of-an-android-projec but still need to investigate how git commit hooks even work – Creos Jul 17 '16 at 00:26

1 Answers1

8

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks, pls add this link to your answer as it's a great template https://gist.github.com/chadmaughan/5889802 – Creos Jul 17 '16 at 16:45
  • @Creos Sure, I have included the pre-commit hook. – VonC Jul 17 '16 at 16:48
  • to answer your question: yes, doing it locally does block you and it sucks but i don't know if my project warrants paying for CI at this stage... i need to look into git hub and see if there are any low-cost or no-cost options for small projects. I just learned about hooks yesterday but I suspect pre-receive won't work on github for free as someone needs to be pay for the CPU cycles on their servers... – Creos Jul 17 '16 at 16:48
  • CI for small projects, linked to GitHub? That would be Travis-CI for instance: https://github.com/mbonaci/mbo-storm/wiki/Integrate-Travis-CI-with-your-GitHub-repo – VonC Jul 17 '16 at 16:50
  • thanks man, checked it out. however, my project is private (small but private repo) and the cheapest option they offer seems to be $129/month so i think i can totally wait the couple of minutes my ~9000 unit tests take to run in parallel on my 8-HT cpu :) – Creos Jul 17 '16 at 17:08
  • for anyone else who might check out this question -- great alternatives to travis and MUCH more reasonably priced for 1-2 private repo projects are drone.io, semaphore, snap-ci, etc. travis is great but they are completely out of touch with market imho, they need an entry level monthly offering in the $15-30 range. – Creos Jul 19 '16 at 00:08