2012-07-16 106 views
25

由于“测试”是Git钩子的常见用法,我的问题很难搜索。如何测试git钩子

我在写一个相当复杂的git post-receive hook,想知道测试它的最好方法是什么。目前,我的过程是:

  • 更改在一个虚拟的“远程”回购对接收后
  • 进行了更改虚拟本地回购
  • 承诺在虚拟本地回购
  • 推改改dummy remote repo

有没有更简单的方法来测试它?理想的情况是它看起来像:

  • 做出改变(收费)虚拟回购后,收到
  • 问题的“神奇”命令来测试后收到

或许我可以“补发”先前的推送还是让远程回购行为好像刚刚接收到具有特定散列的推送?

回答

13

编写一个只记录其参数/环境并将其转储到文件的钩子。然后,您可以在相同的环境/参数下重新调用真正的钩子,它会像您刚刚重新发出完全相同的推动一样。

+0

这是关于我做了什么,而是通过手动检查(回显)相关变量。 – Errol 2012-07-17 00:25:50

+9

这样的“翻车机”钩子会是什么样子? – 2015-11-11 22:28:26

+4

@JonWatson,我刚刚使用'echo $ @; echo; PWD;回声; set' – Lucas 2016-01-28 17:41:01

6

回答这个四岁的问题。

如果您想在本地环境中测试钩,我给了命令的详细信息进行跟踪,使用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,该更新仅推到当地回购/tmp/hook_test/git.git