Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git post接收挂钩不';t从主服务器中删除已删除的文件_Git_Git Post Receive - Fatal编程技术网

Git post接收挂钩不';t从主服务器中删除已删除的文件

Git post接收挂钩不';t从主服务器中删除已删除的文件,git,git-post-receive,Git,Git Post Receive,我在我的服务器上创建了一个裸git repo,并从中设置了以下post接收挂钩: 因此,每当我在本地推送到服务器时,更改将自动发布到每个分支的文件夹中,而无需手动拉取 我为live和dev文件夹设置了正确的权限: drwxrwsr-x 2 git git 4096 Sep 29 12:10 live/ drwxrwsr-x 2 git git 4096 Sep 29 12:09 dev/ 而开发部门的推动工作也如期进行。当我签出主分支并进行合并时,就会出现问题。当我按下主控按钮时,新文件会被复

我在我的服务器上创建了一个裸git repo,并从中设置了以下post接收挂钩:

因此,每当我在本地推送到服务器时,更改将自动发布到每个分支的文件夹中,而无需手动拉取

我为live和dev文件夹设置了正确的权限:

drwxrwsr-x 2 git git 4096 Sep 29 12:10 live/
drwxrwsr-x 2 git git 4096 Sep 29 12:09 dev/
而开发部门的推动工作也如期进行。当我签出主分支并进行合并时,就会出现问题。当我按下主控按钮时,新文件会被复制到我服务器上的live文件夹中,但我在本地删除的文件不会被删除


如何使post接收正确更新live文件夹?谢谢

问题是git不知道删除什么(它在工作树中没有索引,无法跟踪这些内容)。使用每个工作树的索引应该可以解决这个问题,但我认为更简单的方法是将git checkout-f放入一个新的空目录,然后重命名新目录和旧目录,使新版本“上线”。这也缩小了竞争条件窗口:现在没有版本时只有一个短暂的时刻(在
mv
操作之间),而不是在新旧版本混合时有一个稍长的窗口(在
checkout

注意:如果有一个名为
master
develope
的标记,您显示的脚本将出错,因为这两个标记的参考名称分别是
refs/tags/master
refs/tags/develope
。我建议通过shell函数和
case
语句解决此问题(如果您愿意:-),以减少非部署情况下的进程生成,例如:

die() {
    echo "$@" >&2
    exit 1
}

# deploy (to given path, $1) the version named by $2
# if the target is /some/path/there we use a temp version
# named /some/path/tmp.<pid> to hold the new one until we
# can swap it out, and $1.old.<pid> while we remove the old.
deploy() {
    local path=$1 branch=$2
    local tmpdir=${path%/*}/tmp.$$        # tune this as needed

    echo "deploying $branch to $path via $tmpdir..."
    trap "rm -rf $tmpdir" 0 1 2 3 15
    mkdir $tmpdir || die "can't create work dir $tempdir"
    git --work-tree=$tmpdir/ checkout -f $branch
    mv $path $path.old.$$ ||
        die "unable to move live version out of the way"
    mv $tmpdir $path ||
        die "unable to set new version live"
    trap - 0 1 2 3 15
    echo "done, cleaning up old version"
    rm -rf $path.old.$$
}

while read oldrev newrev ref; do
    case $ref in
    refs/heads/master) deploy /path/to/my/project/live master;;
    refs/heads/develop) deploy /path/to/my/project/dev develop;;
    esac
done
die(){
回显“$@”>&2
出口1
}
#部署(到给定路径,$1)以$2命名的版本
#如果目标是/some/path/那么我们使用临时版本
#命名/some/path/tmp。把新的拿着直到我们
#可以换掉,1.5美元。当我们移除旧的。
部署(){
本地路径=$1分支=$2
本地tmpdir=${path%/*}/tmp。$$#根据需要对其进行调优
echo“通过$tmpdir将$branch部署到$path…”
陷阱“rm-rf$tmpdir”01 2 3 15
mkdir$tmpdir | | die“无法创建工作目录$tempdir”
git——工作树=$tmpdir/checkout-f$branch
mv$path$path.old.$$||
die“无法移动实时版本”
mv$tmpdir$path||
die“无法将新版本设置为live”
陷阱-012315
echo“完成,清理旧版本”
rm-rf$path.old$$
}
读取oldrev-newrev-ref时;做
案例$ref in
refs/heads/master)部署/path/to/my/project/live master;;
refs/heads/develop)deploy/path/to/my/project/dev-develop;;
以撒
完成

(注意:完全未经测试)。

我遇到了同样的问题。我正在研究rsync将tmp文件夹文件复制到live文件夹,但后来我想为什么不在工作树上使用git glean呢

我不确定这是否会很糟糕,但它应该只从文件夹中删除未跟踪的文件,并使用.gitignore设置不删除不在repo中的文件

它似乎完成了我想要的,即清除未被推送删除的遗留文件

#!/bin/sh
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then
        GIT_WORK_TREE=/home/store/public_html git checkout -f master
        GIT_WORK_TREE=/home/store/public_html git clean -fd
    fi
    if [ "test" == "$branch" ]; then
        GIT_WORK_TREE=/home/store/test_html git checkout -f test
        GIT_WORK_TREE=/home/store/test_html git clean -fd
    fi
    if [ "dev" == "$branch" ]; then
        GIT_WORK_TREE=/home/store/dev_html git checkout -f dev
        GIT_WORK_TREE=/home/store/dev_html git clean -fd
    fi
done

git工作树是否能按预期工作,如果存储库是裸存储库??是的,或者至少它肯定能与env变量一起工作,它也应该与命令行选项一起工作。我得到一个错误:remote:mv:cannot move'/path/to/my/project/dev/'到它自己的子目录,“/path/to/my/project/dev/.old.847”和remote:无法将实时版本移到别处看起来您在“deploy”的参数中留下了一个斜杠。它要求您不要在那里包含它们(它会根据需要添加它们本身)。现在它似乎正在工作。非常感谢。我是否应该担心这个窗口,以防项目变得更大?
#!/bin/sh
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then
        GIT_WORK_TREE=/home/store/public_html git checkout -f master
        GIT_WORK_TREE=/home/store/public_html git clean -fd
    fi
    if [ "test" == "$branch" ]; then
        GIT_WORK_TREE=/home/store/test_html git checkout -f test
        GIT_WORK_TREE=/home/store/test_html git clean -fd
    fi
    if [ "dev" == "$branch" ]; then
        GIT_WORK_TREE=/home/store/dev_html git checkout -f dev
        GIT_WORK_TREE=/home/store/dev_html git clean -fd
    fi
done