简体   繁体   English

Git:使用'git push'命令将参数发送到'post-receive'钩子

[英]Git: send parameters to 'post-receive' hook with the 'git push' command

I have configured a post-receive hook script on a server for automatic deploy with Git, as described for example here . 我配置了一个post-receive了Git的自动部署服务器挂机脚本,例如描述这里

So, when on my local PC I send a push on the production server: 因此,当我在本地PC上发送生产服务器时:

git push production master

the post-receive hook is triggered, copying the content of the repository inside the live directory. 触发post-receive挂钩,复制实时目录中的存储库内容。

My question is if is there some way to pass some parameter to the post-receive hook when I send the push? 我的问题是,当我发送推送时,是否有某种方法将一些参数传递给post-receive挂钩?

Something like this: 像这样的东西:

git push production master --params="clear-the-cache=1"

then within the hook I can read the 'clear-the-cache=1' parameter and perform additional operations. 然后在钩子内我可以读取'clear-the-cache=1'参数并执行其他操作。

Is there a way to do it? 有办法吗?

Edit: Git version 2.10.0 added a new --push-option (aka -o ) argument to git push , which provides a nice clean way to do this. 编辑:Git版本2.10.0git push添加了一个新的--push-option (aka -o )参数,它提供了一个很好的干净方法。 Thanks to Steve Coffman for reminding me. 感谢Steve Coffman提醒我。 Both server and client need to be at 2.10.0 or higher; 服务器和客户端都需要为2.10.0或更高; the supplied options, one per -o or --push-option argument, are then passed to both the pre-receive and post-receive hooks as arguments. 然后将提供的选项(每个-o--push-option参数一个)作为参数传递给pre-receive和post-receive挂钩。


Not cleanly. 不干净。 There are some methods by which you could provide a side channel, but I think they are all quite ugly. 有一些方法可以提供侧面通道,但我认为它们都非常难看。

Consider that every git push pushes one or more reference labels. 考虑每个git push推送一个或多个引用标签。 If you push at least two labels every time, one of the labels can point to objects (commits, tags, etc) that carry additional information. 如果每次至少推送两个标签,其中一个标签可以指向携带附加信息的对象(提交,标签等)。 The form of that additional information is up to you. 该附加信息的形式由您决定。

Git generally will not allow you to push arbitrary ref-space names like refs/sidechannel : Git通常不允许你推送像refs/sidechannel这样的任意引用空间名称:

remote: error: refusing to create funny ref 'refs/sidechannel' remotely
 ! [remote rejected] refs/sidechannel -> refs/sidechannel (funny refname)

(though I accidentally discovered that github thinks this means to create a branch named refs/sidechannel . Odd.) Anyway, this means that in general, you would have to use a branch or tag name. (虽然我偶然发现github认为这意味着创建一个名为refs/sidechannel分支 。奇怪。)无论如何,这意味着一般来说,你必须使用分支或标签名称。 For instance: 例如:

git push production master sidechannel

In your post-receive script, you could then check for updates to refs/heads/sidechannel and do whatever you like with them (including extract information, then delete the branch). 在你的接收后脚本中,你可以检查refs/heads/sidechannel更新,并用它们做任何你喜欢的事情(包括提取信息,然后删除分支)。

Alternatively, you could use ordinary annotated tags or specially formatted text in commit messages to provide side-channel data. 或者,您可以在提交消息中使用普通的带注释标签或特殊格式的文本来提供旁道数据。 Or, since refs/notes is now a recognized name space, use refs/notes/ name as the name of the side channel. 或者,由于refs/notes现在是可识别的名称空间,因此请使用refs/notes/ name作为辅助通道的名称。

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

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