简体   繁体   English

git“预推”钩子标准输入为空

[英]git "pre-push" hook stdin is empty

I'm trying to create a pre-push hook that will prevent the user from pushing to a branch other than the one they're on.我正在尝试创建一个预推送挂钩,以防止用户推送到他们所在的分支以外的分支。

Based on the the pre-push.sample (and various threads I've read) it looks like I should be able to read which local branch I am pushing and the remote one I'm pushing to via stdin with:根据 pre-push.sample (以及我读过的各种线程),看起来我应该能够读取我正在推送的本地分支以及我通过 stdin 推送到的远程分支:

while read local_ref local_sha remote_ref remote_sha
do
...
done

However when I do this, local_ref and the other variables are empty (even when running the unedited sample hook, it never enters the loop).但是,当我这样做时, local_ref和其他变量都是空的(即使在运行未编辑的示例挂钩时,它也永远不会进入循环)。

Is there some configuration or other step I need to do to access the variables at stdin?我需要做一些配置或其他步骤来访问标准输入中的变量吗?

I'm using git version 2.17.1我正在使用git version 2.17.1

从我自己遇到同样的问题开始,我相信你看到的和我一样,如果推送不包含任何更改,即当你运行 git push 时,你会看到“一切都是最新的”,那么来自 stdin 的输入行将是空白的,必须推动一些更改才能填充该行。

First, make sure your hook is called at all. 首先,确保您的钩子已被调用。
That means: 这意味着:

  • its name shoud be pre-push (no .sample ) 其名称应为pre-push (无.sample
  • it should be executable (unless you are on Windows, in which case it is executable by default) 它应该是可执行的(除非您在Windows上,否则默认情况下它是可执行的)

Please check if you run commands before the loop that already read stdin.请检查您是否在已读取标准输入的循环之前运行命令。 In my case, I had some commands from git lfs in my pre-push script.就我而言,我的预推送脚本中有一些来自 git lfs 的命令。 I had to move the loop before that command.我必须在该命令之前移动循环。

command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/pre-push'.\n"; exit 2; }
git lfs pre-push "$@"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM