如何使用git clone --recursive加速/并行化git子模块的下载?

肾上腺

克隆具有很多子模块的git仓库需要很长时间。在下面的示例中,共有〜100个子模块

git clone --recursive https://github.com/Whonix/Whonix

Git一一克隆它们。花费的时间比要求的长得多。让我们(可能)假设客户端和服务器都有足够的资源来同时回答多个(并行)请求。

如何使用加速/并行化git子模块的下载git clone --recursive

安通

当我运行您的命令时,下载68 Mb的时间需要338秒。

使用以下依赖于GNU并行安装的Python程序,

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

from __future__ import print_function

import os
import subprocess

jobs=16

modules_file = '.gitmodules'

packages = []

if not os.path.exists('Whonix/' + modules_file):
    subprocess.call(['git', 'clone', 'https://github.com/Whonix/Whonix'])

os.chdir('Whonix')

# get list of packages from .gitmodules file
with open(modules_file) as ifp:
    for line in ifp:
        if not line.startswith('[submodule '):
            continue
        package = line.split(' "', 1)[1].split('"', 1)[0]
        #print(package)
        packages.append(package)

def doit():
    p = subprocess.Popen(['parallel', '-N1', '-j{0}'.format(jobs),
                          'git', 'submodule', 'update', '--init',
                          ':::'],
                         stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    res = p.communicate('\n'.join(packages))
    print(res[0])
    if res[1]:
        print("error", res[1])
    print('git exit value', p.returncode)
    return p.returncode

# sometimes one of the updates interferes with the others and generate lock
# errors, so we retry
for x in range(10):
    if doit() == 0:
        print('zero exit from git after {0} times'.format(x+1))
        break
else:
    print('could not get a non-zero exit from git after {0} times'.format(
          x+1))

那个时间缩短至45秒(在同一系统上,我并没有做多的运行,以平均出波动)。

要检查一切是否正常,我将检出的文件与以下文件进行了“比较”:

find Whonix -name ".git" -prune -o -type f -print0 | xargs -0 md5sum > /tmp/md5.sum

在一个目录中

md5sum -c /tmp/md5sum 

在另一个目录中,反之亦然。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章