git 预推送:在 运行 测试时远程主机关闭了连接

git pre-push: connection closed by remote host while running tests

我的 git 存储库中有一个运行测试的预推送脚本。如果测试通过,推送就会继续。如果测试失败,它会中止推送。

该脚本在一段时间内运行良好,直到测试开始超过 3 分钟。 stdout 在测试输出中间显示一个 "Connection to bitbucket closed by remote host"。然后所有的测试都通过了,推送并没有真正通过。

这是预推脚本

#!/bin/sh
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

# This script runs tests before any push to the MASTER branch and fails
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),,')
echo "Current branch: "$current_branch
if [ $current_branch = "master" ]
then
    echo "Pushing to MASTER branch requires tests to pass..."
    ./run.sh test
    if [ $? = 0 ]
    then
        exit 0
    else
        echo "***ERROR> Failed to pass tests! Get tests to pass and then try again..."
        exit 1
    fi
else
    echo "Skipping tests since we're not pushing to MASTER..."
fi

你检查过bitbucket.properties了吗?也许您遇到了一些超时,例如:process.timeout.executionplugin.bitbucket-scm-git.backend.timeout.idle。可能需要快速检查一下是否有一些超时设置为 180 秒。 Here 您可以找到有关可用属性的更多详细信息。

我最终在成功案例中调用了 git push --no-verify。所以它有效地推动了两次。

#!/bin/sh
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

# This script runs tests before any push to the MASTER branch and fails
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),,')
echo "Current branch: "$current_branch
if [ $current_branch = "master" ]
then
    echo "Pushing to MASTER branch requires tests to pass..."
    ./run.sh test
    if [ $? = 0 ]
    then
        # workaround to guarantee my push goes through even if the first attempt times out
        git push --no-verify
        exit 0
    else
        echo "***ERROR> Failed to pass tests! Get tests to pass and then try again..."
        exit 1
    fi
else
    echo "Skipping tests since we're not pushing to MASTER..."
fi

The answer should explain why git is trying to reach Gitlab or Bitbucket or whatever (in my case it's Gitlab) even though the pre-push script is not finished

pre-push 钩子在 commit ec55559, Jan. 2013, Git v1.8.2-rc0

中引入

它是 as/pre-push-hook 补丁的一部分:

参见 commit 87c86dd, commit ec55559, commit 5a7da2d (13 Jan 2013) by Aaron Schrab (aschrab)
(由 Junio C Hamano -- gitster -- in commit bb9a696 合并,2013 年 1 月 24 日)

唯一的其他修改是 commit af65f68 (16 Nov 2015) by Clemens Buchacher (drizzd) 忽略 SIGPIPE,意思是忽略它 标准输入流。 (由 Jeff King -- peff -- in commit 40fdcc5 合并,2015 年 12 月 1 日)

documentation does include:

Information about what is to be pushed is provided on the hook's standard input with lines of the form:

<local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF

For instance, if the command +git push origin master:foreign+ were run the hook would receive a line like the following:

refs/heads/master 67890 refs/heads/foreign 12345

although the full, 40-character SHA1s would be supplied.

  • If the foreign ref does not yet exist the <remote SHA1> will be 40 0.
  • If a ref is to be deleted, the <local ref> will be supplied as (delete) and the <local SHA1> will be 40 0.

为了确定远程 SHA1 的正确值,transport.c 必须与远程存储库(在您的情况下为 GitLab)进行交换