2016-03-31 20 views
1

形勢當創建工作目錄是不乾淨失敗了`git的預先收到hook`

我有一個Git倉庫的我的本地系統上的副本,以及一個遙控器上的第二個服務器(都是非裸露的)。由於環境限制,我不得不在兩者上都使用Git 1.8。

在服務器上,我有[receive] denyCurrentBranch = ignore.git/configgit reset --hard.git/hooks/post-receive。這允許我模擬denyCurrentBranch = updateInstead行爲。

我試圖添加一個pre-receive掛鉤,當我沒有清潔頭時會失敗。

嘗試

在服務器上,如果我跑git status我有以下幾點:

[server test_app]$ git status 
# On branch master 
nothing to commit, working directory clean 

我曾計劃在鉤添加git status,像這樣(僞的bash):

((git status | grep "On branch master" | wc -l) == 1) && ((git status | grep "nothing to commit, working directory clean" | wc -l) == 1) && (exit 0) 
exit 1 

要測試這是否可能,我嘗試了以下在我的鉤

git status 
exit 1 

結果

當我嘗試推,我得到如下:

Pushing to ssh://.../test_app 
remote: # On branch master[K 
remote: # Changes not staged for commit:[K 
remote: # (use "git add/rm <file>..." to update what will be committed)[K 
remote: # (use "git checkout -- <file>..." to discard changes in working directory)[K 
remote: #[K 
remote: # deleted: bin/README[K 
remote: #[K 
remote: # Untracked files:[K 
remote: # (use "git add <file>..." to include in what will be committed)[K 
remote: #[K 
remote: # COMMIT_EDITMSG[K 
remote: # HEAD[K 
remote: # ORIG_HEAD[K 
remote: # config[K 
remote: # description[K 
remote: # hooks/[K 
remote: # index[K 
remote: # info/[K 
remote: # logs/[K 
remote: # objects/[K 
remote: # refs/[K 
remote: no changes added to commit (use "git add" and/or "git commit -a")[K 
remote: I will fail[K 
To ssh://.../test_app 
! [remote rejected] master -> master (pre-receive hook declined) 
error: failed to push some refs to 'ssh://.../test_app' 

結論

我想這意味着我誤解了什麼狀態混帳處於預接收鉤。在我的預接收(或其他)掛鉤中,當工作目錄不乾淨時應該如何防止更新?

回答

0

感謝http://longair.net/blog/2011/04/09/missing-git-hooks-documentation/reset hard on git push,我發現pre-receive將工作目錄和GIT_DIR環境變量設置爲.git /文件夾。

因此,以下是足夠

git --git-dir=. --work-tree=$PWD/.. status 

興趣的人的整個鉤:

#!/bin/bash 

data=`git --git-dir=. --work-tree=$PWD/.. status` 
val=`echo $data | grep "working directory clean" | wc -l` 

if [ $val -eq 1 ] 
then 
    echo "All good" 
    exit 0 
else 
    echo "I will fail" 
    echo $data 
    exit 1 
fi