2012-02-01 23 views
1

我想爲某種不常見的用例設置git。如何防止檢查到不乾淨的遠程工作樹

我有一個存儲庫服務器('回購'),我有一個開發服務器('開發')我想部署到。在回購服務器上,我有一個裸回購('origin'),我從我的本地克隆推送了我的更改。

當我推了一個名爲「發展」到回購服務器分支,我有一個post-receive掛鉤那裏,推動分支到開發服務器:

#!/bin/bash 

# post-receive on repo server 

while read oldrev newrev ref 
    do 
    branch=`echo $ref | cut -d/ -f3` 

    if [ "development" == "$branch" ]; then 
    git push develop development 
    echo 'Changes pushed to development server.' 
    fi 
done 

exit 0; 

在開發服務器我有一個職位 - 接收鉤子讀取

#!/bin/bash 
GIT_WORK_TREE=/srv/www/htdocs/develop git checkout -f 

在開發服務器它可能是有人直接駐留在/ SRV/WWW的工作樹所做的更改的情況下/ htdocs中/發展離不開使用Git(是的,我知道,不要問......)。

現在:什麼是最好的方式來防止在開發服務器上的結帳,這會破壞非gitters修改?

目前我使用呼應的消息,並退出1.

回購服務器上預先接收鉤模擬這種情況下,這將是,如果我做偉大的

$ git push origin development 

和git會回答像這樣:

Counting objects: 11, done. 
Delta compression using up to 2 threads. 
Compressing objects: 100% (6/6), done. 
Writing objects: 100% (9/9), 706 bytes, done. 
Total 9 (delta 3), reused 0 (delta 0) 
remote: Unclean working directory on development server. Please fix that first. 
To [email protected]:development 
! [remote rejected] development -> development (pre-receive hook declined) 
error: failed to push some refs to [email protected]:development' 
+0

恕我直言,這不是一個不尋常的用例,而是一個非常常見的用例。事實上,我認爲這是部署網站的明顯方式。我實際上已經在我的舊工作中工作了,並試圖重新找到我當時的答案......它甚至會給出已更改的文件列表。 – rjmunro 2012-02-02 18:13:00

回答

0

我使用這裏提到的「官方」 post-update鉤:https://stackoverflow.com/a/3838796/3408 以防止更新REM在推後檢查。

我拿了一些代碼來初始化環境,並在預接收鉤子中創建了一個腳本來運行git diff --exit-code --stat--exit-code表示如果有差異,推送將被拒絕。

#!/bin/bash 

is_bare=$(git config --get --bool core.bare) 

if [ -z "$is_bare" ] 
then 
    # for compatibility's sake, guess 
    git_dir_full=$(cd $GIT_DIR; pwd) 
    case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac 
fi 

if [ "$is_bare" = "false" ] 
then 
    active_branch=`git symbolic-ref HEAD` 
    export GIT_DIR=$(cd $GIT_DIR; pwd) 
    GIT_WORK_TREE=${GIT_WORK_TREE-..} 
    while read oldrev newrev ref 
    do 
     if [ "$ref" = "$active_branch" ] 
     then 
      (cd $GIT_WORK_TREE; git diff --exit-code --stat >&2) 
     fi 
    done 
fi 

我剛剛得到這個工作,它只是基本測試。其他人可能會評論這是否可以簡化或改進。

相關問題