从接收后的文件:
# 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"testing它时,我只能看到一个空行。 有谁知道为什么?
这是一个确认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”只报告脚本的名称而不是标准input。
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!
这是因为参数是通过stdin传递的,而不是通过命令行参数。 这是因为可以有多个更改,然后以多行传递给脚本。 所以你可以使用read命令或者从/ dev / stdin获取input。
这是一个解决这个问题的栈上的post。
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