Git预推钩
我想在每次git push之前运行一个unit testing,如果testing失败,取消push,但是我甚至找不到pre-push hook,只有pre-commit和pre-rebase。
我宁愿在预先提交的钩子中运行testing。 因为更改在提交时已经logging。 仅推送和交换有关已logging已更改的信息。 如果一个testing失败,你的版本库里已经有了一个“破损”的版本。 无论你是否推动。
Git在1.8.2
版本中获得了pre-push
钩。
示例pre-push
脚本: https : //github.com/git/git/blob/87c86dd14abe8db7d00b0df5661ef8cf147a72a3/templates/hooks–pre-push.sample
1.8.2发行说明讨论新的预推钩: https : //github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt
Git在1.8.2版本中获得了预推钩。
预推钩子是我需要的预钩子。 除了保护分支之外,他们还可以提供额外的安全性和预先提交的挂钩。
而关于如何使用(从这个很好的条目采取和采用和增强)的例子,
简单的例子login到stream浪汉,运行testing,然后推
#!/bin/bash # Run the following command in the root of your project to install this pre-push hook: # cp git-hooks/pre-push .git/hooks/pre-push; chmod 700 .git/hooks/pre-push CMD="ssh vagrant@192.168.33.10 -i ~/.vagrant.d/insecure_private_key 'cd /vagrant/tests; /vagrant/vendor/bin/phpunit'" protected_branch='master' # Check if we actually have commits to push commits=`git log @{u}..` if [ -z "$commits" ]; then exit 0 fi current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') if [[ $current_branch = $protected_branch ]]; then eval $CMD RESULT=$? if [ $RESULT -ne 0 ]; then echo "failed $CMD" exit 1 fi fi exit 0
正如你可以看到的例子使用受保护的分支,预推钩的主题。
如果你使用的是命令行,最简单的方法是编写一个运行你的unit testing的脚本,如果它们成功的话,就完成这个脚本。
编辑
从git 1.8.2开始,这个答案已经过时了。 上面看manojlds的答案。
没有挂钩,因为推送不是修改您的存储库的操作。
你可以在接收端做接收端的检查。 那就是你通常会拒绝接收推送的地方。 运行unit testing可能需要一点时间才能完成,但这取决于你。
为了logging, Git 1.6有一个补丁,增加了一个预推钩 。 我不知道它是否对1.7有效。
而不是混淆,你可以运行推荐脚本像@ kubi推荐。 你也可以把它作为一个Rake任务,所以它在你的回购。 ruby-git可以帮助这个。 如果您检查目标回购,则只有在推送到生产回购时才能运行testing。
最后,你可以在你的pre-commit
钩子中运行你的testing,但是检查一下分支正在提交什么。 那么你可以有一个production
分支,它要求在接受一个提交之前通过所有的testing,但是你的master
并不关心。 limerick_rake在这种情况下可能会有用。
通过高选答案链接的脚本显示了pre-push
钩子 ( $1
是远程名称, $2
URL)以及如何访问提交(从stdin read
行具有结构<local ref> <local sha1> <remote ref> <remote sha1>
)
#!/bin/sh # An example hook script to verify what is about to be pushed. Called by "git # push" after it has checked the remote status, but before anything has been # pushed. If this script exits with a non-zero status nothing will be pushed. # # This hook is called with the following parameters: # # $1 -- Name of the remote to which the push is being done # $2 -- URL to which the push is being done # # If pushing without using a named remote those arguments will be equal. # # Information about the commits which are being pushed is supplied as lines to # the standard input in the form: # # <local ref> <local sha1> <remote ref> <remote sha1> # # This sample shows how to prevent push of commits where the log message starts # with "WIP" (work in progress). remote="$1" url="$2" z40=0000000000000000000000000000000000000000 while read local_ref local_sha remote_ref remote_sha do if [ "$local_sha" = $z40 ] then # Handle delete : else if [ "$remote_sha" = $z40 ] then # New branch, examine all commits range="$local_sha" else # Update to existing branch, examine new commits range="$remote_sha..$local_sha" fi # Check for WIP commit commit=`git rev-list -n 1 --grep '^WIP' "$range"` if [ -n "$commit" ] then echo >&2 "Found WIP commit in $local_ref, not pushing" exit 1 fi fi done exit 0