git post-receive 钩子没有得到承诺的参数

git post-receive 钩子没有得到承诺的参数

从接收后文件中:

# This 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
#

但当我用 测试时echo "$1 $2 $3",只得到一行空白。有人知道为什么吗?

答案1

这是一个简单的例子,证实了 koumes21s 的回答。我使用以下代码制作了一个 Python 脚本:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys

print "ARGS:", sys.argv

a = sys.stdin.read()
(old, new, ref) = a.split()
print "Old: %s" % old
print "New: %s" % new
print "Ref: %s" % ref

这是推送后的输出。请注意,“ARGS”仅报告脚本的名称,而不报告任何标准输入。

inneralienmbp$ git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 299 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
remote: ARGS: ['hooks/post-receive']
remote: Old: 5c9f9a43132516040200ae76cc2f4f2cad57d724
remote: New: 95e0e2873eaad2a9befa2dff7e2ce9ffdf3af843
remote: Ref: refs/heads/master
To /Users/tweaver/test2/test.git/
   5c9f9a4..95e0e28  master -> master

谢谢 koumes21!

答案2

这是因为参数是通过 stdin 传递的,而不是通过命令行参数传递的。这是因为可以有多个更改,然后这些更改会作为多行传递给您的脚本。因此,您可以使用 read 命令或从 /dev/stdin 获取输入。

答案3

这是解决此问题的有关堆栈的帖子。

https://stackoverflow.com/a/12367999/1354978

以下是您想要获得的内容的一个简单版本:

read oldrev newrev ref
echo "$oldrev"
echo "$newrev"
echo "$ref"

这是我用于 CI 服务器和电子邮件挂钩的版本

read oldrev newrev ref
echo "$oldrev" "$newrev" "$ref" | . /usr/share/git-core/contrib/hooks/post-receive-email


if [ "refs/heads/qa" == "$ref" ]; then
  # Big Tuna YO!
  wget -q -O - --connect-timeout=2 http://127.0.0.1:3000/hooks/build/qa_now
fi

相关内容