公司有一个Gitblit服务器管理代码,现在想把正在开发的Android源代码项目也放到上面管理。听说,用一个Git仓库管理整个源代码库,在git status时会很慢,于是想到用官方的repo管理办法。
环境
本地系统:Win7上的Ubuntu 16.04 虚拟机
Windows服务器:Windows Server 2008 R2 Enterprise
安装repo
下载 repo 工具。
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
## 如果上述 URL 不可访问,可以用下面的:
## curl https://storage-googleapis.proxy.ustclug.org/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
测试repo
建立一个工作目录
~$ mkdir -p code/TestRepo
~$ cd code/TestRepo/
在Gitblit新建manifest.git
clone到本地
~/code/TestRepo$ git clone ssh://[email protected]:29418/~rong/manifest.git
新建default.xml
~/code/TestRepo$ cd manifest/
rong@ubuntu:~/code/TestRepo/manifest$ gedit default.xml
default.xml的内容如下
<?xml version="1.0" encoding="UTF-8" ?>
<manifest>
<remote name="origin" fetch="ssh://[email protected]:29418/~rong/" />
<default remote="origin" revision="master"/>
<project path="hello" name="helo"/>
</manifest>
保存、提交、推送到服务器
~/code/TestRepo/manifest$ git add .
~/code/TestRepo/manifest$ git commit -m "init commit"
~/code/TestRepo/manifest$ git push --all
查看服务器,提交成功。
初始化仓库:
~/code/TestRepo/manifest$ cd ..
~/code/TestRepo$ repo init -u ssh://[email protected]:29418/~rong/manifest.git
## 如果提示无法连接到 gerrit.googlesource.com,可以编辑 ~/bin/repo,把 REPO_URL 一行替换成下面的:
## REPO_URL = 'https://gerrit-googlesource.proxy.ustclug.org/git-repo'
创建helo.git
同步仓库
~/code/TestRepo$ repo sync
同步后的目录结构
实战repo
修改default.xml,添加MySight
<?xml version="1.0" encoding="UTF-8" ?>
<manifest>
<remote name="origin" fetch="ssh://[email protected]:29418/Android/" />
<default remote="origin" revision="master"/>
<project path="packages/apps/MySight" name="MySight"/>
</manifest>
初始化分支上的版本
$ repo init -u ssh://[email protected]:29418/Android/manifest.git -b new
把原来的MySight目录备份后删除,再用repo同步仓库
$ repo sync
注意:此工具有一个已知错误,可能会导致 repo sync 重置本地主题分支。如果在您运行 repo sync 之后,git branch 显示 *(无分支),请再次运行 git checkout。
发现用这个方式同步的仓库默认没有分支,需要手动切换分支
$ cd packages/apps/MySight
$ git checkout master
如果需要添加新的项目,可以修改default.xml,然后repo init
、repo sync
。为了方便我新建了一个文件myrepoinit.sh。
#!/bin/sh
repo init -u ssh://rong@192.168.60.254:29418/Android/manifest.git -b new
修改manifest后,初始化只要执行./myrepoinit.sh
。
总结
repo适合用于下载和更新源代码,或者是管理一个包含很多Git仓库的项目,平时提交代码还是要用Git。
Q&A
修改服务器的仓库位置后怎么在本地修改地址?
解:
例如修改manifest.git的位置后,执行
$ git remote set-url origin ssh://rong@192.168.60.254:29418/Android/manifest.git
用修改后的地址初始化repo
$ repo init -u ssh://[email protected]:29418/Android/manifest.git -b new
报错
~/code/TestRepo$ repo init
gpg: keyring `/home/rong/.repoconfig/gnupg/secring.gpg' created
gpg: keyring `/home/rong/.repoconfig/gnupg/pubring.gpg' created
gpg: /home/rong/.repoconfig/gnupg/trustdb.gpg: trustdb created
gpg: key 920F5C65: public key "Repo Maintainer <[email protected]>" imported
gpg: key 692B382C: public key "Conley Owens <[email protected]>" imported
gpg: Total number processed: 2
gpg: imported: 2 (RSA: 1)
fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
fatal: error [Errno 101] Network is unreachable
解:
编辑 ~/bin/repo,把 REPO_URL 一行替换成下面的:
REPO_URL = 'https://gerrit-googlesource.proxy.ustclug.org/git-repo'
连接失败
$ repo init -u ssh://rong@192.168.60.254:29418/~rong/manifest.git -b new
fatal: Cannot get https://gerrit-googlesource.proxy.ustclug.org/git-repo/clone.bundle
fatal: error [Errno 101] Network is unreachable
解:等一会就好了
参考
从零搭建 repo 服务器 - CSDN博客
http://blog.csdn.net/lb5761311/article/details/47723455
Repo全解之自己搭建repo仓库 - CSDN博客
http://blog.csdn.net/jinguol999/article/details/47192635
如何不翻墙下载Android代码 - CSDN博客
http://blog.csdn.net/sunao2002002/article/details/47869281
AOSP(Android) 镜像使用帮助 [LUG@USTC]
https://lug.ustc.edu.cn/wiki/mirrors/help/aosp
下载源代码 | Android Open Source Project
https://source.android.com/setup/downloading
概览 | Android Open Source Project
https://source.android.com/setup/developing