Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自建 repo 伺服器極簡入門:從安裝到專案同步的完整流程

編輯:關於 Android 開發

這篇文章整理成一份學習筆記,同時也希望對同樣需要自建 repo 伺服器的朋友有幫助。感謝以下幾篇文章提供的參考:

https://blog.csdn.net/hansel/article/details/9798189
https://blog.csdn.net/lb5761311/article/details/47723455
https://bbs.mfunz.com/thread-1015705-1-1.html
https://blog.csdn.net/sunweizhong1024/article/details/8055372
https://blog.csdn.net/itleaks/article/details/30021395

1. 下載 repo bootstrap

repo 的 bootstrap 就是安裝檔,用來取得完整版的 repo:

  1. git clone git://git.omapzoom.org/git-repo.git

clone 完成後,檔案位於 ./git-repo/repo。

2. 安裝 repo bootstrap

a. 建立本地 bin 目錄並把 repo 複製進去:

  1. mkdir ~/bin/
  2. cp ./git-repo/repo ~/bin/

b. 編輯 ~/.profile,確認包含以下三行:

  1. if [ -d "$HOME/bin" ] ; then
  2. PATH="$HOME/bin:$PATH"
  3. fi

c. 讓設定生效:

  1. source ~/.profile

之後就能在命令列直接下達 repo 指令。

3. 修改 repo bootstrap 的下載來源

編輯 ~/bin/repo,把預設的 REPO_URL 換掉:

  1. -- REPO_URL = 'https://gerrit.googlesource.com/git-repo'
  2. ++ REPO_URL = 'git://codeaurora.org/tools/repo.git'

這一步不能省,否則下一步會出現下載逾時:

  1. fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
  2. fatal: error [Errno 101] Network is unreachable

原因很單純:gerrit.googlesource.com 在該網路環境下無法連線。

4. 建立 manifest 清單

repo 是依據 manifest 中的設定來同步專案的。

a. 在伺服器上建立 manifest.git 空倉庫,本例的 remote url 為 git:manifest:

  1. cd manifest.git
  2. git remote -v
  3. origin git:manifest (fetch)
  4. origin git:manifest (push)

b. 在客戶端 clone 這個倉庫:

  1. git clone git:manifest

c. 建立 default.xml,repo 預設以此檔作為設定依據:

  1. cd manifest
  2. vi default.xml

內容如下:



  1. fetch="git:"
  2. review="https://android-review.googlesource.com/" />
  3. remote="origin"
  4. sync-j="4" />



這份設定分為三個區塊:

remote:代表一個同步來源的伺服器位址,可以有複數個。name 是唯一識別名稱;fetch 是 url 前綴,本例倉庫路徑一律是 git:XXX,所以前綴就是 git:;review 暫可忽略,照抄範例即可。

default:代表同步時使用的預設值。revision 是 git 分支名稱;remote 指定要用的 remote;sync-j 是同步時的平行作業數。

project:代表要同步的 git 倉庫,本例假設有一個 git:test。name 是倉庫名;path 決定它在 repo 工作目錄下的落點,這裡設為子目錄 test。

d. 把修改推回伺服器:

  1. git add default.xml
  2. git commit -m "udpate default.xml for repo sync test"
  3. git push origin master

5. 安裝完整版 repo

a. 建立並進入工作目錄:

  1. mkdir test-project
  2. cd test-project

b. 初始化 repo 並指定 manifest 來源:

  1. repo init -u git:manifest

c. 這一步才真正在 clone 完整版的 repo,log 大致如下:

  1. Get git://codeaurora.org/tools/repo.git
  2. remote: Counting objects: 3425, done.
  3. remote: Total 3425 (delta 0), reused 0 (delta 0)
  4. Receiving objects: 100% (3425/3425), 753.71 KiB, done.
  5. Resolving deltas: 100% (2311/2311), done.
  6. From git://codeaurora.org/tools/repo
  7. * [new branch] aosp-new/maint -> origin/aosp-new/maint
  8. * [new branch] aosp-new/master -> origin/aosp-new/master
  9. * [new branch] aosp-new/stable -> origin/aosp-new/stable
  10. * [new branch] caf-stable -> origin/caf-stable
  11. * [new branch] master -> origin/master
  12. * [new branch] stable -> origin/stable
  13. * [new tag] v1.0 -> v1.0
  14. * [new tag] v1.0.1 -> v1.0.1
  15. ......
  16. Getting manifest ...
  17. from git:manifest
  18. remote: Counting objects: 18, done.
  19. remote: Compressing objects: 100% (16/16), done.
  20. remote: Total 18 (delta 4), reused 0 (delta 0)
  21. Unpacking objects: 100% (18/18), done.
  22. From git:manifest
  23. * [new branch] master -> origin/master

d. 接著會要求輸入身分資訊,中括號內是 git 的既有設定,冒號後輸入相同內容即可:

  1. Your Name [willqian]: willqian
  2. Your Email [690004467@qq.com]: 690004467@qq.com
  3. Your identity is: willqian <690004467@qq.com>
  4. is this correct [y/n]? y

e. 完成後,目錄下會多出一個隱藏資料夾 .repo,完整版 repo 位於 .repo/repo/。

6. 同步專案

a. 執行同步:

  1. cd test-project
  2. repo sync

完成後 manifest 中列出的 test 倉庫就被 clone 下來了。不過此時進入倉庫查看,會發現處於 no branch 狀態:

  1. cd test
  2. git status
  3. # Not currently on any branch.
  4. nothing to commit (working directory clean)

b. 切到 master 分支:

  1. cd test-project
  2. repo start master --all

再進到 test 倉庫確認:

  1. cd test
  2. git status
  3. # On branch master
  4. nothing to commit (working directory clean)

至此,repo 的完整安裝與專案同步流程就走完了。

實務上的幾點提醒

repo start <branch> --all 會對所有專案建立同名分支,在只有單一專案的測試環境很安全;但在真實的多倉庫專案中,--all 可能一次動到幾十個倉庫,建議改用 -b 搭配 -t 針對特定專案操作。

另外,manifest 的任何異動都要記得 commit 並 push,否則其他人 repo sync 時拿到的仍是舊版清單,這也是自建 repo 環境最常見的困惑來源。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved