4

我正在尝试编写一个pre-receive hookfor git,它将提取最新版本的正在推送的代码并针对它运行单元测试。我的代码在下面,但是当它到达“git checkout $newrev”时,我得到:

远程:致命:参考不是树:188de39ca68e238bcd7ee9842a79397f39a5849e

在接收发生之前,我需要做什么才能结帐正在推送的代码?

#!/bin/bash
while read oldrev newrev refname
do
  echo "Preparing to run unit tests for $newrev"
  TEST_DIR=/opt/git/sommersault-push-tests/sommersault

  # check out this version of the code
  unset GIT_DIR
  echo $refname
  cd $TEST_DIR
  git checkout $newrev

  ...do more stuff...
done
4

2 回答 2

5

尽管其他人建议已收到提交,但尚未编写。

我什至会说,预接收挂钩更适合部署后接收挂钩。这就是 Heroku 使用预接收钩子进行部署的原因。如果你的部署没有通过,你可以拒绝提交。

这是一些应该为您解决问题的代码:

#!/bin/bash
while read oldrev newrev refname
do
    echo "Preparing to run unit test for $newrev ... "
    git archive $newrev | tar -x -C /tmp/newrev
    cd /tmp/newrev

    echo "Running unit test for $newrev ... "
    # execute your test suite here

    rc=$?

    cd $GIT_DIR
    rm -rf /tmp/newrev
    if [[ $rc != 0 ]] ; then
        echo "Push rejected: Unit test failed on revision $newrev."
        exit $rc
    fi
done

exit 0
于 2014-03-04T23:16:56.757 回答
0

我正在使用这个基于这个howto的脚本。仅在选定的分支中执行 phpunit

#!/bin/sh

while read oldrev newrev refname
do
    # Only run this script for the dev branch. You can remove this
    # if block if you wish to run it for others as well.
    if [ $refname = "refs/heads/dev" ] ; then
        # Anything echo'd will show up in the console for the person
        # who's doing a push
        echo "Preparing to run phpunit for $newrev ... "

        # Since the repo is bare, we need to put the actual files someplace,
        # so we use the temp dir we chose earlier
        mkdir -p /tmp/$refname
        git archive $newrev | tar -x -C /tmp/$refname

        echo "Running phpunit for $newrev ... "

        # This part is the actual code which is used to run our tests
        # In my case, the phpunit testsuite resides in the tests directory, so go there
        cd /tmp/$refname
        composer install > /dev/null

        # And execute the testsuite, phpunit will send a response error if tests fail
        phpunit --group no-db

        # Clean temp dir
        rm -rf /tmp/$refname
    fi
done

# Everything went OK so we can exit with a zero
exit 0

随意定制...

于 2014-11-01T10:51:48.687 回答