本文介绍了git pre-receive hook - 获取最新的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为git编写一个预接收钩子,它将拉取最新版本的代码,并针对它运行单元测试。我的代码在下面,但是当它到达git checkout $ newrev时,我得到:

I am trying to write a pre-receive hook for git that will pull the latest version of the code being pushed and run unit tests against it. My code is below, but when it gets to "git checkout $newrev", I get:

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

What do I need to do to get a checkout of the code being pushed before the receive has happened?

#!/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

推荐答案

尽管其他人认为提交已经收到,但还没有写入。

Despite what other people suggested the commit is already received, but it's not written yet.

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

I would even go as far as saying, that pre-receive hooks are better for deployment that post receive hooks. That why Heroku uses pre-receive hooks to deploy. If you're deployment doesn't go through, you can just reject the commit.

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

Here's some code that should do the trick for you:

#!/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

这篇关于git pre-receive hook - 获取最新的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:05