Skip to content
On this page

REPO原理与使用


软件版本硬件版本更新内容

1.概述

repo起源自android项目,在android这个开源项目中存在700个左右的git仓库,当你下载android源码时,如果手动同步这么多的git仓库,必然很累又不可靠,所以即使没有repo,你也可能会想着通过一个脚本工具来同步android的git仓库,只不过现在google帮你写了这个脚本,并且还加了很多其他的功能,你可以直接使用。

repo不是用于替代git,而是依赖于git来工具,对仓库真正的操作还是通过git,只不过是通过repo调用git对每个仓库来进行操作. repo

2.repo一般使用流程

通常我们同步一个由repo管理的git仓库时,由如下几个:

  1. repo init 存入manifest文件的仓库地址,如从google同步android源码
c
repo init -u https://android.googlesource.com/platform/manifest
  1. repo sync 根据manifest文件配置的git仓库逐个同步
c
repo sync
  1. repo upload 来提交修改 当你同步完一份代码,修改完某个仓库的代码你可以如下指定提交
c
git add .
git commit
repo upload .

当然你也可以通过git直接提交

3.repo的目录结构

3.1 repo init之后的目录结构

当你通过repo init之后,在你执行命令的目录下会存在一个**.repo**文件夹,里面的内容如下

c
manifests  manifests.git  manifest.xml  repo

其中manifest.git是你执行repo sync后面加的那个manifest的git仓库,这个仓库里主要的文件就是一个xml文件,里面定义很多的git仓库,名子一般为default.xml,manifest是从mainfest.git中checkout出来的。manifest.xml是由repo创建,其内容一般如下:

c
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT THIS FILE!  It is generated by repo and changes will be discarded.
If you want to use a different manifest, use `repo init -m <file>` instead.

If you want to customize your checkout by overriding manifest settings, use
the local_manifests/ directory instead.

For more information on repo manifests, check out:
https://gerrit.googlesource.com/git-repo/+/HEAD/docs/manifest-format.md
-->
<manifest>
  <include name="default.xml" />
</manifest>

里面有的default.xml就是你mainfest文件夹中的default.xml 最后一个repo目录,repo的执行需要的脚本文件。

3.2 repo sync之后的目录结构

执行完repo sync之后,目录结构如下:

c
manifests  manifests.git  manifest.xml  project-objects  projects  repo

就是多个project-objects和projects两个目录,这两个目录里存放的在default.xml的记录的git仓库,其中projects中存在是git的子模块所在的仓库。

4. repo 常用命令介绍

  1. repo init -u url [options]
  • -u:指定从中检索清单代码库的网址。常见清单位于 https://android.googlesource.com/platform/manifest。
  • -m:选择代码库中的清单文件。如果未选择清单名称,则默认为 default.xml。
  • -b:指定修订版本,即特定的 manifest-branch。
  1. repo sync [project-list]

下载新的更改并更新本地环境中的工作文件,基本上可以在所有 Git 代码库中完成 git fetch。如果在未使用任何参数的情况下运行 repo sync,则该命令会同步所有项目的文件 运行 repo sync 后,将出现以下情况:

  • 如果目标项目从未同步过,则 repo sync 相当于 git clone。远程代码库中的所有分支都会复制到本地项目目录中。
  • 如果目标项目以前同步过,则 repo sync 相当于:
c
  git remote update
  git rebase origin/branch

其中 branch 是本地项目目录中当前已检出的分支。如果本地分支没有在跟踪远程代码库中的分支,则项目不会发生任何同步。

  • 如果 Git rebase 操作导致合并冲突,请使用常规 Git 命令(例如 git rebase --continue)来解决冲突。
  1. repo upload [<PROJECT_LIST>]

这条命令通过用于提交代码。

  1. repo forall [<PROJECT_LIST>] -c <COMMAND>
  • -c:要运行的命令和参数。此命令会通过 /bin/sh 进行求值,它之后的任何参数都将作为 shell 位置参数传递。
  • -p:在指定命令输出结果之前显示项目标头。这通过以下方式实现:将管道绑定到命令的 stdin、stdout 和 sterr 流,然后通过管道将所有输出结果传输到一个页面调度会话中显示的连续流中。
  • -v:显示该命令向 stderr 写入的消息。

这条命令很有用,其实是对每个git仓库执行-c后面的指定,可以git命令,也可以shell

  1. repo start <BRANCH_NAME> [<PROJECT_LIST>]

从清单中指定的修订版本开始,创建一个新的分支进行开发。 <BRANCH_NAME> 参数应简要说明您尝试对项目进行的更改。如果您不知道,则不妨考虑使用默认名称。 <PROJECT_LIST> 指定了将参与此主题分支的项目。

5.创建repo仓库

这里基于github的创建

  1. 创建一个mainfest仓库
  2. 在manifest仓库创建一个default.xml文件,内容如下
c
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<default remote="origin" revision="master" fetch="https://github.com/username" />
<project path="xxx" name="xxx"  />
<project path="xxx" name="xxx"  />
<project path="xxx" name="xxx"  />
<project path="xxx" name="xxx"  />
...
</manifest>

其中fetch地下+project中的name就是一个git仓库的地址。repo 会通过git来克隆这个git仓库。 具体这个xml的编写参考[3]文档

  1. 通过前面两步之后就可以通过repo sync来同步了。

参考文档

  1. repo layout
  2. repo command
  3. repo xml

提示

欢迎评论、探讨,如果发现错误请指正。转载请注明出处! 探索者


Released under the MIT License.