git在pre-push hook中提交 [英] git commit in pre-push hook

查看:360
本文介绍了git在pre-push hook中提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 pre-push 钩子中添加了类似的内容:

  gs0 = $(git status)
pip-dump
gs1 = $(git status)
if [gs0!=gs1]
then
git commit -mpip-dump
fi

(这是更新我的点子需求文件)



看起来推送并不是推动新的提交,而是在脚本开始时的头部提交。 b
$ b

如何解决这个问题?

解决方案

c> push 命令指出在调用钩子之前承诺推送的内容,并在钩子退出0时推送它。



我看到三个选项:


  1. 退出非零值,告诉用户因为我添加了一个提交而被拒绝

  2. 退出零,告诉用户推送过去了,但你需要再次推送,因为我添加了一个提交

  3. 在插入后执行另一个(不同的) g新的提交,注意你的钩子不会无休止地递归,因为内部推送运行挂钩,它决定做另一个再次内部推送等等。然后,退出非零,中止外部推送宣布您必须执行内部推送以获得额外的提交。

我个人偏好将是这些。一个预先推送的钩子是指作为验证这个推送是否正常的操作,而不是改变这个推送意味着其他不同的推送操作。这意味着你不会违背软件的意图。使用预推钩作为验证者;如果你想要一个在自动添加 pip-dump commit后调用 git push 的脚本,一个使用不同名称的脚本,如 dump-and-push


I have added something like that in pre-push hook:

gs0=$(git status)
pip-dump
gs1=$(git status)
if [ "gs0" != "gs1" ]
then
    git commit -m "pip-dump"
fi

(this is updating my pip requirements file)

It seems that the push is not pushing the new commit, but the one which the HEAD was on at the beginning of the script.

How to fix that?

解决方案

You can't: the push command figures out which commits to push before invoking the hook, and pushes that if the hook exits 0.

I see three options:

  1. Exit nonzero, telling the user "push rejected because I added a commit"
  2. Exit zero, telling the user "push went through but you'll need to push again because I added a commit"
  3. Do another (different) push inside the hook, after adding the new commit, taking care that your hook does not endlessly recurse because the "inner" push runs the hook which decides to do another "inner-again" push, etc. Then, exit nonzero, aborting the "outer" push, after announcing that you had to do an "inner" push to get the extra commit sent through.

My personal preference would be the first of these. A pre-push hook is meant as a "verify that this push is OK" operation, not a "change this push to mean some other different push" operation. So that means you're not working against the "intent" of the software. Use the pre-push hook as a verifier; and if you want a script that invokes git push after automatically adding a pip-dump commit if needed, write that as a script, using a different name, such as dump-and-push.

这篇关于git在pre-push hook中提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆