3

我有一个服务器端挂钩(更新后),它将在我每次推送代码后运行。即使没有要推送的新更改,是否有任何方法(或者可能是另一个钩子?)来运行它?

例子:

$ git push origin master
remote: Hook running....
$
$ git push origin master
Everything up-to-date
$

我希望它再次运行。那可能吗?

4

2 回答 2

1

创建一个pre-receive钩子,制作它exit 1,并从客户端git push origin master HEAD:non-existing-branch。这将触发pre-receive钩子,即使master.

为避免错误消息,请让pre-receive钩子成功退出 ( exit 0),然后手动non-existing-branchpost-receive钩子中删除 。但是,这会创建一个小的时间窗口(当文件non-existing-branch存在时),git push ...从另一个客户端启动不会运行挂钩。

于 2013-04-13T09:55:31.860 回答
0

非常感谢@pts的回答(今天使用了我所有的选票,不能立即投票:));对于那些(像我一样)在理解它的确切功能方面有一点问题的人,这里有一个简单的命令行日志,演示了non-existing-branchto trigger的用法pre-receive

# create original repo:

$ cd /tmp
$ mkdir repotest_git
$ cd repotest_git/
$ git init
Initialized empty Git repository in /tmp/repotest_git/.git/
$ git config user.name "Your Name"
$ git config user.email you@example.com

# populate repo with 1 commit:

$ echo "test" > test.txt
$ git add test.txt
$ git commit -m "initial commit"
[master (root-commit) 2b60608] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
$ 

# create remote "bare" repo; and add pre-receive hook there:

$ cd /tmp
$ git clone --bare repotest_git repotest-remote.git
Cloning into bare repository 'repotest-remote.git'...
done.
$ cd repotest-remote.git
$ 
$ cat > hooks/pre-receive <<"EOF"
#!/bin/sh
echo from pre-receive: HELLO_VAR [$HELLO_VAR]
exit 1
EOF
$ chmod +x hooks/pre-receive

# go back to original repo, add a remote reference
# to the newly created remote "bare" repo; update with pull:

$ cd /tmp
$ cd repotest_git
$ git remote add origin file:///tmp/repotest-remote.git
$ git pull origin master
From file:///tmp/repotest-remote
 * branch            master     -> FETCH_HEAD
Already up-to-date.

# try testing the hook script;
# since we don't have any changes, Everything is 
# up-to-date, and the pre-receive won't trigger:

$ git push
Everything up-to-date

# try testing with HEAD:non-existing-branch
# pre-receive gets triggered - and 
# we can see variable is not there:

$ git push origin master HEAD:non-existing-branch
Total 0 (delta 0), reused 0 (delta 0)
remote: from pre-receive: HELLO_VAR []
To file:///tmp/repotest-remote.git
 ! [remote rejected] HEAD -> non-existing-branch (pre-receive hook declined)
error: failed to push some refs to 'file:///tmp/repotest-remote.git'

# try testing again, this time specify 
# env. variable on command line....
# pre-receive gets triggered - and 
# we can see variable is there:

$ HELLO_VAR=hello git push origin master HEAD:non-existing-branch
Total 0 (delta 0), reused 0 (delta 0)
remote: from pre-receive: HELLO_VAR [hello]
To file:///tmp/repotest-remote.git
 ! [remote rejected] HEAD -> non-existing-branch (pre-receive hook declined)
error: failed to push some refs to 'file:///tmp/repotest-remote.git'

在这里,对于本地工作,一切都按预期使用变量;但是,显然,如果您的远程存储库位于服务器等后面,那么查看变量的结束方式/位置可能会出现问题 - 因此仅调试该方面非常有用,而不必每次都对文件进行更改 + git add/ commit/ push,只是为了触发脚本。

于 2013-04-22T20:49:58.813 回答