git commit in pre-push hook

核能气质少年 提交于 2019-12-19 05:07:36

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21334493/git-commit-in-pre-push-hook

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!