【问题标题】:git pre-receive hook - getting the newest codegit pre-receive hook - 获取最新代码
【发布时间】:2012-08-16 01:39:54
【问题描述】:

我正在尝试为 git 编写一个pre-receive hook,它将拉取被推送的代码的最新版本并针对它运行单元测试。我的代码在下面,但是当它到达“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

【问题讨论】:

  • 你确定你能做到吗?它如何检查它没有收到的代码?我认为你需要做一个接收后挂钩,然后在测试失败时让它回滚(重置)。
  • 我认为@wadesworld 给了你正确的建议。我可以将此链接作为类似问题的解决方案:stackoverflow.com/questions/2087216/…
  • 啊,好吧。我开始看这篇文章 - codeutopia.net/blog/2011/06/30/… - 这似乎暗示有可能获得代码。 (它使用“git archive”而不是“git checkout”,虽然我都无法工作。)但它还不能访问代码是有道理的。
  • 实际上,我收回了这一点。 githooks 手册页显示This hook is invoked by git-receive-pack on the remote repository,这表明它发生在收到新内容之后。但是请注意,它会为每个要更新的引用接收一行输入。虽然还不确定错误的原因..

标签: git githooks


【解决方案1】:

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

我什至会说,预接收挂钩更适合部署后接收挂钩。这就是 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

【讨论】:

    【解决方案2】:

    我正在使用这个基于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
    

    随意定制...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-17
      • 2017-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      相关资源
      最近更新 更多