现在的位置: 首页 > 综合 > 正文

repo使用简介

2016年07月24日 ⁄ 综合 ⁄ 共 10074字 ⁄ 字号 评论关闭

1 repo介绍

Android 使用 Git 作为代码管理工具,开发了 Gerrit 进行代码审核以便更好的对代码进行集中式管理,还开发了 Repo 命令行工具,对 Git 部分命令封装,将百多个 Git 库有效的进行组织。

1.1    清单库文件介绍

一个清单库可以包含多个清单文件和多个分支,每个清单文件和分支都有对应的版本。清单文件以xml格式组织的。举个例子:

Ø         remote元素,定义了名为korg的远程版本库,其库的基址为git//172.16.1.31/

Ø         default元素,设置各个项目默认远程版本库为korg,默认的的分支为gingerbread-exdroid-stable。当然各个项目(project元素)还可以定义自己的remoterevision覆盖默认的配置

Ø         project元素,用于定义一个项目,path属性表示在工作区克隆的位置,name属性表示该项目的远程版本库的相对路径

Ø         project元素的子元素copyfile,定义了项目克隆后的一个附件动作,从src拷贝文件到dest

1.2 下载repo代码

$mkdir android2.3.4

$cd android2.3.4

$git clone git://172.16.1.31/repo.git

     于是在android目录下便有repo文件夹,里面包含了repo的源代码,里面有个repo脚本,用它来执行repo指令。

在本地开发的用户需要下载repo代码,在172.16.1.7服务器上开发的用户则不用下载repo代码,因为已经把repo脚本添加到了环境变量,执行repo
init
 就会附加的下载repo代码。

2 repo常用指令

备注:“*”表示新添加的指令

2.1 repo init (下载repo并克隆manifest)

Usage

repo init –u URL [OPTIONS]

Options:

l         -u:指定一个URL,其连接到一个maniest仓库

l         -m:manifest仓库中选择一个xml文件

l         -b:选择一个maniest仓库中的一个特殊的分支

命令repo init 要完成如下操作:

Ø         完成repo工具的完整下载,执行的repo脚本只是引导程序

Ø         克隆清单库manifest.git
(
地址来自于-u 参数)

Ø         克隆的清单库位于manifest.git中,克隆到本地.repo/manifests.清单.repo/manifest.xml只是符号链接,它指向.repo/manifests/default.xml

Ø         如果manifests中有多个xml文件,repo
init
 可以任意选择其中一个,默认选择是default.xml

Example

repo init  -u git://172.16.1.31/manifest.git

 
  repo使用


android2.3.4目录下面出现了.repo文件夹。

 

repo  init  -u git://172.16.1.31/manifest.git –m android.xml

选择的是android.xml里面的配置,.repo/manifest.xml便指向.repo/manifests/android.xml

2.2 repo sync(下载代码)

Usage:

repo sync [<project>…]

用于参照清单文件.repo/manifest.xml克隆并同步版本库。如果某个项目版本库尚不存在,则执行repo
sync
 命令相当于执行git clone,如果项目版本库已经存在,则相当于执行下面的两条指令:

l         git
remote update

相当于对每一个remote源执行了fetch操作

l         git
rebase origin/branch

针对当前分支的跟踪分支执行rebase操作。

Example:

repo sync

也可以选择克隆其中的一个项目:

repo sync platform/build

2.3 repo start(创建并切换分支)

Usage:

repo start  <newbranchname> [--all | <project>…]

    刚克隆下来的代码是没有分支的,repo start实际是对git
checkout –b
 命令的封装。为指定的项目或所有项目(若使用—all参数),以清单文件中为设定的分支,创建特性分支。这条指令与git
checkout –b
 还是有很大的区别的,git checkout –b 是在当前所在的分支的基础上创建特性分支,而repo
start
是在清单文件设定分支的基础上创建特性分支。

Example

  repo start  stable  --all

假设清单文件中设定的分支是gingerbread-exdroid-stable,那么执行以上指令就是对所有项目,在gingerbread-exdroid-stable的基础上创建特性分支stable

  repo start  stable  platform/build
platform/bionic

假设清单文件中设定的分支是gingerbread-exdroid-stable,那么执行以上指令就是对platform/buildplatform/bionic项目,在gingerbread-exdroid-stable的基础上创建特性分支stable

    

2.4 repo checkout(切换分支)

 Usage

repo checkout <branchname>  [<project>…]

实际上是对git checkout 命令的封装,但不能带-b参数,所以不能用此命令来创建特性分支。

Example

repo checkout crane-dev 

repo checkout crane-dev  platform/build  platform/bionic

2.5 repo branches(查看分支)

Usage

repo branches [<project>…]

Example

repo branches 

repo branches platform/build platform/bionic

2.6 repo diff(查看工作区文件差异)

 Usage

repo diff [<project>…]

   实际是对git
diff
 命令的封装,用于分别显示各个项目工作区下的文件差异。

Example

repo diff                            ---查看所有项目

repo diff platform/build platform/bionic  ---只查看其中两个项目

2.7 repo stage(把文件添加到index表中)

     实际是对git add --interactive命令的封装、用于挑选各个项目工作区中的改动以加入暂存区。

Usage

repo stage -i [<project>…]

    -i代表git
add --interactive
命令中的--interactive,给出个界面供用户选择

2.8 repo prune(删除已经合并分支)

   实际上是对git branch –d命令的封装,该命令用于扫面项目的各个分支,并删除已经合并的分支,用法如下:

repo prune [<project>…]

 

2.9 repo abandon(删除指定分支)

   实际上是对git branch –D 命令的封装,用法如下:

repo abandon <branchname> [<project>…]

2.10 repo status(查看文件状态)

实际上是对git diff-indexgit
diff-filse
命令的封装,同时显示暂存区的状态和本地文件修改的状态

$repo/repo status platform/bionic

repo使用

以上的实例输出显示了platform/bionic项目分支的修改状态

Ø         每个小节的首行显示羡慕名称,以及所在分支的名称

Ø         第一个字母表示暂存区的文件修改状态

l         -:没有改变

l         A:添加(不在HEAD中,在暂存区中)

l         M:修改(在HEAD中,在暂存区中,内容不同)

l         D:删除(在HEAD中,不在暂存区)

l         R:重命名(不在HEAD中,在暂存区,路径修改)

l         C:拷贝(不在HEAD中,在暂存区,从其他文件拷贝)

l         T:文件状态改变(在HEAD中,在暂存区,内容相同)

l         U:未合并,需要冲突解决

Ø         第二个字母表示工作区文件的更改状态

l         -:新/未知(不在暂存区,在工作区)

l         m:修改(在暂存区,在工作区,被修改)

l         d:删除(在暂存区,不在工作区)

Ø         两个表示状态的字母后面,显示文件名信息。如果有文件重名还会显示改变前后的文件名及文件的相似度

2.11 *repo remote(设置远程仓库)

Usage:

repo remote add <remotename>  <url> [<project>…] 

repo remote rm <remotename>  [<project>…]

Example:

repo remote add org ssh://172.16.1.31/git_repo

这个指令是根据xml文件添加的远程分支,方便于向服务器提交代码,执行之后的build目录下看到新的远程分支org

repo使用

删除远程仓库:

$repo  remote  rm  org

2.12 *repo push

repo push org

   这是新添加的指令,用于向服务器提交代码,使用方法:

repo push <remotename> [--all |<project>…]

repo会自己查询需要向服务器提交的项目并提示用户。

2.13repo forall

 Usage

repo forall [<project>…] –c <command>

迭代器,可以在所有指定的项目中执行同一个shell指令

 Options

l         -c:后面所带的参数着是shell指令

l         -p:shell指令输出之前列出项目名称

l         -v:列出执行shell指令输出的错误信息

 additional environment variables:

l         REPO_PROJECT:指定项目的名称

l         REPO_PATH:指定项目在工作区的相对路径

l         REPO_REMOTE:指定项目远程仓库的名称

l         REPO_LREV:指定项目最后一次提交服务器仓库对应的哈希值

l         REPO_RREV:指定项目在克隆时的指定分支,manifest里的revision属性

 另外,如果-c后面所带的shell指令中有上述环境变量,则需要用单引号把shell指令括起来。

3.13.1 添加的环境变量

 

repo forall –c ‘echo $REPO_PROJECT’

 
 
repo使用

  

$repo forall  –c ‘echo $REPO_PATH’

 
   

 

 

3.13.2 merge(合并多个分支)

    把所有项目多切换到master分支,执行以下指令将topic分支合并到master分支

 

repo forall –p –c git merge topic

 
   

3.13.3 tag(打标签)

在所有项目下打标签

repo forall –c git tag crane-stable-1.6

3.13.4 remote
(
设置远程仓库)

引用环境变量REPO_PROJECT添加远程仓库:

repo forall –c ‘git remote add korg ssh://xiong@172.16.31/$REPO_PROJECT.git’

删除远程仓库:

repo forall –c git remote add korg

3.13.5 branch(创建特性分支)

repo forall –c git branch crane-dev

repo forall –c git checkout –b crane-dev

3 repo的额外命令集

3.1 repo grep

相当于对git grep 的封装,用于在项目文件中进行内容查找

3.2 repo manifest

显示manifest文件内容

Usage:

repo manifest –o android.xml

3.3 repo version

显示repo的版本号

3.4 repo upload

repo upload相当于git push,但是又有很大的不同。它不是将版本库改动推送到克隆时的远程服务器,而是推送到代码审核服务器(Gerrit软件架设)的特殊引用上,使用SSH协议。代码审核服务器会对推送的提交进行特殊处理,将新的提交显示为一个待审核的修改集,并进入代码审查流程,只有当审核通过后,才会合并到官方正式的版本库中。

因为全志没有代码审核服务器,所以这条指令用不到。

 Usage

repo upload [--re --cc] {[<project>]… | --replace <project>}

 Options:

l         -h,
--help:
显示帮助信息

l         -t:发送本地分支名称到Gerrit代码审核服务器

l         --replace:发送此分支的更新补丁集

l         --re=REVIEWERS:要求指定的人员进行审核

l         --cc=CC:同时发送通知到如下邮件地址

3.5 repo download

主要用于代码审核者下载和评估贡献者提交的修订

Usage

repo download {project change [patchset]}…

3.6 repo selfupdate

    用于repo自身的更新

参考:

http://wenku.baidu.com/view/672c8faddd3383c4bb4cd257.html

4 添加的remote指令
   在sumcmds中添加remote.py,程序如下:
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from color import Coloring
from command import Command
from progress import Progress
class Remote(Command):
  common = True
  helpSummary = "View current topic branches"
  helpUsage = """
%prog add <remotebranchname> <url> [<project>...]
       %prog rm  <remotebranchname> [<project>...]
--------------
"""
  def str_last(self ,string):
     for c in string:
         last=c
     return last
  def Execute(self, opt, args):
    if not args:
      print >>sys.stderr, "error:..........wrong command........"
      print >>sys.stderr, "Usage:repo remote add <remotebranchname> <url> [<project>...]"
      print >>sys.stderr, "      repo remote rm  <remotebranchname> [<project>...] "          
      print >>sys.stderr, "................................"

 

 return
    err = []
    operate=args[0]
    #url = args[2]
   # branch_name=args[1]
    if operate == "rm":
       if not len(args) >=2:
         print >>sys.stderr, "error:miss remotebrancname"
         return
       branch_name=args[1]
       projects = args[2:]
    elif operate == "add":
       if not len(args) >=3:
         print >>sys.stderr, "error:miss remotebranchname or url"
         return
       branch_name=args[1]
       projects = args[3:]
    else:
       print >>sys.stderr, "error: the operand is add or rm "
       return
     
    all = self.GetProjects(projects)
   # print >>sys.stderr, all
    pm = Progress('remote %s' % operate, len(all))
    for project in all:
       if operate == "add":
          if self.str_last(args[2])=="/":

  

url = args[2]+project.name+'.git'
          else :
             url = args[2]+'/'+project.name+'.git'
       else:
         url = ""
       pm.update() 
       if not project.Remote(operate,branch_name,url):
         err.append(project)
    pm.end()
       
    if err:
      if len(err) == len(all):
        print >>sys.stderr, 'error: no project remote  %s %s' % (operate,branch_name)  
      else:
        for p in err:
          print >>sys.stderr,\
            "error: %s/: cannot remote %s %s " \
            % (p.relpath, operate,branch_name)
      sys.exit(1)
在preject.py中添加Remote(operate,branch_name,url)方法:
   def Remote(self,operate,branch_name,url):
     """Prune  topic branches already merged into upstream.
     """
     if url=="":   #rm
       return GitCommand(self,
                         ['remote', operate, branch_name],
                         capture_stdout = True,
capture_stderr = True).Wait() == 0
     else:  #add
       return GitCommand(self,
                         ['remote', operate, branch_name,url],
                         capture_stdout = True,
                         capture_stderr = True).Wait() == 0
 在subcmds中添加push.py,代码如下:
 #
# Copyright (C) 2010 JiangXin@ossxp.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import re
import sys
from command import InteractiveCommand
from editor import Editor
from error import UploadError, GitError
from project import ReviewableBranch
def _ConfirmManyUploads(multiple_branches=False):
  if multiple_branches:
    print "ATTENTION: One or more branches has an unusually high number of commits."
  else:
    print "ATTENTION: You are uploading an unusually high number of commits."
  print "YOU PROBABLY DO NOT MEAN TO DO THIS. (Did you rebase across branches?)"
  answer = raw_input("If you are sure you intend to do this, type 'yes': ").strip()
  return answer == "yes"
def _die(fmt, *args):
  msg = fmt % args
  print >>sys.stderr, 'error: %s' % msg
  sys.exit(1)
def _SplitEmails(values):
  result = []
  for str in values:
    result.extend([s.strip() for s in str.split(',')])
  return result
class Push(InteractiveCommand):
  common = True
  helpSummary = "Upload changes for code review"

抱歉!评论已关闭.