如何将推送的分支复制到另一个目录? [英] How to copy a pushed branch to another directory?
问题描述
我有一个名为 master
(Production)的分支,另一个名为 development
的分支。当我从master推送提交时,post-receive钩子执行:
git --work-tree = / var / www /mywebsite.com --git-dir = / var / repo / mywebsite.com.git checkout -f
<网站是活的。但我希望我的开发
分支复制到/var/www/mywebsite.com.dev。
你想要做的是 Capistrano 这样的工具,他们会做得更好(例如,你的版本不是原子的,所以你的网站混合了一些旧/新的文件,而capistrano会检查你的目录树,然后指向它的符号链接,这是原子)。
如果您仍然使用git checkout,那么git checkout development将签出开发分支,因此您希望在收到ref关于开发的更新。该脚本将沿用以下内容:
#!/ bin / sh
在阅读时新的refname
do
在
refs / heads / master中$$ refname
git --work-tree = / var / www / mywebsite.com --git- dir = / var / repo / mywebsite.com.git checkout -f
;;
refs / heads / development)
git --work-tree = / var / www / mywebsite.com.dev --git-dir = / var / repo / mywebsite.com.git checkout -f refs / heads / development
;;
esac
完成
现在,我们可以通过几种方式改进脚本:
$ b
-
实际上,由于钩子接收到新的ref值,你甚至不需要分支名称,并且可以使用$新的,而不是更新(更强大的情况下,有几个ref更新在时间上接近彼此)。为了提高效率,我们可以在每次结帐时维护一个索引文件(所以未修改的文件不需要实际签出)
-
--git-dir选项可以被删除,因为我们正在检查当前目录(该钩子在仓库中执行)。
改进的脚本是:
#!/ bin / sh
读取旧的新的refname
做
的情况$ refname in
refs / heads / master)
GIT_INDEX_FILE =$ PWD/index.master git --work-tree = / tmp / www / mywebsite.com checkout -f $ new
;;
refs / heads / development)
GIT_INDEX_FILE =$ PWD/index.development git --work-tree = / tmp / www / mywebsite.com.dev checkout -f $ new
;;
esac
完成
在相关主题上,您还可以查看在Git 2.3中引入了 receive.denyCurrentBranch
的新 updateInstead
值。例如,请参见 https://上的推送部分github.com/blog/1957-git-2-3-has-been-released
I have a branch called master
(Production) and another called development
. When I push a commit from master, the post-receive hook executes:
git --work-tree=/var/www/mywebsite.com --git-dir=/var/repo/mywebsite.com.git checkout -f
And the website is live. But I want my development
branch copied to /var/www/mywebsite.com.dev .
What you're trying to do by hand is what tools like Capistrano are meant for, and they will do it better (for example, your version is not atomic so your website has a mix of old/new files for a while, while capistrano will check out your tree in a directory, and then point a symlink to it, which is atomic).
If you stay with "git checkout", then "git checkout development" will checkout the development branch, so you want to execute this command when you receive a ref update on development. The script would go along the lines of:
#!/bin/sh
while read old new refname
do
case "$refname" in
refs/heads/master)
git --work-tree=/var/www/mywebsite.com --git-dir=/var/repo/mywebsite.com.git checkout -f
;;
refs/heads/development)
git --work-tree=/var/www/mywebsite.com.dev --git-dir=/var/repo/mywebsite.com.git checkout -f refs/heads/development
;;
esac
done
Now, we can improve the script in several ways:
Actually, since the hook receives the new ref value, you don't even need the branch name, and can use $new instead (more robust in case there are several ref updates close to each other in time).
For efficiency we can maintain one index file per checkout (so that unmodified files do not need to be actually checked-out)
The --git-dir option can be dropped, since we're checking out from the current directory (the hook is executed within the repository).
The improved script is:
#!/bin/sh
while read old new refname
do
case "$refname" in
refs/heads/master)
GIT_INDEX_FILE="$PWD"/index.master git --work-tree=/tmp/www/mywebsite.com checkout -f $new
;;
refs/heads/development)
GIT_INDEX_FILE="$PWD"/index.development git --work-tree=/tmp/www/mywebsite.com.dev checkout -f $new
;;
esac
done
On a related subject, you can also look at the new updateInstead
value for receive.denyCurrentBranch
introduced in Git 2.3. See for example the "Push to deploy" section on https://github.com/blog/1957-git-2-3-has-been-released
这篇关于如何将推送的分支复制到另一个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!