Redmine用Git repo镜像脚本

一般我用git来管理代码,后台用gitoliteredmine来管理项目,redmine中可以结合git来参看git提交信息时,非常有用。

以下代码参照了http://blog.buginception.com/blog/2012/04/02/redmine-use-git-code-review

make-mirror.shview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash

set -x

if [ "$1" = "" ]; then
echo "Usage: $0 [repo]"
exit 1
fi

REPO=$1.git
GIT_BASE=/home/git/repositories
MIRROR_PATH=/home/jxb/git-mirrors
REPO_PATH=$MIRROR_PATH/$REPO

# 我們要使用 git clone --mirror 建立一個原始 repo 的鏡像:
cd $MIRROR_PATH && git clone --mirror $GIT_BASE/$REPO

# 接下來下一步,由於之後 git 使用者會透過 post-receive hook 來同步兩個 repository,
# 我們直接修改這個境像 repository 的 owner / group 為 git 來讓它有讀寫權限。
chown -R git:git $REPO_PATH

# 再來我們要設定原本 repo 的 hooks :
cd $GIT_BASE/$REPO/hooks

cat > post-receive <<EOS
#!/bin/bash
/usr/bin/git push --mirror $REPO_PATH
EOS

# 建立完 post-receive 檔案後修改權限:
chown git:git post-receive
chmod 700 post-receive

# 由於 Git 在做 mirror push 的時候,會保留原始的檔案與資料夾存取權限,
# 在 gitolite 控管下,只有 owner 有讀寫權限,所以一做 mirror push 這個鏡像的 repository 就沒辦法被其他 process 讀取到了,
# 所以我們一開始就要告訴這個 mirror repository 它是被分享的,並且設定它應該要有的存取權限:
sudo -u git sh <<EOS
set -x
cd $REPO_PATH
chmod a+rX -R ./
git config --add core.sharedRepository 644
EOS

使用时需要用root权限

sudo sh make-mirror.sh <REPO-NAME> # REPO-NAME 中不带.git后缀

如果不需要特别强大的项目管理功能,可以用gitlabhq,这是一个类似github的代码管理应用,支持fork,pull request功能,非常实用,选择哪个用来管理也就见仁见智了。