repo使用

repo使用



1、repo介绍

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

1.1 清单库文件介绍

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

  • remote元素,定义了名为korg的远程版本库,其库的基址为git://172.16.1.31/。
  • default元素,设置各个项目默认远程版本库为korg,默认的的分支为gingerbread-exdroid-stable。当然各个项目(project元素)还可以定义自己的remote和revision覆盖默认的配置。
  • 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:

  • -u:指定一个URL,其连接到一个maniest仓库。
  • -m:在manifest仓库中选择一个xml文件。
  • -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

在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,如果项目版本库已经存在,则相当于执行下面的两条指令:

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

  • 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/build、platform/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-index、git diff-filse命令的封装,同时显示暂存区的状态和本地文件修改的状态

$repo/repo status platform/bionic

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

  • 每个小节的首行显示羡慕名称,以及所在分支的名称
  • 第一个字母表示暂存区的文件修改状态
    • -:没有改变
    • A:添加(不在HEAD中,在暂存区中)
    • M:修改(在HEAD中,在暂存区中,内容不同)
    • D:删除(在HEAD中,不在暂存区)
    • R:重命名(不在HEAD中,在暂存区,路径修改)
    • C:拷贝(不在HEAD中,在暂存区,从其他文件拷贝)
    • T:文件状态改变(在HEAD中,在暂存区,内容相同)
    • U:未合并,需要冲突解决
  • 第二个字母表示工作区文件的更改状态
    • -:新/未知(不在暂存区,在工作区)
    • m:修改(在暂存区,在工作区,被修改)
    • 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.13 repo forall

Usage:

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

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

Options:
- -c:后面所带的参数着是shell指令
- -p:在shell指令输出之前列出项目名称
- -v:列出执行shell指令输出的错误信息
additional environment variables:
- REPO_PROJECT:指定项目的名称
- REPO_PATH:指定项目在工作区的相对路径
- REPO_REMOTE:指定项目远程仓库的名称
- REPO_LREV:指定项目最后一次提交服务器仓库对应的哈希值
- REPO_RREV:指定项目在克隆时的指定分支,manifest里的revision属性

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

2.13.1添加的环境变量
repo forall –c ‘echo $REPO_PROJECT’
repo forall  –c ‘echo $REPO_PATH
2.13.2 merge(合并多个分支)

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

repo forall –p –c git merge topic
2.13.3 tag(打标签)

在所有项目下打标签

repo forall –c git tag crane-stable-1.6
2.13.4 remote (设置远程仓库)

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

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

删除远程仓库:

repo forall –c git remote add korg
2.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:

  • -h, –help:显示帮助信息
  • -t:发送本地分支名称到Gerrit代码审核服务器
  • –replace:发送此分支的更新补丁集
  • –re=REVIEWERS:要求指定的人员进行审核
  • –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

5、添加push指令

在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"

转载自:http://blog.sina.com.cn/s/blog_89f592f50100vpau.html


  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
To make edits to changes after they have been uploaded, you should use a tool like git rebase -i or git commit --amend to update your local commits. After your edits are complete: Make sure the updated branch is the currently checked out branch. For each commit in the series, enter the Gerrit change ID inside the brackets: # Replacing from branch foo [ 3021 ] 35f2596c Refactor part of GetUploadableBranches to lookup one specific... [ 2829 ] ec18b4ba Update proto client to support patch set replacments # Insert change numbers in the brackets to add a new patch set. # To create a new change record, leave the brackets empty. After the upload is complete the changes will have an additional Patch Set. If you only want to upload the currently checked out Git branch, you can use the flag --current-branch (or --cbr for short). diff repo diff [<PROJECT_LIST>] Shows outstanding changes between commit and working tree using git diff. download repo download <TARGET> <CHANGE> Downloads the specified change from the review system and makes it available in your project's local working directory. For example, to download change 23823 into your platform/build directory: repo download platform/build 23823 A repo sync should effectively remove any commits retrieved via repo download. Or, you can check out the remote branch; e.g., git checkout m/master. Note: There is a slight mirroring lag between when a change is visible on the web in Gerrit and when repo download will be able to find it for all users, because of replication delays to all servers worldwide. forall repo forall [<PROJECT_LIST>] -c <COMMAND> Executes the given shell command in each project. The following additional environment variables are made available by repo forall: REPO_PROJECT is set to the unique name of the project. REPO_PATH is the path relative to the root of the client. REPO_REMOTE is the name of the remote system from the manifest. REPO_LREV is the name of the revision from the manifest, translated to a local tracking branch. Used if you need to pass the manifest revision to a locally executed git command. REPO_RREV is the name of the revision from the manifest, exactly as written in the manifest. Options: -c: command and arguments to execute. The command is evaluated through /bin/sh and any arguments after it are passed through as shell positional parameters. -p: show project headers before output of the specified command. This is achieved by binding pipes to the command's stdin, stdout, and sterr streams, and piping all output into a continuous stream that is displayed in a single pager session. -v: show messages the command writes to stderr. prune repo prune [<PROJECT_LIST>] Prunes (deletes) topics that are already merged. start repo start <BRANCH_NAME> [<PROJECT_LIST>] Begins a new branch for development, starting from the revision specified in the manifest. The <BRANCH_NAME> argument should provide a short description of the change you are trying to make to the projects.If you don't know, consider using the name default. The <PROJECT_LIST> specifies which projects will participate in this topic branch. Note: "." is a useful shorthand for the project in the current working directory. status repo status [<PROJECT_LIST>] Compares the working tree to the staging area (index) and the most recent commit on this branch (HEAD) in each project specified. Displays a summary line for each file where there is a difference between these three states. To see the status for only the current branch, run repo status. The status information will be listed by project. For each file in the project, a two-letter code is used: In the first column, an uppercase letter indicates how the staging area differs from the last committed state. letter meaning description - no change same in HEAD and index A added not in HEAD, in index M modified in HEAD, modified in index D deleted in HEAD, not in index R renamed not in HEAD, path changed in index C copied not in HEAD, copied from another in index T mode changed same content in HEAD and index, mode changed U unmerged conflict between HEAD and index; resolution required In the second column, a lowercase letter indicates how the working directory differs from the index. letter meaning description - new/unknown not in index, in work tree m modified in index, in work tree, modified d deleted in index, not in work tree Was this page helpful? Let us know how we did:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值