Android 使用 Git 作为代码管理工具,开发了 Gerrit 进行代码审核以便更好的对代码进行集中式管理,还开发了 Repo 命令行工具,对 Git 部分命令封装,将百多个 Git 库有效的进行组织。
一个清单库可以包含多个清单文件和多个分支,每个清单文件和分支都有对应的版本。清单文件以xml格式组织的。举个例子:
$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代码。
备注:“*”表示新添加的指令
Usage:
repo init –u URL [OPTIONS]
Options:
命令repo init 要完成如下操作:
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。
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
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。
Usage:
repo checkout <branchname> [<project>…]
实际上是对git checkout 命令的封装,但不能带-b参数,所以不能用此命令来创建特性分支。
Example:
repo checkout crane-dev
repo checkout crane-dev platform/build platform/bionic
Usage:
repo branches [<project>…]
Example:
repo branches
repo branches platform/build platform/bionic
Usage:
repo diff [<project>…]
实际是对git diff 命令的封装,用于分别显示各个项目工作区下的文件差异。
Example:
repo diff ---查看所有项目
repo diff platform/build platform/bionic ---只查看其中两个项目
实际是对git add –interactive命令的封装、用于挑选各个项目工作区中的改动以加入暂存区。
Usage:
repo stage -i [<project>…]
-i代表git add –interactive命令中的–interactive,给出个界面供用户选择。
实际上是对git branch –d命令的封装,该命令用于扫面项目的各个分支,并删除已经合并的分支,用法如下:
repo prune [<project>…]
实际上是对git branch –D 命令的封装,用法如下:
repo abandon <branchname> [<project>…]
实际上是对git diff-index、git diff-filse命令的封装,同时显示暂存区的状态和本地文件修改的状态
$repo/repo status platform/bionic
repo使用
以上的实例输出显示了platform/bionic项目分支的修改状态
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
repo push org
这是新添加的指令,用于向服务器提交代码,使用方法:
repo push <remotename> [--all |<project>…]
repo会自己查询需要向服务器提交的项目并提示用户。
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指令括起来。
repo forall –c ‘echo $REPO_PROJECT’
repo forall –c ‘echo $REPO_PATH’
把所有项目多切换到master分支,执行以下指令将topic分支合并到master分支
repo forall –p –c git merge topic
在所有项目下打标签
repo forall –c git tag crane-stable-1.6
引用环境变量REPO_PROJECT添加远程仓库:
repo forall –c ‘git remote add korg sh://xiong@172.16.31/$REPO_PROJECT.git’
删除远程仓库:
repo forall –c git remote add korg
repo forall –c git branch crane-dev
repo forall –c git checkout –b crane-dev
相当于对git grep 的封装,用于在项目文件中进行内容查找
显示manifest文件内容
Usage:
repo manifest –o android.xml
显示repo的版本号
repo upload相当于git push,但是又有很大的不同。它不是将版本库改动推送到克隆时的远程服务器,而是推送到代码审核服务器(Gerrit软件架设)的特殊引用上,使用SSH协议。代码审核服务器会对推送的提交进行特殊处理,将新的提交显示为一个待审核的修改集,并进入代码审查流程,只有当审核通过后,才会合并到官方正式的版本库中。
因为全志没有代码审核服务器,所以这条指令用不到。
Usage:
repo upload [--re --cc] {[<project>]… | --replace <project>}
Options:
主要用于代码审核者下载和评估贡献者提交的修订
Usage:
repo download {project change [patchset]}…
用于repo自身的更新
参考:http://wenku.baidu.com/view/672c8faddd3383c4bb4cd257.html
在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 [...]
%prog rm [...]
--------------
"""
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 [...]"
print >>sys.stderr, " repo remote rm [...] "
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"
转载自:http://blog.sina.com.cn/s/blog_89f592f50100vpau.html