关闭

Repo 详解

标签: repogit版本控制AndroidManifest
1344人阅读 评论(0) 收藏 举报
分类:

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"
  helpUsage="""
%prog <remotebranchname> {[<project>]... }
"""
  helpDescription = """
The '%prog' command is used to send changes to the Gerrit Code
Review system.  It searches for topic branches in local projects
that have not yet been published for review.  If multiple topic
branches are found, '%prog' opens an editor to allow the user to
select which branches to upload.

'%prog' searches for uploadable changes in all projects listed at
the command line.  Projects can be specified either by name, or by
a relative or absolute path to the project's local directory. If no
projects are specified, '%prog' will search for uploadable changes
in all projects listed in the manifest.

If the --reviewers or --cc options are passed, those emails are
added to the respective list of users, and emails are sent to any
new users.  Users passed as --reviewers must already be registered
with the code review system, or the upload will fail.

If the --replace option is passed the user can designate which
existing change(s) in Gerrit match up to the commits in the branch
being uploaded.  For each matched pair of change,commit the commit
will be added as a new patch set, completely replacing the set of
files and description associated with the change in Gerrit.

Configuration
-------------

review.URL.autoupload:

To disable the "Upload ... (y/n)?" prompt, you can set a per-project
or global Git configuration option.  If review.URL.autoupload is set
to "true" then repo will assume you always answer "y" at the prompt,
and will not prompt you further.  If it is set to "false" then repo
will assume you always answer "n", and will abort.

review.URL.autocopy:

To automatically copy a user or mailing list to all uploaded reviews,
you can set a per-project or global Git option to do so. Specifically,
review.URL.autocopy can be set to a comma separated list of reviewers
who you always want copied on all uploads with a non-empty --re
argument.

review.URL.username:

Override the username used to connect to Gerrit Code Review.
By default the local part of the email address is used.

The URL must match the review URL listed in the manifest XML file,
or in the .git/config within the project.  For example:

  [remote "origin"]
    url = git://git.example.com/project.git
    review = http://review.example.com/

  [review "http://review.example.com/"]
    autoupload = true
    autocopy = johndoe@company.com,my-team-alias@company.com

References
----------

Gerrit Code Review:  http://code.google.com/p/gerrit/

"""


   
    

  def _SingleBranch(self, opt, branch):
    project = branch.project
    name = branch.name
    remote = project.GetBranch(name).remote

    key = 'review.%s.autoupload' % remote.review
    answer = project.config.GetBoolean(key)

    if answer is False:
      _die("upload blocked by %s = false" % key)

    if answer is None:
      date = branch.date
      list = branch.commits

      print 'Upload project %s/:' % project.relpath
      print '  branch %s (- commit%s, %s):' % (
                    name,
                    len(list),
                    len(list) != 1 and 's' or '',
                    date)
      for commit in list:
        print '         %s' % commit

      pushurl = project.manifest.manifestProject.config.GetString('repo.pushurl')
      sys.stdout.write('to %s (y/n)? ' % (pushurl and 'server: ' + pushurl or 'remote') )
      answer = sys.stdin.readline().strip()
      answer = answer in ('y', 'Y', 'yes', '1', 'true', 't')

    if answer:
      self._UploadAndReport(opt, [branch])
    else:
      _die("upload aborted by user")

  def _MultipleBranches(self, opt,remoebranch ,pending):
    projects = {}
    branches = {}

    script = []
    script.append('# Uncomment the branches to upload:')
    for project, avail in pending:
      script.append('#')
      script.append('# project %s/:' % project.relpath)

      b = {}
      for branch in avail:
        name = branch.name
        date = branch.date
        list = branch.commits
       # print >>sys.stdout, name
        

        if b:
          script.append('#')
        script.append('  branch %s (- commit%s, %s):' % (    ##########3
                      name,
                      len(list),
                      len(list) != 1 and 's' or '',
                      date))
        for commit in list:
          script.append('#         %s' % commit)
        b[name] = branch

      projects[project.relpath] = project
      branches[project.name] = b
    script.append('')

  #  script = Editor.EditString("\n".join(script)).split("\n")

    project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$')
    branch_re = re.compile(r'^\s*branch\s*([^\s(]+)\s*\(.*')

    project = None
    todo = []

    for line in script:
      m = project_re.match(line)
      if m:
        name = m.group(1)
        project = projects.get(name)
        if not project:
          _die('project %s not available for upload', name)
        continue

      m = branch_re.match(line)
      if m:
        name = m.group(1)
        if not project:
          _die('project for branch %s not in script', name)
        branch = branches[project.name].get(name)
        if not branch:
          _die('branch %s not in %s', name, project.relpath)
        todo.append(branch)
    if not todo:
      _die("nothing uncommented for upload")

    self._UploadAndReport(opt, remoebranch,todo)

  def _UploadAndReport(self, opt,remoebranch,todo):
    have_errors = False
    for branch in todo:
      try:
        # Check if there are local changes that may have been forgotten
        if branch.project.HasChanges():
            key = 'review.%s.autoupload' % branch.project.remote.review
            answer = branch.project.config.GetBoolean(key)

            # if they want to auto upload, let's not ask because it could be automated
            if answer is None:
                sys.stdout.write('Uncommitted changes in ' + branch.project.name + ' (did you forget to amend?). Continue uploading? (y/n) ')
                a = sys.stdin.readline().strip().lower()
                if a not in ('y', 'yes', 't', 'true', 'on'):
                    print >>sys.stderr, "skipping upload"
                    branch.uploaded = False
                    branch.error = 'User aborted'
                    continue

        branch.project.UploadNoReview(opt, remoebranch,branch=branch.name)
        branch.uploaded = True
      except UploadError, e:
        branch.error = e
        branch.uploaded = False
        have_errors = True
      except GitError, e:
        print >>sys.stderr, "Error: "+ str(e)
        sys.exit(1)


    print >>sys.stderr, ''
    print >>sys.stderr, '--------------------------------------------'

    if have_errors:
      for branch in todo:
        if not branch.uploaded:
          print >>sys.stderr, '[FAILED] %-15s %-15s  (%s)' % (
                 branch.project.relpath + '/', \
                 branch.name, \
                 branch.error)
      print >>sys.stderr, ''

    for branch in todo:
        if branch.uploaded:
          print >>sys.stderr, '[OK    ] %-15s %s' % (
                 branch.project.relpath + '/',
                 branch.name)

    if have_errors:
      sys.exit(1)

  def Execute(self, opt, args):
    
    if len(args)==0:
      print >>sys.stdout,"error:miss remotebranchname"
      print >>sys.stdout, "Usage:repo push <remotebranchname> [<project>...]"
      return
    project_list = self.GetProjects(args[1:])
    pending = []

    remoebranch=args[0]
    # if not create new branch, check whether branch has new commit.
    for project in project_list:
      if (project.GetUploadableBranch(project.CurrentBranch) is None):
        continue
      branch = project.GetBranch(project.CurrentBranch)
      
      rb = ReviewableBranch(project, branch, branch.LocalMerge)
      pending.append((project, [rb]))

    # run git push
    if not pending:
      print >>sys.stdout, "no branches ready for upload"
   # elif len(pending) == 1 and len(pending[0][1]) == 1:
     # self._SingleBranch(opt, pending[0][1][0])
    else:
      self._MultipleBranches(opt,remoebranch ,pending)




  在preject.py中添加如下方法:UploadNoReview(opt, remoebranch,branch=branch.name)

   def UploadNoReview(self, opt, remoebranch,branch=None):
    """If not review server defined, uploads the named branch directly to git server.
    """
    #print >>sys.stdout, branch
    now_branch=branch
    if branch is None:
      branch = self.CurrentBranch
    if branch is None:
      raise GitError('not currently on a branch')

    branch = self.GetBranch(branch)

    if not branch.LocalMerge:
      raise GitError('branch %s does not track a remote' % branch.name)

  

   # if opt.new_branch:
   #   dest_branch = branch.name
   # else:
    dest_branch = branch.merge

    if dest_branch.startswith(R_TAGS):
      raise GitError('Can not push to TAGS (%s)! Run repo push with --new flag to create new feature branch.' % dest_branch)
    if not dest_branch.startswith(R_HEADS):
      dest_branch = R_HEADS + dest_branch

    if not branch.remote.projectname:
      branch.remote.projectname = self.name
      branch.remote.Save()

    # save git config branch.name.merge
   ## if opt.new_branch:
    #  branch.merge = dest_branch
    #  branch.Save()

    ref_spec = '%s:%s' % (R_HEADS + branch.name, dest_branch)
    pushurl = self.manifest.manifestProject.config.GetString('repo.%s.pushurl'
              % branch.remote.name)
    if not pushurl:
      pushurl = self.manifest.manifestProject.config.GetString('repo.pushurl')
    if not pushurl:
      pushurl = branch.remote.name
    else:
      pushurl = pushurl.rstrip('/') + '/' + self.name
      remote = self.manifest.remotes.get(branch.remote.name)
      if remote and remote.autodotgit is not False:
        pushurl += ".git"

    cmd = ['push']
    #print >>sys.stdout, now_branch+'now'
    cmd.append(remoebranch)
    cmd.append(now_branch)
    print >>sys.stdout, "push"+" "+self.name+':'
    if GitCommand(self, cmd).Wait() != 0:
      raise UploadError('Upload failed')

    if branch.LocalMerge and branch.LocalMerge.startswith('refs/remotes'):
      self.bare_git.UpdateRef(branch.LocalMerge,
                              R_HEADS + branch.name)

本文转自:http://blog.csdn.net/winceos/article/details/18667605
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:201563次
    • 积分:2641
    • 等级:
    • 排名:第14634名
    • 原创:63篇
    • 转载:97篇
    • 译文:0篇
    • 评论:17条
    最新评论