批量Clone Git仓库代码
问题
经常在工作中需要clone大量项目代码,仓库特别多,git一条命令一条命令下载太过于繁琐。
解决办法
使用GitPython
下载并安装GitPython
pip3 install GitPython
编写批量Clone代码
import argparse
import os
from concurrent.futures import ThreadPoolExecutor
from git import Repo
argparser = argparse.ArgumentParser("批量下载Git Repos")
argparser.add_argument("-p", "--project-path", type=str, required=True, help="项目地址,所有批量下载的repos都会存在这个目录下。")
argparser.add_argument("-m", "--multi-thread", default=False, action="store_true", help="开启多线程,默认False")
args = argparser.parse_args()
repos = [
... # 这里可以填入你想要clone的仓库地址
]
def bulk_clone_repos():
def get_dest(git_repo_url:str):
return os.path.join(args.project_path, git_repo_url.rsplit(".", maxsplit=1)[0].rsplit("/", maxsplit=1)[1])
executor = ThreadPoolExecutor(max_workers=10)
for repo in repos:
print("Git clone: [{}]".format(repo))
if args.multi_thread:
executor.submit(Repo.clone_from, repo, get_dest(repo))
else:
Repo.clone_from(url=repo, to_path=get_dest(repo))
if __name__ == "__main__":
bulk_clone_repos()
改进
其实repos可以通过文件方式读入可能会更好。
即如下
python3 bulk_git_clone.py -p xxx -m -r [file]
再编写一个可以管理项目repo的脚本,等到新人来的时候,直接用脚本生成他需要clone的repos列表文件,让他直接调用这个bulk_git_clone加上列表。
芜湖,起飞。