在提交 Git 提交之前,可以将 Android Studio 配置为 运行 gradle 任务吗?

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

我正在使用 Android Studio (itellij) IDE 和对 Git 作为 VCS 的内置支持。

是否有任何挂钩/选项可以预测在某些 gradle 任务的成功 运行 上提交提交。正如您可能怀疑的那样,我正在尝试 运行 自动化整个单元测试套件,并在任何单元测试失败时阻止本地提交。

still need to investigate how git commit hooks even work

作为shown in this gist

create a hooks directory in your .git folder and just name the file "pre-commit"

(如果你不在 Windows 上,也让它可执行)

但如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

换句话说,提交并推送到带有预接收挂钩的中间存储库,如果整个单元测试套件在任何时候失败,它将拒绝您的推送。


OP Creos points out the following pre-commit hook gist example by 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