Git post-receive hook将每个分支签出到不同的文件夹? [英] Git post-receive hook to checkout each branch to different folders?
问题描述
我们的Git工作流程的基本设置是在本地网络服务器上的裸仓库,其中有两位开发人员推/拉它。
我们想自动复制(结帐)每个分支被推送到我们的本地网络服务器上的不同位置。举例说明:
将'develop'分支副本推送到'develop'子文件夹。
将'master'分支拷贝到'master'子文件夹中。
我们遇到的问题是获取post-receive钩子来执行此操作。以下是我们目前拥有的:
#!/ bin / bash
while read oldrev newrev ref
做
分支=`echo $ ref | cut -d / -f3`
if [master==$ branch];然后
GIT_WORK_TREE = / master
git checkout -f $ branch
echo'变更推送主。'
fi
if [develop== $ branch];然后
GIT_WORK_TREE = / develop
git checkout -f $ branch
echo'推动开发的变化'
fi
完成
$ c $收到的错误是:
'remote:fatal:必须运行此操作在工作树中
remote:推动开发的变化。'
正如您对该错误期望的那样 - 没有任何内容会被检出。
我也尝试过这种方法,但是同样的问题:
#!/ bin / bash
while read oldrev newrev ref
do
branch =`echo $ ref | cut -d / -f3`
if [master==$ branch];然后
git --work-tree = / master checkout -f $ branch
echo'变更推送的主人。'
fi
if [develop== $ branch];然后
git --work-tree = / develop checkout -f $ branch
echo'推动开发的变化'
fi
完成
任何人都可以在这里解释我在做什么错误(随意解释它,就像你会3岁:))。谢谢。
为了让未来的读者更清楚地知道答案,Torek将其击中头部。我正在使用 - work-tree = / master
来尝试访问我裸露回购的根目录中名为'master'的文件夹(例如旁边的'branches' '钩'等)。只要我将它改为 - work-tree =。/ master
(注意正斜杠前面的点),一切都按我的预期工作。
解决方案 你得到这个是因为 / master
和 / develop
是不存在的目录:
$ git --work-tree = / nonexistent checkout master
fatal:此操作必须在工作树中运行
您可能还想查看我对一个关于另一个问题的问题的回答,这个问题突然出现在这个方法中,这也解决了你从流行中复制的一个小错误 - 但是错误的post-receive技术(使用 cut
来分析更新后的ref)。
[Your措辞也让我怀疑你是否想把这两个分支部署到接收推送的(大概是 - bare
)存储库中的一个子目录中。这可能不是一个好主意。]
部署的另一种(不同的)方法是在部署位置有一个真正的git树。然后,而不是 git --work-tree = ... checkout
你可以这样做:
deploy()
{
local path = $ 1 branch = $ 2
(cd $ path&&&unset GIT_DIR&& git fetch& git checkout -f origin / $ branch)
}
(未经测试,随时进行实验和/或修改)。这与磁盘空间和更新窗口(我在其他答案中提到的)有其他稍微不同的折衷。
The basic setup of our Git workflow is a bare repository on a local network server with two developers pushing/pulling to it.
We would like to automagically copy (checkout) each branch that gets pushed to a different location on our local network server. To exemplify:
Pushing the 'develop' branch copies to 'develop' sub-folder.
Pushing the 'master' branch copies to the 'master' sub-folder.
The problem we are having is getting the post-receive hook to do this. Here is what we currently have:
#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ]; then
GIT_WORK_TREE=/master
git checkout -f $branch
echo 'Changes pushed master.'
fi
if [ "develop" == "$branch" ]; then
GIT_WORK_TREE=/develop
git checkout -f $branch
echo 'Changes pushed to develop.'
fi
done
The error received is:
'remote: fatal: This operation must be run in a work tree
remote: Changes pushed to develop.'
As you would expect from that error - nothing is actually checked out.
I had also tried the post-receive this way but the same issue:
#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ]; then
git --work-tree=/master checkout -f $branch
echo 'Changes pushed master.'
fi
if [ "develop" == "$branch" ]; then
git --work-tree=/develop checkout -f $branch
echo 'Changes pushed to develop.'
fi
done
Can anyone explain what I am doing wrong here (feel free to explain it like you would to a 3-year old :)). Thanks.
To make the answer clearer for future readers, Torek hit it on the head. I was using --work-tree=/master
to try and get to to a folder called 'master' inside the root of my bare repo (e.g. alongside 'branches', 'hooks' etc). As soon as I changed this to --work-tree=./master
(note the dot before the forward slash) everything worked as I expected.
解决方案 You're getting this because /master
and /develop
are non-existent directories:
$ git --work-tree=/nonexistent checkout master
fatal: This operation must be run in a work tree
You may also want to see my answer to a question about another problem that crops up with this approach, which also addresses a small bug you've copied from a popular-but-wrong post-receive technique (the use of cut
to parse the updated ref).
[Your phrasing also makes me wonder if you are thinking of deploying those two branches into a sub-directory within the (presumably --bare
) repository that is receiving the pushes. This is probably not a great idea.]
Another (different) method for deploying is to have a "real" git tree in the deployment location. Then, instead of git --work-tree=... checkout
you do something like this instead:
deploy()
{
local path=$1 branch=$2
(cd $path && unset GIT_DIR && git fetch && git checkout -f origin/$branch)
}
(untested, feel free to experiment and/or modify). This has other, slightly different tradeoffs with respect to disk space and update windows (which I mention in the other answer).
这篇关于Git post-receive hook将每个分支签出到不同的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文