我使用Android Studio(itellij)IDE和内置支持Git作为VCS。
是否有任何钩子/选项来预测在成功运行某个gradle任务时提交提交。正如您可能怀疑的那样,如果任何单元测试失败,我会尝试自动运行整个单元测试套件并阻止本地提交通过。
答案 0 :(得分:3)
仍然需要调查git commit hooks如何工作
在
hooks
文件夹中创建.git
目录,只需将文件命名为“pre-commit
”
(如果你不在Windows上,也可以执行它)
但正如your linked question sschuberth中提到的那样:
在
pre-commit
钩子中添加长时间运行的任务通常是一个坏主意,因为它阻止你工作 此类检查应在CI系统上完成,该系统会阻止破坏测试的提交的合并
换句话说,使用预接收挂钩提交并推送到中间存储库,如果整个单元测试套件在任何时候发生故障,它将拒绝您的推送。
OP Creos in the comments following pre-commit hook gist example Chad Maughan指出作为一个好模板:
#!/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