建立repo服務器四(多個項目repo批量下載)

下載git-repo
官網是https://code.google.com/archive/p/git-repo/

``` git clone https://android.googlesource.com/tools/repo

git clone https://gerrit.googlesource.com/git-repo ```

無法訪問可以使用清華的mirror,https://mirror.tuna.tsinghua.edu.cn/help/git-repo/


  • 下載repo
客戶端運行
$git clone https://mirror.tuna.tsinghua.edu.cn/help/git-repo/

$ vim  git-repo.git/repo
REPO_URL = 'ssh://192.168.199.119:29418/git-repo'
REPO_REV = 'master'

$ cp -f ./git-repo.git/repo  /bin    //注意:repo的REPO_VER爲git-repo倉庫的分支

//git-repo建倉
ssh -p 29418 [email protected] gerrit create-project  git-repo
rm -rf .git
git init 
git add ./
git commit -m "git-repo"
git push ssh://[email protected]:29418/git-repo  HEAD:refs/heads/master

git clone ssh://[email protected]:29418/git-repo 
git tag -a -m "master" master
git push origin –tags

每次使用repo命令時都會自動下載git-repo,故修改REPO_URL = 'ssh://192.168.199.119:29418/git-repo'


  • 將repo拷貝到客戶端 ~/bin

在客戶端空倉庫原始代碼裏,運行腳本

#!/bin/bash

SERVER_IP="192.168.199.119"
SERVER_PORT="29418"
REMOTE_FETCH="ssh://${SERVER_IP}:${SERVER_PORT}/"
REMOTE_REVIEW="http://${SERVER_IP}"
PROJECT_NAME="testGit"
BRANCH="dev"
SRC_SOURCE=`pwd`

#根據目錄下的.git創建project.list
#刪除目錄下的.git&.gitignore
#在空目錄下創建.gitkeep,避免建倉時空目錄不能提交
function createProjectList(){
    find ./ -name ".git" > project.list
    sed -ri "s/^..(.*)..git$/\1/" project.list    
    find ./ -name ".git" | xargs rm -rf {}
    find ./ -name ".gitignore" | xargs rm -rf {}
    touch .gitkeep
    find ./ -type d -empty | xargs -i cp ./.gitkeep {}
    rm -f ./.gitkeep

}

#創建manifests.xml清單文件
function createManifestsXml(){

    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > manifest.xml
    echo "<manifest>" >> manifest.xml
    echo "  <remote fetch=\"${REMOTE_FETCH}\" name=\"origin\" review=\"${REMOTE_REVIEW}\"/>" >> manifest.xml
    echo "  <default remote=\"origin\" revision=\"${BRANCH}\" sync-j=\"4\" sync-c=\"true\"/>" >> manifest.xml
    echo "" >> manifest.xml
  
    GIT_PROJECTS=`cat project.list | xargs`

    for git_path in ${GIT_PROJECTS}  
    do
        if [ ${git_path} == "build" ]
        then
            echo "  <project name=\"${PROJECT_NAME}/${git_path}\" path=\"${git_path}\" >" >> manifest.xml
                echo "      <copyfile dest=\"Makefile\" src=\"core/root.mk\"/>" >> manifest.xml
                echo "  </project>" >> manifest.xml
        elif [ ${git_path} == "vendor/intel/support" ]
        then
            echo "  <project name=\"${PROJECT_NAME}/${git_path}\" path=\"${git_path}\" >" >> manifest.xml
                echo "      <copyfile dest=\"device/intel/Android.mk\" src=\"x86_only_Android.mk\"/>" >> manifest.xml
            echo "      <copyfile dest=\"platform/vendor/intel/Android.mk\" src=\"x86_only_Android.mk\"/>" >> manifest.xml
            echo "  </project>" >> manifest.xml
        else
            echo "  <project path=\"${git_path}\" name=\"${git_path}\" />" >> manifest.xml
        fi
    done
    echo "" >> manifest.xml
    echo "</manifest>" >> manifest.xml
}

#遠程建倉並提交SRC
function createRepo() {
    GIT_PROJECTS=`cat project.list | xargs`
    for repoName in ${GIT_PROJECTS}
    do
        #批量建倉
        echo "Init Empty Repo:${PROJECT_NAME}/${repoName}"
        ssh -p $SERVER_PORT git@$SERVER_IP gerrit create-project -n ${PROJECT_NAME}/${repoName}
    done

    while read git_name
    do
        if [ -d ${SRC_SOURCE}/${git_name} ]
        then
            echo "${SRC_SOURCE}/${git_name}"
            cd ${SRC_SOURCE}/${git_name}
            git init
            git add ./
            git commit -m "Init Repo"
            git push ssh://git@${SERVER_IP}:${SERVER_PORT}/${PROJECT_NAME}/${git_name}  HEAD:refs/heads/master
        fi
    done < project.list
}

createProjectList
createManifestsXml
createRepo

以上腳本爲僅供參考腳本,有些。實際使用時需做適當修改

實踐中的2種方法:

方法一、先建立本地git倉庫:

1、創建git_create.sh,初始化空倉庫,以下腳本測試過OK,可以優化爲簡單的for循環腳本,執行此腳本後源碼將會建立很多個git倉庫,可以自己定義,再細分,下載鏈接 ://download.csdn.net/download/xiangzi10/12053871

2、執行以下2條命令後將生成project.list文件,後面將會以此文件創建gerrit的倉庫

$find ./ -name ".git" > project.list
$sed -ri "s/^..(.*)..git$/\1/" project.list

腳本執行完,會在gerrit服務器新建多個倉庫

#cat project.list 
ndk
device
hardware
build
bootable
platform_testing
developers
tools
bionic
libnativehelper
libcore
abi
development
vendor/mstar/mif
vendor/mstar/pm
vendor/mstar/fpp
vendor/mstar/mboot/MBoot
vendor/mstar/kernel/linaro
vendor/mstar/rt-pm
vendor/mstar/supernova
system
external
docs
frameworks
pdk
art
cts
sdk
prebuilts
packages
dalvik

3、創建create_manifest.sh 腳本,下載鏈接如下://download.csdn.net/download/xiangzi10/12053682

執行此腳本後會自動創建manifest.xml文件,此文件以後將添加到manifest倉庫作爲repo下載的依據

4、創建create_GerritProject.sh,下載鏈接如下://download.csdn.net/download/xiangzi10/12053696

執行此腳本可以使用ssh -p $SERVER_PORT $gerrit_admin@$SERVER_IP gerrit create-project --empty-commit批量創建倉庫

然後批量推送本地代碼到gerrit倉庫

至此gerrit倉庫已完成。

方法二、先建立目錄最後建立git倉庫並上傳:

使用一體化腳本


  • 創建gerrit manifest倉庫

新建名爲manifest倉庫,下載到客戶端,創建新分支,以後以分支名來區別不同的repo,添加剛纔生成的manifest.xml

#git checkout -b MSD6A358

#git branch -a
* MSD6A358
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

#git status
#git add -A

#git commit -a -m "xxx"

#git push --set-upstream origin MSD6A358                建立遠程分支

#git branch -a
* MSD6A358
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/MSD6A358
  remotes/origin/master


  • repo pull 遠程代碼:注意:參數--no-repo-verify爲不驗證簽名
#repo init -u ssh://[email protected]:29418/manifests -b MSD6A358 -m manifest.xml
Get ssh://192.168.0.198:29418/git-repo
remote: Counting objects: 4496, done
remote: Finding sources: 100% (4496/4496)
remote: Total 4496 (delta 2972), reused 4496 (delta 2972)
Receiving objects: 100% (4496/4496), 1.52 MiB | 10.87 MiB/s, done.
Resolving deltas: 100% (2972/2972), done.
From ssh://192.168.0.198:29418/git-repo
 * [new branch]      master     -> origin/master

fatal: branch 'master' has not been signed
fatal: cloning the git-repo repository failed, will remove '.repo/repo' 

添加參數--no-repo-verify爲不驗證簽名

#repo init -u ssh://[email protected]:29418/manifests -b MSD6A358 -m manifest.xml --no-repo-verify
Get ssh://192.168.0.198:29418/git-repo
remote: Counting objects: 4496, done
remote: Finding sources: 100% (4496/4496)
remote: Total 4496 (delta 2972), reused 4496 (delta 2972)
Receiving objects: 100% (4496/4496), 1.52 MiB | 11.10 MiB/s, done.
Resolving deltas: 100% (2972/2972), done.
From ssh://192.168.0.198:29418/git-repo
 * [new branch]      master     -> origin/master
Get ssh://[email protected]:29418/manifests
fatal: No names found, cannot describe anything.
remote: Counting objects: 6, done        
remote: Finding sources: 100% (6/6)           
remote: Total 6 (delta 0), reused 4 (delta 0)        
From ssh://192.168.0.198:29418/manifests
 * [new branch]      MSD6A358 -> origin/MSD6A358
 * [new branch]      master        -> origin/master

Your identity is: xxx <[email protected]>
If you want to change this, please re-run 'repo init' with --config-name

repo has been initialized in /home/htp828/sda1_2tb/clone/repo_358
#ls -ll .repo/
total 12
drwxrwxr-x  3 htp828 htp828 4096 Dec 25 10:59 manifests
drwxrwxr-x 10 htp828 htp828 4096 Dec 25 10:59 manifests.git
lrwxrwxrwx  1 htp828 htp828   22 Dec 25 10:59 manifest.xml -> manifests/manifest.xml
drwxrwxr-x  7 htp828 htp828 4096 Dec 25 10:59 repo

成功

#repo sync 成功下載代碼

#repo start devo --all
給所有倉庫弄一個本地分支

git add
git commit -m "xxx"
git pull --rebase
repo upload .----失敗
git  push origin HEAD:refs/for/master

repo forall -c 就是對所有倉庫遞歸,當然沒有必要提交的要先清掉
repo forall -c git add . 這樣是可以遞歸的
repo forall -c git commit -m "xxx"
repo forall -c git pull --rebase
repo forall -c git push origin HEAD:refs/for/master


問題點:

Q1:沒有註冊郵箱,group member不能識別

#git  push origin HEAD:refs/for/master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 319 bytes | 319.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2)
remote: Processing changes: refs: 1, done    
remote: ERROR: commit dba6a7c: email address [email protected] is not registered in your account, and you lack 'forge committer' permission.
remote: You have not registered any email addresses.
remote: To register an email address, visit:
remote: http://192.168.0.198:8081/settings#EmailAddresses
remote: 
remote: 
To ssh://192.168.0.198:29418/MSD6A358/build
 ! [remote rejected] HEAD -> refs/for/master (commit dba6a7c: invalid committer)
error: failed to push some refs to 'ssh://192.168.0.198:29418/MSD6A358/build'

原因:repo init時使用了git用戶,但是push使用的不是這個用戶,導致出錯。

#git  push ssh://[email protected]:29418/MSD6A358/build HEAD:refs/for/master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 319 bytes | 319.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2)
remote: Processing changes: refs: 1, new: 1, done    
remote: 
remote: SUCCESS
remote: 
remote:   http://192.168.0.198:8081/c/MSD6A358/build/+/63 test [NEW]
remote: 
To ssh://192.168.0.198:29418/MSD6A358/build
 * [new branch]      HEAD -> refs/for/master

故repo init時要注意不用帶用戶@,repo init -u ssh://git@192.168.0.198:29418/manifests -b MSD6A358 -m manifest.xml --no-repo-verify

解決辦法:gerrit添加權限

Q2:#repo init -u ssh://[email protected]:29418/repo/MSD6A648/manifests.git --no-repo-verify
Get ssh://192.168.0.198:29418/git-repo
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: cloning the git-repo repository failed, will remove '.repo/repo' 

錯誤:

cat ~/.ssh/id_rsa.pub

將公鑰加入到gerrit服務器上

#touch ~/.ssh/config

#vi ~/.ssh/config

Host 192.168.0.198
HostName 192.168.0.198
User 201
IdentityFile ~/.ssh/id_rsa

#rm -rf ~/.repoconfig

#rm -rf .repo

#repo init -u ssh://192.168.0.198:29418/repo/MSD6A648/manifests.git --no-repo-verify
Get ssh://192.168.0.198:29418/git-repo
remote: Counting objects: 4496, done
remote: Finding sources: 100% (4496/4496)
remote: Total 4496 (delta 2972), reused 4496 (delta 2972)
Receiving objects: 100% (4496/4496), 1.52 MiB | 10.64 MiB/s, done.
Resolving deltas: 100% (2972/2972), done.
From ssh://192.168.0.198:29418/git-repo
 * [new branch]      master     -> origin/master
Get ssh://192.168.0.198:29418/repo/MSD6A648/manifests.git
fatal: No names found, cannot describe anything.
fatal: project repo/MSD6A648/manifests not found
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: project repo/MSD6A648/manifests not found
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: cannot obtain manifest ssh://192.168.0.198:29418/repo/MSD6A648/manifests.git

git clone "ssh://[email protected]:29418/repo/MSD6A648/manifest"

單獨獲取是可以的

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