bash git:接收后挂钩中的空参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3762084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:39:50  来源:igfitidea点击:

git: empty arguments in post-receive hook

gitbashgithooks

提问by takeshin

I'm writing post-receive hook basing on the post-receive-emailscript from the contribdir, but it seems that the oldrevand newrevarguments are empty.

我正在根据目录中的post-receive-email脚本编写接收后挂钩contrib,但似乎oldrevnewrev参数是空的。

The script looks like this:

脚本如下所示:

#!/bin/bash

oldrev=$(git rev-parse )
newrev=$(git rev-parse )

The script runs on push, but all $1, $2, $oldrevand $newrevare empty. Should I configure something to get it running?

该脚本在运行推,但所有$1$2$oldrev$newrev是空的。我应该配置一些东西来让它运行吗?

(The repository was created by gitoliteif it does matter)

(存储库是由gitolite是否重要创建的)

采纳答案by J?rg W Mittag

The post-receivehook doesn't take any arguments. Quoth the manual(emphasis added):

post-receive钩不带任何参数。引用手册(强调):

This hook is invoked by git-receive-pack on the remote repository, which happens when a git push is done on a local repository. It executes on the remote repository once after all the refs have been updated.

This hook executes once for the receive operation. It takes no arguments, but gets the same information as the pre-receivehook does on its standard input.

This hook does not affect the outcome of git-receive-pack, as it is called after the real work is done.

This supersedes the post-updatehook in that it gets both old and new values of all the refs in addition to their names.

Both standard output and standard error output are forwarded to git send-packon the other end, so you can simply echo messages for the user.

The default post-receivehook is empty, but there is a sample script post-receive-emailprovided in the contrib/hooksdirectory in git distribution, which implements sending commit emails.

这个钩子由远程存储库上的 git-receive-pack 调用,当在本地存储库上完成 git push 时会发生这种情况。在更新所有引用后,它会在远程存储库上执行一次。

这个钩子为接收操作执行一次。它不接受参数,但获得与pre-receive钩子在其标准输入上所做的相同的信息。

这个钩子不会影响 的结果git-receive-pack,因为它是在真正的工作完成后调用的。

这取代了post-update钩子,因为它除了名称之外还获得所有引用的旧值和新值。

标准输出和标准错误输出都转发到git send-pack另一端,因此您可以简单地为用户回显消息。

默认post-receive钩子是空的,但是在git分发post-receive-emailcontrib/hooks目录下有一个示例脚本,它实现了发送提交邮件。

回答by Fran?ois

I stumbled into this problem while setting up a continuous integration server. Since the arguments are not passed to post-receive via the command line, you have to use the read command. Here is how I did it:

我在设置持续集成服务器时偶然发现了这个问题。由于参数没有通过命令行传递给 post-receive,你必须使用 read 命令。这是我如何做到的:

#!/bin/sh
read oldrev newrev refname
BRANCH=${refname#refs/heads/} 
curl --request POST "http://my.ci.server/hooks/build/myproject_$BRANCH"

回答by estani

There are no arguments though the information is passed over STDIN. To read that information from bash simply do this:

尽管信息通过 STDIN 传递,但没有参数。要从 bash 读取该信息,只需执行以下操作:

read oldrev newrev refname
echo "Old revision: $oldrev"
echo "New revision: $newrev"
echo "Reference name: $refname"

I'm just summarizing the answers already posted.

我只是总结已经发布的答案。

回答by aanno

A more elaborated version of Fran?ois script would be

弗朗索瓦脚本的更详细版本是

#!/bin/bash

JENKINS_URL="http://192.168.1.116/jenkins"
GIT_URL="[email protected]:nuclos/nuclos.git"

# remove all spaces and newlines from ARG
trim() {
  local ARG=""
  shift
  echo -e "$ARG" | tr -d "[:space:]\n" 
}

# unique sort ARG items separated by newlines
unique() {
  local ARG=""
  shift
  echo -e "$ARG" | sort -u -i
}

# cut first and last character from ARG
cutfl() {
  local ARG=""
  shift
  local LEN="${#ARG}"
  let LEN="$LEN - 2"
  echo "${ARG:1:$LEN}"
}
BRANCHES=""
while read oldrev newrev refname; do
  BRANCH=`trim ${refname#refs/heads/}`
  if [ -n "$BRANCH" ]; then
    BRANCHES+="${BRANCH}\n"
  fi
done

BRANCHES=`unique "$BRANCHES" | tr '\n' ','`
BRANCHES=`cutfl "$BRANCHES"`
echo wget -q -O - "$JENKINS_URL/git/notifyCommit?url=$GIT_URL&branches=$BRANCHES"
at "now + 5 minutes" <<END
wget -q -O - "$JENKINS_URL/git/notifyCommit?url=$GIT_URL&branches=$BRANCHES"
END

This version could cope with more than one branch and only triggers one build for each one.

这个版本可以处理多个分支,并且每个分支只触发一个构建。

回答by Dennis

Actually, I don't accept the "it takes no arguments", because the sample script post-receive.sample is having the following comment:

实际上,我不接受“它不需要任何参数”,因为示例脚本 post-receive.sample 有以下注释:

# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated.  It is passed arguments in through
# stdin in the form
#  <oldrev> <newrev> <refname>
# For example:
#  aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master