如何测试 git 钩子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11512155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test git hooks
提问by Errol
Since "testing" is a common use for a Git hook, my question is hard to search for.
由于“测试”是 Git 钩子的常见用途,因此很难搜索我的问题。
I'm writing a fairly involved git post-receive hook and want to know what the best way to test it is. Currently my process is:
我正在编写一个相当复杂的 git post-receive hook,想知道测试它的最佳方法是什么。目前我的流程是:
- make changes to post-receive in a dummy "remote" repo
- make a change to a dummy local repo
- commit change in dummy local repo
- push change to dummy remote repo
- 在虚拟的“远程”存储库中对 post-receive 进行更改
- 对虚拟本地仓库进行更改
- 在虚拟本地仓库中提交更改
- 将更改推送到虚拟远程仓库
Is there any easier way to test this? Ideally it would look like:
有没有更简单的方法来测试这个?理想情况下,它看起来像:
- make change(s) to post-receive in a dummy repo
- issue "magic" command to test post-receive
- 在虚拟仓库中对接收后进行更改
- 发出“magic”命令来测试接收后
Perhaps I can "reissue" a previous push or have the remote repo act as though it just received a push with a specific hash?
也许我可以“重新发布”以前的推送,或者让远程仓库表现得好像它刚刚收到了具有特定哈希值的推送?
采纳答案by Lily Ballard
Write a hook that just records its arguments/environment and dumps that to a file. Then you can just re-invoke the real hook at your leisure with the same environment/arguments and it will act as though you just re-issued the exact same push.
编写一个仅记录其参数/环境并将其转储到文件的钩子。然后,您可以在闲暇时使用相同的环境/参数重新调用真正的钩子,它的行为就像您刚刚重新发出完全相同的推送一样。
回答by BMW
Answer this four-years-old question.
回答这个四年前的问题。
If you'd like to test hook in local environment, I give the detail commands for following up, use post-receive
as sample:
如果你想在本地环境中测试钩子,我给出了后续的详细命令,post-receive
用作示例:
$ mkdir /tmp/hook_test
$ cd /tmp/hook_test
# set local git repo, where you put hooks in it.
$ git clone --bare https://github.com/git/git.git
# set develop environment which is cloned from the new created repo.
$ git clone git.git repo
# copy and rename the hook you need test to "post-receive"
$ cd git.git/hooks
$ cp ~/post-receive-test post-receive
# suppose the hook script is bash script.
# edit "post-receive" and add "set -x" to second line in it to active debug
$ cd /tmp/hook_test/repo
# emulate a hook trigger, do some changes, "git add" and "git commit" it
$ git push
# Now you should see the script "post-receive" runs automatically with debug details.
You should be free to run git push
, that the updates are only pushed to local repo /tmp/hook_test/git.git
您应该可以自由运行git push
,更新仅推送到本地存储库/tmp/hook_test/git.git
回答by dirksen
My approach is to dial the HEAD at the remote repo back one commit, and then push again:
我的方法是将远程仓库中的 HEAD 拨回一次提交,然后再次推送:
ssh <repo> 'cd /<repo_path>; git update-ref refs/heads/master HEAD^' && git push origin master
回答by fariwesen
For debugging you could also end your hook in something like this:
对于调试,您还可以以如下方式结束钩子:
echo "No errors found."
exit 1
If you are happy with you hook you of course take out the last line again.
如果您对钩子感到满意,当然可以再次取出最后一行。