首页 > 解决方案 > 为什么我无法在预推中读取标准输入?

问题描述

我尝试使用示例 pre-push 钩子,但它似乎不起作用:

#!/bin/bash
remote="$1"
url="$2"

z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha
do
        if [ "$local_sha" = $z40 ]
        then
                # Handle delete
                :
        else
                if [ "$remote_sha" = $z40 ]
                then
                        # New branch, examine all commits
                        range="$local_sha"
                else
                        # Update to existing branch, examine new commits
                        range="$remote_sha..$local_sha"
                fi

                # Check for WIP commit
                commit=`git rev-list -n 1 --grep '^WIP' "$range"`
                if [ -n "$commit" ]
                then
                        echo >&2 "Found WIP commit in $local_ref, not pushing"
                        exit 1
                fi
        fi
done

echo "local_ref=${local_ref} local_sha=${local_sha} remote_ref=${remote_ref} remote_sha=${remote_sha}"
echo "remote=$remote"
exit 1

git push输出是:

local_ref= local_sha= remote_ref= remote_sha=
remote=origin
error: failed to push some refs to...

所有的提交信息都是空的。但是在原点之前有 2 个提交。我做错了什么?

标签: git

解决方案


您的脚本中有一个错误(或多个错误)(有多少取决于您想要完成的操作)。

您正确使用了while read ...循环:

while read local_ref local_sha remote_ref remote_sha

在循环内部进行各种测试;如果其中一些成功,则循环本身会提前退出(没关系)。但是,如果它们都不成功,则循环结束,读取有关整个推送的信息,然后读取将所有四个变量设置为空字符串文件结束指示

如果您想在每次循环中观察它们的值,您必须在循环内执行此操作。

如果要收集数据,则必须在一些附加变量中收集数据,并在循环外使用该变量(或那些变量)。


推荐阅读