git - 为什么我无法在预推中读取标准输入?
问题描述
我尝试使用示例 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 个提交。我做错了什么?
解决方案
您的脚本中有一个错误(或多个错误)(有多少取决于您想要完成的操作)。
您正确使用了while read ...
循环:
while read local_ref local_sha remote_ref remote_sha
在循环内部进行各种测试;如果其中一些成功,则循环本身会提前退出(没关系)。但是,如果它们都不成功,则循环结束,读取有关整个推送的信息,然后读取将所有四个变量设置为空字符串的文件结束指示。
如果您想在每次循环中观察它们的值,您必须在循环内执行此操作。
如果要收集数据,则必须在一些附加变量中收集数据,并在循环外使用该变量(或那些变量)。
推荐阅读
- node.js - 如何获得密钥斗篷令牌?
- javascript - 在 cypress 的测试中访问网站提供的 JavaScript 变量
- arrays - Swift:根据在前端另一个创建的数组中保存的 ID 对从后端检索到的对象数组进行排序
- php - PDO 无法在 SELECT 查询中为 NULL 绑定值
- winforms - 由于分辨率差异,我如何通过 Ranorex 单击错误的项目?
- r - 将列中的分类值存储在向量 R 中
- python - 如何在 HTML 中发布查询集的第一个值?
- object - Dataweave - 在 mappedObject 中动态拆分名称字段
- java - 如何让 MacOS (Catalina) 使用自定义 Java 版本?
- python - 如何在字典的循环中获取句柄和标签?