搭建repo服务器管理多个git工程

搭建repo服务器管理多个git工程

原文链接: http://www.lisongze.com/2018/11/02/repo/ or http://www.lisongze.cn/2018/11/02/repo/

1.repo介绍

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

鉴于repo能够管理多个git库,针对一个项目需要多个git库分开管理使用repo就非常方便。如嵌入式项目一般由uboot、kernel、文件系统rootfs、用户程序等组成。这里就以这样的项目组织来搭建repo服务器。

├── kernel
│   └── linux-3.5.y
├── rootfs
│   └── rootfs
├── uboot
│   └── uboot-2018.11
└── userdata
    └── UartTest

服务器:192.168.3.5
账户:git

2.下载repo

git-repo下载可在服务器端通过以下任一方式下载。

git clone https://gerrit.googlesource.com/git-repo		(谷歌官方源)
git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo	(国内清华源)
git clone https://gerrit-googlesource.lug.ustc.edu.cn/git-repo	(国内中科大源)

在服务端git用户下

$ mkdir -p tools/git-repo
$ cd tools/git-repo
$ pwd
/home/git/tools/git-repo
$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -b stable

3.初始化工程和mainifest git仓库

3.1 初始化项目代码仓库 git server端

git@lisongze-virtual-machine:~$ mkdir exynos4412
git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/kernel/linux-3.5.y.git
git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/uboot/uboot-2018.11.git
git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/rootfs/rootfs.git
git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/userdata/UartTest.git

git@lisongze-virtual-machine:~$ cd exynos4412/platform/kernel/linux-3.5.y.git
git@lisongze-virtual-machine:~/exynos4412/platform/kernel/linux-3.5.y.git$ git init --bare
已初始化空的 Git 仓库于 /home/git/exynos4412/platform/kernel/linux-3.5.y.git/
git@lisongze-virtual-machine:~/exynos4412/platform/uboot/uboot-2018.11.git$ cd

git@lisongze-virtual-machine:~$ cd exynos4412/platform/uboot/uboot-2018.11.git
git@lisongze-virtual-machine:~/exynos4412/platform/uboot/uboot-2018.11.git$ git init --bare
已初始化空的 Git 仓库于 /home/git/exynos4412/platform/uboot/uboot-2018.11.git/
git@lisongze-virtual-machine:~/exynos4412/platform/uboot/uboot-2018.11.git$ cd

git@lisongze-virtual-machine:~$ cd exynos4412/platform/rootfs/rootfs.git
git@lisongze-virtual-machine:~/exynos4412/platform/rootfs/rootfs.git$ git init --bare
已初始化空的 Git 仓库于 /home/git/exynos4412/platform/rootfs/rootfs.git/
git@lisongze-virtual-machine:~/exynos4412/platform/rootfs/rootfs.git$ cd

git@lisongze-virtual-machine:~$ cd exynos4412/platform/userdata/UartTest.git
git@lisongze-virtual-machine:~/exynos4412/platform/userdata/UartTest.git$ git init --bare
已初始化空的 Git 仓库于 /home/git/exynos4412/platform/userdata/UartTest.git/
git@lisongze-virtual-machine:~/exynos4412/platform/userdata/UartTest.git$ cd

3.2 初始化manifest仓库 git server端

3.2.1 初始化 manifest git server端

git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/manifest.git
git@lisongze-virtual-machine:~$ cd
git@lisongze-virtual-machine:~/exynos4412/platform/manifest.git$ git init --bare
已初始化空的 Git 仓库于 /home/git/exynos4412/platform/manifest.git/

3.2.2 mainfest 提交default.xml

git@lisongze-virtual-machine:~/exynos4412$ git clone git@192.168.3.5:~/exynos4412/platform/mainfest.git
正克隆到 'mainfest'...
git@192.168.3.5's password:
warning: 您似乎克隆了一个空仓库。

default.xml

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <remote  name="linux"
           fetch="ssh://192.168.3.5/home/git/exynos4412" />
  <default revision="master"
           remote="linux"
           sync-j="1" />

  <project path="kernel/linux-3.5.y" name="platform/kernel/linux-3.5.y" />
  <project path="rootfs/rootfs" name="platform/rootfs/rootfs" />
  <project path="uboot/uboot-2018.11" name="platform/uboot/uboot-2018.11" />
  <project path="userdata/UartTest" name="platform/userdata/UartTest" />

</manifest>

remote name:远程仓库名
revision:分支名

git@lisongze-virtual-machine:~/exynos4412/manifest$ git add default.xml
git@lisongze-virtual-machine:~/exynos4412/manifest$ git comit -m "add default.xml"
git@lisongze-virtual-machine:~/exynos4412/manifest$ git push origin master

3.3 客户端git提交初始代码

在客户端 192.168.3.xxx源码工程下提交初始代码推送到远程服务器下对应的git仓库。

//在exynos4412/kernel/linux-3.5.y下执行
$ git init
$ git add .
$ git commit -m "Init Code"
$ git push git@192.168.3.5:/home/git/exynos4412/platform/kernel/linux-3.5.y.git master

//在exynos4412/rootfs/rootfs下执行
$ git init
$ git add .
$ git commit -m "Init Code"
$ git push git@192.168.3.5:/home/git/exynos4412/platform/rootfs/rootfs.git master

//在exynos4412/uboot/uboot-2018.11下执行
$ git init
$ git add .
$ git commit -m "Init Code"
$ git push git@192.168.3.5:/home/git/exynos4412/platform/uboot/uboot-2018.11.git master

//在exynos4412/exynos4412/userdata/UartTest下执行
$ git init
$ git add .
$ git commit -m "Init Code"
$ git push git@192.168.3.5:/home/git/exynos4412/platform/userdata/UartTest.git master

4. repo拉取工程代码

在客户端上repo拉取服务器端代码

repo 需要从git-repo源码中拷贝过来,修改url并加上执行权限chmod 777 repo。

  • 将REPO_URL = ‘https://gerrit.googlesource.com/git-repo’ 修改为 REPO_URL = ‘ssh://192.168.3.5/home/git/tools/git-repo’

如有fatal: branch ‘stable’ has not been signed报错,可将REPO_REV = ‘stable’ 改为 REPO_REV = ‘master’,或着可在git-repo源码中切换到stable分支,git check -b state remote/origin/stable,也可重新获取state分支 git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -b stable

在客户端用户下拉取代码

$ mkdir exynos4412_code
$ cd exynos4412_code
$ ./repo init -u git@192.168.3.5:/home/git/exynos4412/platform/manifest.git
$ ./repo sync

5. repo创建分支

在项目中我们经常会基于某分支新建分支,来开发新的项目。这里我们演示基于master分支创建tiny4412_evb新分支。

5.1 客户端创建分支并推送远程仓库

./repo forall -pv -c "git checkout remotes/m/master -B tiny4412_evb"
./repo forall -pv -c "git push linux tiny4412_evb"

推送远程仓库

git push [remote-name] [branch-name]

  • remote-name:远程仓库名(一般默认远程分支名称为origin)
  • branch-name: 分支名称
    这里远程分支为linux,是由于在default.xml中定义的。

其中会报两个错误,是由于没有权限导致,如下
error: unpack failed: unable to create temporary object directory
remote: error: cannot lock ref ‘refs/heads/tiny4412_evb’: 不能创建 '/home/git/exynos4412/platform/
解决方法比较暴力,到对应的git服务器端chmod 777 -R object 和 chmod 777 -R refs

5.2 修改远端服务端manifest

git@lisongze-virtual-machine:~/exynos4412/manifest$ git branch -a
* master
  remotes/origin/master
git@lisongze-virtual-machine:~/exynos4412/manifest$ git checkout -b tiny4412_evb remotes/origin/master
分支 'tiny4412_evb' 设置为跟踪来自 'origin' 的远程分支 'master'。
切换到一个新分支 'tiny4412_evb'
git@lisongze-virtual-machine:~/exynos4412/manifest$ vim default.xml
git@lisongze-virtual-machine:~/exynos4412/manifest$ git diff
diff --git a/default.xml b/default.xml
index d1aeaa7..d790a73 100644
--- a/default.xml
+++ b/default.xml
@@ -2,7 +2,7 @@
 <manifest>
   <remote  name="linux"
            fetch="ssh://192.168.3.5/home/git/exynos4412" />
-  <default revision="master"
+  <default revision="tiny4412_evb"
            remote="linux"
            sync-j="1" />

git@lisongze-virtual-machine:~/exynos4412/manifest$ git add default.xml
git@lisongze-virtual-machine:~/exynos4412/manifest$ git commit -m "add new branch tiny4412_evb"
[tiny4412_evb 25d1cf6] add new branch tiny4412_evb
 1 file changed, 1 insertion(+), 1 deletion(-)
git@lisongze-virtual-machine:~/exynos4412/manifest$ git push origin tiny4412_evb
git@192.168.3.5's password:
对象计数中: 3, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 293 bytes | 293.00 KiB/s, 完成.
Total 3 (delta 1), reused 0 (delta 0)
To 192.168.3.5:~/exynos4412/platform/manifest.git
 * [new branch]      tiny4412_evb -> tiny4412_evb

5.3 拉取新建分支代码

mkdir tiny4412_evb
cd tiny4412_evb
./repo init -u git@192.168.3.5:/home/git/exynos4412/platform/manifest.git -b tiny4412_evb
./repo sync

6. 增加子工程git管理

经常我们需要增加一个单独的子工程需要用git管理,就需要修改远端服务器。

6.1 服务器侧操作—创建目录初始化git server

git@lisongze-virtual-machine:~$ mkdir -p exynos4412/platform/rootfs/tslib.git
git@lisongze-virtual-machine:~$ cd exynos4412/platform/rootfs/tslib.git
git@lisongze-virtual-machine:~$ git init --bare

6.2 客户端操作-提交代码

这里我示例增加rootfs/tslib源码,在tiny4412_evb分支上。
在tiny4412_evb目录下操作,拷贝tslib源码到rootfs下

./repo forall -pv -c "git checkout -b tiny4412_evb remotes/linux/tiny4412_evb"
cd rootfs/tslib/
git init
git add .
git commit -m "add tslib code"
git branch tiny4412_evb
git checkout tiny4412_evb
git push git@192.168.3.5:/home/git/exynos4412/platform/rootfs/tslib.git tiny4412_evb

6.3 服务器侧操作-修改manifest

git@lisongze-virtual-machine:git checkout -B tiny4412_evb remotes/origin/tiny4412_evb
git@lisongze-virtual-machine:~/exynos4412/manifest$ git branch
  master
* tiny4412_evb
git@lisongze-virtual-machine:~/exynos4412/manifest$ git diff
diff --git a/default.xml b/default.xml
index d790a73..9cb1d41 100644
--- a/default.xml
+++ b/default.xml
@@ -8,6 +8,7 @@

   <project path="kernel/linux-3.5.y" name="platform/kernel/linux-3.5.y" />
   <project path="rootfs/rootfs" name="platform/rootfs/rootfs" />
+  <project path="rootfs/tslib" name="platform/rootfs/tslib" />
   <project path="uboot/uboot-2018.11" name="platform/uboot/uboot-2018.11" />
   <project path="userdata/UartTest" name="platform/userdata/UartTest" />

git@lisongze-virtual-machine:~/exynos4412/manifest$ git add default.xml
git@lisongze-virtual-machine:~/exynos4412/manifest$ git status
位于分支 tiny4412_evb
您的分支与上游分支 'origin/tiny4412_evb' 一致。

要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

        修改:     default.xml

git@lisongze-virtual-machine:~/exynos4412/manifest$ git commit -m "add tslib"
[tiny4412_evb 740d86f] add tslib
 1 file changed, 1 insertion(+)
git@lisongze-virtual-machine:~/exynos4412/manifest$ git push origin tiny4412_evb
git@192.168.3.5's password:
对象计数中: 3, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 307 bytes | 307.00 KiB/s, 完成.
Total 3 (delta 1), reused 0 (delta 0)
To 192.168.3.5:~/exynos4412/platform/manifest.git
   25d1cf6..740d86f  tiny4412_evb -> tiny4412_evb

提交完验证

./repo init -u git@192.168.3.5:/home/git/exynos4412/platform/manifest.git -b tiny4412_evb
./repo sync

7. 给整个工程打tag标签

git打tag命令:

git tag –a TAG_NAME -m "add tag TAG_NAME"

我们在此版本上打上标签为TINY4412_V1.0.0,命令如下

./repo sync
./repo forall -pv -c "git tag -a TINY4412_V1.0.0 -m "add tag TINY4412_V1.0.0""
./repo forall -pv -c "git push linux TINY4412_V1.0.0"

总结

通过上面方法创建repo服务器,可以看出简单的项目工程还是可以手动创建,但较复杂的工程,如android还是比较麻烦,最好的方式就是通过sh脚本来实现。但原理是相通的,repo 管理一个项目有多个git仓库有强大的优势的。

优化

上述方案中,git push 解决方法比较暴力,到对应的git服务器端chmod 777 -R object 和 chmod 777 -R refs。此次可以使用ssh 公钥来解决。

manifest调整

default.xml
主要修改 fetch=“ssh://git@192.168.3.5/home/git/exynos4412” ,然后git提交上去即可

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <remote  name="linux"
           fetch="ssh://git@192.168.3.5/home/git/exynos4412" />
  <default revision="master"
           remote="linux"
           sync-j="1" />

  <project path="kernel/linux-3.5.y" name="platform/kernel/linux-3.5.y" />
  <project path="rootfs/rootfs" name="platform/rootfs/rootfs" />
  <project path="uboot/uboot-2018.11" name="platform/uboot/uboot-2018.11" />
  <project path="userdata/UartTest" name="platform/userdata/UartTest" />

</manifest>

拉取代码 带上git@xxx

./repo init -u git@192.168.3.5:/home/git/exynos4412/platform/manifest.git  -b master

公钥添加方法

在服务端git用户下

$ ssh-keygen
$ cd .ssh
$ touch authorized_keys
$ vim authorized_keys //添加客户端其他用户公钥,保存退出

客户端用户生成公钥

$ ssh-keygen
$ cd .ssh
$ 将id_rsa.pub公钥内容,给管理员,添加到git用户下的.ssh/authorized_keys中即可
$ 验证公钥是否成功
$ ssh -T git@192.168.3.5 提示不用输入密码,即添加公钥成功
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值