25.9.3 改进的repo无审核模式

前面介绍的使用repo forall迭代器实现在无审核服务器情况下向上游推送提交,只是权宜之计,尤其是用repo start建立工作分支要求和上游一致,实在是有点强人所难。

我改造了repo,增加了两个新的子命令repo config和repo push,让repo可以脱离Gerrit服务器直接向上游推送。代码托管在Github上:http://github.com/ossxp-com/repo。下面简单地介绍一下如何使用改造之后的repo。

1.下载改造后的repo引导脚本

建议使用改造后的repo引导脚本替换原脚本,否则在执行repo init命令时需要提供额外的—no-repo-verify参数、—repo-url和—repo-branch参数。


$curl-L-k http://github.com/ossxp-com/repo/raw/master/repo>~/bin/repo

$chmod a+x~/bin/repo


2.用repo从Github上检出测试项目

如果安装了改造后的repo引导脚本,使用下面的命令初始化repo及清单库。


$mkdir test

$cd test

$repo init-u git://github.com/ossxp-com/manifest.git

$repo sync


如果用的是标准的(未经改造的)repo引导脚本,使用下面的命令。


$mkdir test

$cd test

$repo init—repo-url=git://github.com/ossxp-com/repo.git\

—repo-branch=master—no-repo-verify\

-u git://github.com/ossxp-com/manifest.git

$repo sync


当子项目代码全部同步完成后,执行make命令。可以看到各个子项目的版本及清单库的版本。


$make

Version of test1:1:0.2-dev

Version of test2:2:0.2

Version of manifest:current


3.用repo config命令设置pushurl

现在如果进入到各个子项目目录,是无法成功执行git push命令的,因为上游Git库的地址是一个只读访问的URL,无法提供写服务。可以用新增的repo config命令设置执行git push时的URL地址。


$repo config repo.pushurl ssh://git@github.com/ossxp-com/


设置成功后,可以使用repo config repo.pushurl查看设置。


$repo config repo.pushurl

ssh://git@github.com/ossxp-com/


4.创建本地工作分支

使用下面的命令创建一个工作分支jiangxin。


$repo start jiangxin—all


使用repo branches命令可以查看当前所有的子项目都属于jiangxin分支。


$repo branches

*jiangxin|in all projects


参照下面的方法修改test/test1子项目。对test/test2项目也作类似修改。


$cd test/test1

$echo "1:0.2-jiangxin">version

$git diff

diff—git a/version b/version

index 37c65f8..a58ac04 100644

—-a/version

+++b/version

@@-1+1@@

-1:0.2-dev

+1:0.2-jiangxin

$repo status

on branch jiangxin

project test/test1/branch jiangxin

-m

version

$git add-u

$git commit-m "0.2-dev->0.2-jiangxin"


执行make命令,看看各个项目的改变。


$make

Version of test1:1:0.2-jiangxin

Version of test2:2:0.2-jiangxin

Version of manifest:current


5.PUSH到远程服务器

直接执行repo push就可以推送各个项目的改动。


$repo push


如果有多个项目同时进行了改动,为了避免出错,会弹出编辑器显示因为包含改动而需要推送的项目列表。


Uncomment the branches to upload:

#

project test/test1/:

branch jiangxin(1 commit,Mon Oct 25 18:04:51 2010+0800):

4f941239 0.2-dev->0.2-jiangxin

#

project test/test2/:

branch jiangxin(1 commit,Mon Oct 25 18:06:51 2010+0800):

86683ece 0.2-dev->0.2-jiangxin


每行前面的井号都是注释会被忽略。将希望推送的分支前的注释去掉,就可以将该项目的分支执行推送动作。下面的操作中把其中的两个分支的注释都去掉了,这两个项目当前分支的改动会push到上游服务器。


Uncomment the branches to upload:

#

project test/test1/:

branch jiangxin(1 commit,Mon Oct 25 18:04:51 2010+0800):

4f941239 0.2-dev->0.2-jiangxin

#

project test/test2/:

branch jiangxin(1 commit,Mon Oct 25 18:06:51 2010+0800):

86683ece 0.2-dev->0.2-jiangxin


保存退出(如果使用vi编辑器,输入<ESC>:wq执行保存退出)后,马上开始对选择的各个项目执行git push。


Counting objects:5,done.

Delta compression using up to 2 threads.

Compressing objects:100%(2/2),done.

Writing objects:100%(3/3),293 bytes,done.

Total 3(delta 0),reused 0(delta 0)

To ssh://git@github.com/ossxp-com/test1.git

27aee23..4f94123 jiangxin->master

Counting objects:5,done.

Writing objects:100%(3/3),261 bytes,done.

Total 3(delta 0),reused 0(delta 0)

To ssh://git@github.com/ossxp-com/test2.git

7f0841d..86683ec jiangxin->master


[OK]test/test1/jiangxin

[OK]test/test2/jiangxin


从推送的命令输出可以看出来,本地的工作分支jiangxin的改动被推送到远程服务器的master分支(本地工作分支跟踪的上游分支)上。

再次执行repo push,会显示没有项目需要推送。


$repo push

no branches ready for upload


6.在远程服务器创建新分支

如果想在服务器上创建一个新的分支,该如何操作呢?如下使用—new_branch参数调用repo push命令。


$repo start feature1—all

$repo push—new_branch


经过同样的编辑操作之后自动调用git push,在服务器上创建新分支feature1。


Total 0(delta 0),reused 0(delta 0)

To ssh://git@github.com/ossxp-com/test1.git

*[new branch]feature1->feature1

Total 0(delta 0),reused 0(delta 0)

To ssh://git@github.com/ossxp-com/test2.git

*[new branch]feature1->feature1


[OK]test/test1/feature1

[OK]test/test2/feature1


用git ls-remote命令查看远程版本库的分支,会发现远程版本库中已经建立了新的分支。


$git ls-remote git://github.com/ossxp-com/test1.git refs/heads/*

4f9412399bf8093e880068477203351829a6b1fb refs/heads/feature1

4f9412399bf8093e880068477203351829a6b1fb refs/heads/master

b2b246b99ca504f141299ecdbadb23faf6918973 refs/heads/test-0.1


注意到feature1和master分支引用指向了相同的SHA1哈希值,这是因为feature1分支是直接从master分支创建的。

7.通过不同的清单库版本,切换到不同分支

换用不同的清单库,需要建立新的工作区,并且在执行repo init时,通过-b参数指定清单库的分支。


$mkdir test-0.1

$cd test-0.1

$repo init-u git://github.com/ossxp-com/manifest.git-b test-0.1

$repo sync


当子项目代码全部同步完成后执行make命令。可以看到各个子项目的版本及清单库的版本不同于之前的输出。


$make

Version of test1:1:0.1.4

Version of test2:2:0.1.3-dev

Version of manifest:current-2-g12f9080


可以用repo manifest命令来查看清单库。


$repo manifest-o-

<?xml version="1.0" encoding="UTF-8"?>

<manifest>

<remote fetch="git://github.com/ossxp-com/"name="github"/>

<default remote="github" revision="refs/heads/test-0.1"/>

<project name="test1"path="test/test1">

<copyfile dest="Makefile"src="root.mk"/>

</project>

<project name="test2" path="test/test2"/>

</manifest>


仔细看看上面的清单文件,可以注意到默认的版本指向到了refs/heads/test-0.1引用所指向的分支test-0.1。

如果在子项目中修改、提交,然后使用repo push会将改动推送到远程版本库的test-0.1分支中。

8.切换到清单库里程碑版本

执行如下命令可以查看清单库包含的里程碑版本:


$git ls-remote—tags git://github.com/ossxp-com/manifest.git

43e5783a58b46e97270785aa967f09046734c6ab refs/tags/current

3a6a6da36840e716a14d52252e7b40e6ba6cbdea refs/tags/current^{}

4735d32613eb50a6c3472cc8087ebf79cc46e0c0 refs/tags/v0.1

fb1a1b7302a893092ce8b356e83170eee5863f43 refs/tags/v0.1^{}

b23884d9964660c8dd34b343151aaf968a744400 refs/tags/v0.1.1

9c4c287069e29d21502472acac34f28896d7b5cc refs/tags/v0.1.1^{}

127d9789cd4312ed279a7fa683c43eec73d2b28b refs/tags/v0.1.2

47aaa83866f6d910a118a9a19c2ac3a2a5819b3e refs/tags/v0.1.2^{}

af3abb7ed0a9ef7063e9d814510c527287c92ef6 refs/tags/v0.1.3

99c69bcfd7e2e7737cc62a7d95f39c6b9ffaf31a refs/tags/v0.1.3^{}


可以从任意里程碑版本的清单库初始化整个项目。


$mkdir v0.1.2

$cd v0.1.2

$repo init-u git://github.com/ossxp-com/manifest.git-b refs/tags/v0.1.2

$repo sync


当子项目代码全部同步完成后执行make命令。可以看到各个子项目的版本及清单库的版本不同于之前的输出。


$make

Version of test1:1:0.1.2

Version of test2:2:0.1.2

Version of manifest:v0.1.2