014.Repo 使用入门
# 什么是 Repo
随着软件项目的发展,一个软件项目中可能存在上百个 git 仓库,这个时候我们就需要一个工具来管理多个 git 仓库,不然一个个操作这些仓库,要把开发累死。Repo 就是这个管理多个 git 仓库的工具。
# 自己搭建一个 Repo 仓库
搭建清单 git 仓库
mkdir RepoLearn
touch default.xml
1
2
2
接着编辑 default.xml 文件,其内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<manifest>
<!-- remote 节点用于指定远程仓库 -->
<!--
name: 远程仓库名,在使用 git clone 时默认为 origin
fetch: 远程仓库地址,用户和 project 节点中的 name 一起确定项目地址
-->
<!-- remote server -->
<remote
name="github"
fetch="https://github.com" />
<remote
name="rx"
fetch="https://github.com/ReactiveX" />
<remote
name="square"
fetch="https://github.com/square" />
<!-- default 节点用于指定后续 project 节点的默认属性 -->
<!--
remote: 默认使用的远程仓库
revision: 默认使用的分支
sync-j: 同步代码时的并发数
-->
<default
remote="github"
revision="master"
sync-j="4" />
<!--
project: 单个代码库配置
name: 项目地址,和指定的 remote 节点中的 fetch 一起组成仓库地址(fetch + name)
group: 项目所属分组,可选
path: 项目拉取到本地后所在目录
remote: 指定所在远程仓库
revision: 指定代码分支,这里使用 refs/tags/v3.1.8,表示使用特定版本的 tag
-->
<!-- RxJava -->
<project
name="RxJava.git"
group="android,rx"
path="lib/RxJava"
remote="rx"
revision="refs/tags/v3.1.8" />
<!-- OkHttp -->
<project
name="okhttp.git"
group="android,square"
path="lib/OkHttp"
remote="square" />
</manifest>
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
43
44
45
46
47
48
49
50
51
52
53
54
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
43
44
45
46
47
48
49
50
51
52
53
54
接着初始化当前 git 仓库。
git init
git add .
git commmit -m "init"
1
2
3
2
3
接着在 gitee 上创建一个新仓库 RepoLearn。然后我们将本地仓库与远程仓库绑定:
git remote add origin git@gitee.com:yuandaimaahao/repo-learn.git
git push -u origin "master"
1
2
2
至此,我们的清单仓库就完成了。
接下来我们就可以使用这个清单仓库来下载代码了:
mkdir test
cd test
repo init -u git@gitee.com:yuandaimaahao/repo-learn.git -b master
repo sync
1
2
3
4
2
3
4
Usage:
repo init -u URL [OPTIONS]
Options:
-u:指定manifest项目清单库地址。
-m,–manifest-name:指定manifests库中的清单文件,默认为maniftests/default.xml。
-b, –manifest-branch:指定manifest仓的分支,默认为master分支。
-g:指定manifests库中的组来下载代码,默认为all。
不常用参数:
–-repo-url:指定远程repo库地址,当引导脚本中的地址不可访问时,可以通过该参数指定可访问的repo地址。
–-repo-branch:同manifest这个git库一样,repo这个git库也是有版本差异的,可以通过该参数来指定下载repo这个远程git库的特定分支。
–-no-repo-verify:在下载repo库时,会对repo的源码进行检查。通过–repo-url指定第三方repo库时,可能会导致检查不通过,所以可以配套使用该参数,强制不进行检查。
--depth {number}:限制下载记录次数,加速代码下载。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
经过上述的操作,我们在 default.xml 指定的两个 git 仓库就被下载到 lib/RxJava
和 lib/OkHttp
目录下了
# 使用 Repo 开发的流程
常见的工作流:
repo init # 初始化仓库清单文件
repo sync # 同步代码
repo start main-x.x.x --all # 在所有的 git 仓库中开启一个分支,分支名自定义
# 接下来进入到具体某个 git 仓库继续开发工作
# 到这里,就和普通的单仓库开发流程就是一样的了,可以使用自己喜欢的 git 工作流,如果开了新分支,开发完成后,记得把代码合并到 main-x.x.x 分支中
# 完成功能开发后,有两个选择
# 选择一,在单个仓库中通过 git push 上传代码
# 选择二,使用 repo upload 将代码上传到 gerrit 审核服务器上,人工审核后再上传到 git 远程仓库中
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9