2012-11-15 65 views
3

我有一個服務器端鉤子(更新後),將在每次代碼推送後運行。即使沒有新的更改推送,是否有任何方法(或者可能是另一個鉤子?)來運行它?當沒有更新時Git運行服務器端鉤子

例子:

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

我想它再次運行。那可能嗎?

+0

'post-receive'鉤子怎麼樣?另外,更新後的鉤子是否真的沒有運行,或者只是在空參數列表中沒有做任何事情?僅供參考'git help hooks'爲您提供了git鉤子的手冊頁。 – asm

+2

嘗試了所有* -update和* -receive,發生同樣的情況。如果沒有更改,那麼就不會運行鉤子(如果我使用鉤子來實現它們的目標,並且不會觸發構建過程,即使沒有更改,也是有意義的......) – Matt

+0

這似乎是一個奇怪的要求。如果您推送,並且沒有任何推送,服務器上沒有任何反應。爲什麼你會想觸發一些事情,因爲什麼都沒發生?你能解釋你想解決什麼問題嗎? – sleske

回答

1

創建一個pre-receive掛鉤,使其成爲exit 1,並從客戶端git push origin master HEAD:non-existing-branch。這將觸發pre-receive掛鉤,即使沒有更改爲master

爲了避免錯誤信息,使成功地pre-receive鉤出口(exit 0),以及手動從post-receive鉤卸下的non-existing-branch。但是,這會創建一個很小的時間窗口(當文件non-existing-branch存在時),而從另一個客戶端發起的git push ...將不會運行掛接。

+0

這項工作將如何進行?如果你有'出口1'的預接收鉤子,*所有*按鈕都會失敗。此外,這意味着客戶端必須執行特殊的push命令,而不僅僅是簡單的'git push'。如果客戶必須運行一個特殊的命令,爲什麼不直接觸發無操作推動應該觸發? – sleske

+1

@sleske:預接收鉤子在其stdin上接收名稱'non-existing-branch',所以'exit 1'可以作爲條件。請注意,我的答案也包含一個「退出0」。我認爲特殊的推式參數(包括'master')可以使用'git config'進行默認設置。在我的回答中,只需要使用最初的想法進行一些實驗。 – pts

0

非常感謝@pts的回答(今天用我所有的票,不能馬上upvote :));對於那些(像我)有一個小問題,瞭解它的功能完全相同,這裏是一個簡單的命令行日誌,展示瞭如何使用的non-existing-branch觸發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 [email protected] 

# 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每一次,只是爲了觸發腳本。

相關問題