工具GIT 专题Android 之路

如何使用Google Repo管理复杂项目

2017-03-28  本文已影响1506人  afb2a83e89ca

当项目比较大的时候便于复用,或者多个项目共用module。这个时候需要抽取项目公共模块,而且现在基本是使用git作为版本控制工具,这样就变成App Project 有多个module,每个module都是一个独立的git repository。而且我们希望单个的module都可以独立维护,可以更方便查看和随时随地同步更新。

为了达成这样的目的我们可以使用git-submoduleRepo 、gitslave 、git-subtree。研究了submodule和repo后,还是决定使用repo来管理我的module。

其实repo更适合管理比较大、module比较多、module会经常变更的项目。如果module比较少的情况,使用git-submodule完全够用,但是submodule会有一些坑,可以参考唐巧的文章Git submodule的坑

1、关于Repo

Repo是管理Android源码时候会用到一组工具之一,其他两个是Git和Gerrit。Android使用Git作为代码管理工具,开发了Gerrit进行代码审核以便更好地对代码进行管理管理,还开发了 Repo 命令行工具,对Git部分命令进行了封装,将百多个Git库有效地组织起来。

Android本身是一个大的workspace,里面包含非常多的git repository。为了方便管理这些git repository,Google发明了Repo。Repo是由Google用Python script所写出來,专门用来使用Git的一套script,主要是用来下载、管理Android下所有的git repository。

使用Repo来管理Git仓库主要涉及repo库、项目manifest文件和module repository。

2、安装Repo

$ curl  https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo

$ chmod a+x~/bin/repo

$ curl  https://storage.googleapis.com/git-repo-downloads/repo 只是下载repo文件,如果在命令行中一直下载失败(比如我),可以直接用Chrome下载文件后放在/bin下。我使用Mac OS 10.12,无法放在~/bin目录,将Repo文件放在/usr/local/bin下,就安装成功了。

3、Module Repository

你可能将项目公共模块抽取出来,项目由App project + 多个module组成。比如将项目拆分成Module Base、Module uikit、Module util、Module component四个基础库。主项目再分别依赖这四个Module。现在分别将四个Module放在Git 服务器上就搞定了。

4、Manifest文件

此时你已经有其他module 的 repository。首先将你的App project Clone到本地,接着创建该项目repo manifest -- default.xml。你可以参考下面的manifest文件。

<manifest>

    <remote  name="bullet" fetch="https://github.com/" />

    <default revision="master" remote="bullet" sync-j="4" />

    <project name="bullet-base" remote="bullet" path="bullet_base"/>

    <project name="bullet-uikit" remote="bullet" path="bullet_uikit"/>

    <project name="bullet-util" remote="bullet" path="bullet_util"/>

    <project name="bullet-component" remote="bullet" path="bullet_component"/>

</manifest>

<remote>

首先需要配置该<remote> tag,这个tag是定义远程版本库相关的信息,给出远程Git库的访问路径。

<remote  name="该remote唯一的名字" fetch="module repository Host地址" />

当然你可以配置多个<remote>,如果你的module来自不同的地方。

<default>

接着你可以配置一些默认的行为,你可以具体说明你要Check out哪个分支,当然这个分支必须在你的.git 文件夹中,我通常使用master,我想要在项目里默认使用master分支。sync-j =“4”意思是指定四个子线程同时进行处理。

<default revision="master" remote="bullet" sync-j="4" />

<project>

现在我们需要使用<project> tag来添加我们的module repositories。

<project name="module在git服务器上的路径" remote="远程服务器地址 使用我们定义的remote tag" path="在本地的路径"/>

你可以添加非常多个的<project>,只要你愿意。

这个时候不要忘记 git add, git commit 将default.xml上传到服务器上。

5、使用Repo获取代码

这时候你已经将项目配置好,准备开始使用Repo。只要执行

repo init -u manifest_git_path -m manifest_file_name -b branch_name --repo-url=repo_url --no-repo-verify

因为我们manifest 使用的是默认的命名default.xml,所以可以不需要指定-m。

repo init -u git@github.com:xxx/bullet.git

这样就可以完成项目的初始化。我在这个时候遇到了错误 --

Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle 

虽然我已经翻墙了,但是在iTerm中执行就是报错。

因为当repo init时,执行的repo是环境变量里面默认的repo,这个repo只是单纯一个几百行的Python脚本而不是完整的repo-project,所以要先去网络远端sync完整的repo-project。

该问题可以通过增加如下option解决

repo init -u git@github.com:xxx/bullet.git --repo-url https://gerrit-google.tuna.tsinghua.edu.cn/git-repo

接着使用

repo sync

就成功的使用Repo 管理你的项目啦~

上一篇下一篇

猜你喜欢

热点阅读