git pre-receive hook - 获取最新的代码

Mar*_*son 4 git githooks

我正在尝试编写一个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
Run Code Online (Sandbox Code Playgroud)

cod*_*joe 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
Run Code Online (Sandbox Code Playgroud)