搭建Repo服务器

1 安装repo

参考:清华大学开源软件镜像站:Git Repo 镜像使用帮助

在这里插入图片描述

2 创建manifest仓库

2.1 创建仓库

git init --bare manifest.git

在这里插入图片描述

2.2 创建default.xml文件

default.xml文件内容:

<?xml version="1.0" encoding="UTF-8" ?>
<manifest>
    <remote
        name="origin"
        fetch="ssh://linux@192.168.1.53:/tmp/codes"
    />
    <default
        remote="origin"
        revision="master"
        sync-j="4"
    />
    <project path="module1" name="module1.git"/>
    <project path="module2" name="module2.git"/>
</manifest>

在这里插入图片描述
提交并推送default.xml文件:
在这里插入图片描述

3 创建工程仓库

在文件中有两个示例仓库module1.git和module2.git,需要创建:

git init --bare module1.git
git init --bare module2.git

在这里插入图片描述

3.1 空仓库提交一次(可忽略)

在这里插入图片描述
在这里插入图片描述

4 Repo使用

4.1 repo初始化

repo init -u ssh://linux@192.168.1.53:/tmp/codes/manifest.git

在这里插入图片描述

4.2 同步代码到本地

repo sync

在这里插入图片描述

4.3 推送代码到服务器

注意:此时说的是推送代码到服务器,修改代码及提交代码还是使用git工具(git add/commit)。

repo提供了upload命令可以提交代码,但是不是将代码直接提交到git仓,而是提交到Gerrit评审工具,且需要在xml文件中配置,本文没有配置xml,也没有搭建Gerrit评审工具服务,那么就无法使用repo upload命令上传代码,直接使用会报找不到review路径:
在这里插入图片描述
但是可以使用repo forall命令:

repo forall -pv -c "git push origin master"

repo forall命令将‘-c’后边的指令在每一个git仓下去执行一次,可以达到将代码直接推送到服务器的目的。

5 搭建本地repo源码服务器

在repo初始化时,需要联网下载依赖脚本,4.1节使用的是清华大学开源镜像export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'

地址,本节介绍本地搭建repo源码服务器。

4.1节初始化后,就已经下载了repo源码,其路径是.repo/repo

5.1 创建git-repo仓库

git init --bare git-repo.git

在这里插入图片描述

5.2 添加仓库地址

进入repo源码目录,添加git-repo仓库地址

git remote add local_vm ssh://linux@192.168.1.53:/tmp/codes/git-repo.git

在这里插入图片描述

5.3 推送代码到git-repo仓库

在这里插入图片描述
注意:必须上传stable分支
在这里插入图片描述
在这里插入图片描述

5.4 使用本地repo源码服务器初始化repo

5.4.1 方法1:使用‘–repo-url=’选选项

repo init -u ssh://linux@192.168.1.53:/tmp/codes/manifest.git --repo-url=ssh://linux@192.168.1.53:/tmp/codes/git-repo.git

在这里插入图片描述

5.4.2 方法2:使用’REPO_URL’环境变量

export REPO_URL='ssh://linux@192.168.1.53:/tmp/codes/git-repo.git'
repo init -u ssh://linux@192.168.1.53:/tmp/codes/manifest.git

在这里插入图片描述

6 Repo使用扩展:forall

本章主要介绍repo forall命令,方便操作git仓库。

6.1 查看remote

repo forall -pv -c 'git remote -v'

在这里插入图片描述

6.2 添加备份remote

repo同步后只有一个仓库连接,当需要添加多个路径时,可以单个仓库单独添加,也可以使用forall命令批量添加:

repo forall -pv -c 'git remote add baiducloud ubuntu@106.12.156.236:/codes/git/$REPO_PROJECT'

说明:REPO_PROJECT是repo的环境变量,代表当前仓库名。

可以将以上命令用alias命令虫命名:

alias repo_remote_add="echo repo forall -pv -c \'git remote add baiducloud ubuntu@106.12.156.236:/codes/git/'$'REPO_PROJECT\' | bash"

在这里插入图片描述

添加成功后就可以使用repo forall -pv -c "git push baiducloud master"命令推送代码了。

附录(扩展)

仓库关联多个远程仓库

参考:Git仓关联多个远程仓路径.md

本地台式机指令:

EBX_COM_GIT_ROOT_URL='ssh://wanghb@192.168.10.120:29418/codes/git/'
BAIDU_CLOUT_GIT_ROOT_URL='ssh://ubuntu@192.168.0.110:/codes/git/'

alias repo_remote_show="echo repo forall -pv -c \'git remote -v\' | bash"
alias repo_remote_push_master_to_origin="echo repo forall -pv -c \'git push origin master:master\' | bash"

alias repo_remote_add_origin_ebx_showcmd="echo repo forall -pv -c \'git remote set-url origin --push --add $EBX_COM_GIT_ROOT_URL'$'REPO_PROJECT\'"
alias repo_remote_add_origin_baidu_showcmd="echo repo forall -pv -c \'git remote set-url origin --push --add $BAIDU_CLOUT_GIT_ROOT_URL'$'REPO_PROJECT\'"
alias repo_remote_add_origin_ebx="echo repo forall -pv -c \'git remote set-url origin --push --add $EBX_COM_GIT_ROOT_URL'$'REPO_PROJECT\' | bash"
alias repo_remote_add_origin_baidu="echo repo forall -pv -c \'git remote set-url origin --push --add $BAIDU_CLOUT_GIT_ROOT_URL'$'REPO_PROJECT\' | bash"

仓库备份或迁移

将一个仓库克隆,并将完整仓库(分支和标签)推送到另一个服务器

git clone remote_url --mirror
git push new --mirror

多仓库处理脚本:

#!/bin/bash

WORKROOT_PATH=${PWD}

GIT_REMOTE_URL_HEAD="http://192.168.10.120/r/codes/git/"
GIT_REMOTE_NEW_URL_HEAD="git@192.168.57.140:git/"
RESP_LIST=" bin
            demo
            Documents
            gnc_api_lib
            libmodbus"

for sub_repo in $RESP_LIST
do
    remote_url="$GIT_REMOTE_URL_HEAD$sub_repo.git"
    remote_new_url="$GIT_REMOTE_NEW_URL_HEAD$sub_repo.git"
    echo "      sub_repo:$sub_repo"
    echo "    remote_url:$remote_url"
    echo "remote_new_url:$remote_new_url"

    cd $WORKROOT_PATH

    # clone
    git clone $remote_url --mirror                              || { echo "[line:$LINENO] failed"; exit $LINENO; }

    cd $sub_repo.git &> /dev/null                               || { echo "[line:$LINENO] failed"; exit $LINENO; }

    # 设置远端路径
    git remote add new $remote_new_url                          || { echo "[line:$LINENO] failed"; exit $LINENO; }

    # 推送代码
    git push new --mirror                                       || { echo "[line:$LINENO] failed"; exit $LINENO; }

    # 完成
    echo "push $sub_repo finished "
    echo "-----------------------------------------------------"
    echo ""
done

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值