Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何测试git钩子_Git_Git Post Receive - Fatal编程技术网

如何测试git钩子

如何测试git钩子,git,git-post-receive,Git,Git Post Receive,由于“测试”是Git钩子的常见用法,所以我的问题很难找到 我正在编写一个相当复杂的git post receive钩子,想知道测试它的最佳方法是什么。目前我的流程是: 在虚拟“远程”回购中更改post接收 更改虚拟本地回购协议 在虚拟本地repo中提交更改 将更改推送到虚拟远程回购 有没有更简单的方法来测试这一点?理想的情况是: 更改以在虚拟回购中过账接收 发出“magic”命令以测试post接收 也许我可以“重新发布”以前的推送,或者让远程repo像刚收到带有特定哈希的推送一样工作?编

由于“测试”是Git钩子的常见用法,所以我的问题很难找到

我正在编写一个相当复杂的git post receive钩子,想知道测试它的最佳方法是什么。目前我的流程是:

  • 在虚拟“远程”回购中更改post接收
  • 更改虚拟本地回购协议
  • 在虚拟本地repo中提交更改
  • 将更改推送到虚拟远程回购
有没有更简单的方法来测试这一点?理想的情况是:

  • 更改以在虚拟回购中过账接收
  • 发出“magic”命令以测试post接收

也许我可以“重新发布”以前的推送,或者让远程repo像刚收到带有特定哈希的推送一样工作?

编写一个钩子,只记录其参数/环境并将其转储到文件中。然后,你可以在空闲时用相同的环境/参数重新调用真正的钩子,它的行为就好像你刚刚重新发布了完全相同的推送。

回答这个四年前的问题

如果您想测试钩子,首先需要在本地环境中进行测试,我给出了详细的后续命令,使用
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.

您应该可以自由运行git push,更新只会被推送到本地repo
/tmp/hook_test/git.git
我的方法是在远程repo处拨回一个提交头,然后再次推送:

ssh <repo> 'cd /<repo_path>; git update-ref refs/heads/master HEAD^' && git push origin master
ssh'cd/;git更新参考文件/磁头/主磁头^’&git推送原点主磁头(&G)

为了进行调试,您还可以用如下方式结束钩子:

echo "No errors found."
exit 1

如果你对钩子很满意,你当然会再次取出最后一行。

这是我做的,但是我会手动检查(回显)相关变量。这样的“转储”钩子会是什么样子?@JonWatson,我刚刚使用了
echo$@;回声;pwd;回声;set
这对某些人来说可能很明显,但请确保您的钩子脚本是可执行的!我在文本编辑器中创建了我的,而不是复制.sample,它没有执行.Perfect,这正是我需要的。