如何防止签出不干净的远程工作树

时间:2012-02-01 09:49:55

标签: git

我想为一个有点不寻常的用例设置git。

我有一个存储库服务器('repo'),我有一个我要部署的开发服务器('develop')。在repo服务器上,我有一个简单的回购('origin'),我将我的更改推送到我的本地克隆。

当我将名为'development'的分支推送到repo服务器时,我在那里有一个post-receive hook将分支推送到开发服务器:

#!/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

在开发服务器上,有人可能会直接在工作树中进行更改,而不使用git(是的,我知道,不要问......)。 / p>

现在:防止在开发服务器上结账会破坏非gitters修改的最佳方法是什么?

目前我使用repo服务器上的预接收挂钩来模拟这种情况,该挂钩回显消息并退出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 git@repo:development
 ! [remote rejected] development -> development (pre-receive hook declined)
error: failed to push some refs to git@repo:development'

1 个答案:

答案 0 :(得分:0)

我正在使用此处提到的“官方”post-update挂钩:https://stackoverflow.com/a/3838796/3408 防止在推送后更新远程结账。

我从中获取了一些代码以初始化环境,并制作了一个脚本以在预接收挂钩中运行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

我刚刚开始工作,它基本上只是经过测试。其他人可能会评论是否可以简化或改进。