最近需要下载github代码发现速度出奇的慢,修改hosts效果也不明显,加上github代码内有些文件夹是以@XXXX形式的链接存在,需使用 --recurse-submodules 递归下载子文件夹。
好在发现https://hub.fastgit.org/ 包括国内一些镜像网站提供了一个国内快速下载github的解决方案。
用https://hub.fastgit.org/ 直接替换 https://github.com/可加快下载速度。
但是递归下载子模块时仍然需要链接到https://github.com/下载,导致子模块下载出错。
需要手动将下载后的项目文件夹中的“.gitmodule”文件内的
url = https://github.com/xxxx 修改文为:url = https://hub.fastgit.org/XXXX
单项目下载还可以接受,批量下载则比较麻烦,因此写了一个简单的批处理进行解决
注:此批处理默认按最新tag下载分支,通常情况下按tag下载分支都可以编译通过,按branch下载有时候不能编译通过,因此以最新tag为准下载分支。
fastgit.bat //单个调用
call_fastgit.bat // 批量调用
github_url //github项目链接地址
fastgit.bat 内容如下
@echo off
setlocal enabledelayedexpansion
echo "Usage:fastgit.bat https://github.com/horsicq/stringsx64dbg.git"
echo %1
set remain=%1
set str=%remain:github.com=hub.fastgit.org%
echo "下载地址:%str%"
set toFind=git
set isFind=false
set now=
set next=
:loop
for /f "tokens=1* delims=./" %%a in ("%remain%") do (
::echo %%b
set now=%%a
set next=%%b
::pause
if "%toFind%"=="%%b" (
set isFind=true
::echo %now% finded
goto :finded
)
set remain=%%b
)
if defined remain goto :loop
:finded
git clone %str%
cd %now%
git tag
git describe --tags --abbrev=0
for /f "delims=" %%i in ('git tag') do set new_tag=%%i
cd ..
rmdir /s /q %now%
echo 下载:git clone -b %new_tag% %str%
git clone -b %new_tag% %str%
cd %now%
SET modules= %~dp0%now%\.gitmodules
if exist %modules% (
goto :gitmodules_replace)
) else (
goto :no_gitmodules
)
:gitmodules_replace
echo "进入gitmodules_replace"
echo %modules%
for /f "delims=" %%I in (.gitmodules) do (
set "aa=%%I"
set "aa=!aa:github.com=hub.fastgit.org!"
echo !aa!>>1.txt)
del .gitmodules
rename 1.txt .gitmodules
git submodule update --init --recursive
echo %now%" 下载成功!"
goto :exit
:no_gitmodules
echo .gitmodules 不存在
goto :exit
:exit
cd ..
echo “退出子目录”
call_fastgit.bat 内容如下:
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (github_url.txt) do ( echo 开始下载:fastgit %%a& fastgit %%a)
pause
github_url.txt内容如下:
https://github.com/xxxxx/aaaaa.git
https://github.com/xxxxx/bbbbb.git
https://github.com/xxxxxx/ccccc.git
运行call_fastgit.bat 即可开始下载