搭建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	(國內中科大源)

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/mainfest.git
git@lisongze-virtual-machine:~$ cd
git@lisongze-virtual-machine:~/exynos4412/platform/mainfest.git$ git init --bare
已初始化空的 Git 倉庫於 /home/git/exynos4412/platform/mainfest.git/

3.2.2 mainfest 提交default.xml

git@lisongze-virtual-machine:~/exynos4412$ git clone [email protected]:~/exynos4412/platform/mainfest.git
正克隆到 'mainfest'...
[email protected]'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/mainfest$ git add default.xml
git@lisongze-virtual-machine:~/exynos4412/mainfest$ git comit -m "add default.xml"
git@lisongze-virtual-machine:~/exynos4412/mainfest$ git push origin mster

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 [email protected]:/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 [email protected]:/home/git/exynos4412/platform/rootfs/rootfs.git master

//在exynos4412/uboot/uboot-2018.11下執行
$ git init
$ git add .
$ git commit -m "Init Code"
$ git push [email protected]:/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 [email protected]:/home/git/exynos4412/platform/userdata/UartTest.git master

4. repo拉取工程代碼

在客戶端上repo拉取服務器端代碼

repo 需要從git-repo源碼中拷貝過來,修改url並加上執行權限chmod 777 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 state

$ mkdir exynos4412_code
$ cd exynos4412_code
$ ./repo init -u [email protected]:/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
[email protected]'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 [email protected]:/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 [email protected]:/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
[email protected]'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 [email protected]:/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倉庫有強大的優勢的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章