当前位置:   article > 正文

从repo仓库提取git裸仓库_如何从bare repo检出数据

如何从bare repo检出数据

背景

公司从供应商获取到的sdk,解压出来后,里面是repo管理的源码仓库集合。但是我们要将这些仓库上传到我们自己的服务器进行管理。然而这个repo仓库不是mirror仓库。所以需要将里面的git按照原sdk来提取出来。因此写了这个脚本用来从sdk里面提取所有的裸仓库。

原理

  1. 根据.repo目录里面project.list获取到所有的git仓库路径。
  2. 再到.repo目录里面的manifest.xml里面找到对应的git路径。
  3. 然后用git clone --bare xxx提取出裸仓库到对应的git路径。

待优化

manifest完全可以用xml来解析,而不是按行来匹配。

参考

按https://blog.csdn.net/mmh19891113/article/details/83024816这篇文章操作出现报错。

源码

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import argparse

try:
    import configparser as configparser
except Exception:
    import ConfigParser as configparser
import os
import re
import shutil

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Get gits from repo project')
    parser.add_argument("-s", "--src", help="source absolute repo project dir")
    parser.add_argument("-d", "--dst", help="dest dir for save gits")
    args = parser.parse_args()

    print("source dir=%s", args.src)
    print("dst dir=%s", args.dst)

    project_list = os.path.join(args.src, ".repo", "project.list")
    manifest_xml = os.path.join(args.src, ".repo", "manifest.xml")

    with open(project_list) as p:
        p_lines = p.readlines()

    with open(manifest_xml) as m:
        m_lines = m.readlines()

    os.system("cd %s && repo start master --all" % args.src)

    pass_count = 0
    for p in p_lines:
        path = "path=\"%s\"" % p.strip()
        is_find = False
        for m in m_lines:
            if path in m.strip():
                src_git = os.path.join(args.src, p.strip())
                d = re.search(r"name=\"([^\"]*)\"", m.strip(), re.M | re.I)
                dst_git = os.path.join(args.dst, d.group(1))
                p_dst_git = os.path.dirname(dst_git)
                if not os.path.exists(p_dst_git):
                    os.makedirs(p_dst_git)
                dst_git_name = os.path.basename(dst_git) + ".git"
                os.system("cd %s && git clone --bare %s %s" % (p_dst_git, src_git, dst_git_name))
                # print("cd %s && git clone --bare %s %s" % (p_dst_git, src_git, dst_git_name))
                is_find = True
                pass_count = pass_count + 1
                print("[%d/%d]" % (pass_count, len(p_lines)))
                break
        if not is_find:
            print("cannot find %s" % p.strip())
            exit(-1)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/56293?site
推荐阅读
  

闽ICP备14008679号