![](https://img.haomeiwen.com/i6943526/aceee36e42b59d61.jpg)
最近需要从Gitlab上拉取多份代码,一个个操作有点麻烦,就使用使用Gitlab提供的API来批量Clone代码。
![](https://img.haomeiwen.com/i6943526/3c980b8441c94ef7.png)
gitlab有提供api来获取git仓库的信息,利用这些信息clone下项目
http://your-gitlab-server//api/v4/projects
--每页显示150个项目
http://your-gitlab-server//api/v4/projects?per_page=150
参见文档:https://docs.gitlab.com/ee/api/projects.html#list-all-projects
操作步骤
1.获取Personal Access Token(注意不是Feed token)
![](https://img.haomeiwen.com/i6943526/27ac4b56ac5eff44.png)
2.配置环境
配置python环境,我这里安装的是python3.1
![](https://img.haomeiwen.com/i6943526/90c198112ab72236.png)
3.执行脚本
以下代码保存为batchClone.py
打开命令提示框, 执行 py batchClone.py
![](https://img.haomeiwen.com/i6943526/b8c40bde46c9b6cf.png)
from urllib.request import urlopen
import json
import os
def clone_ssh(thisProject):
# ssh 下载
thisProjectURL = thisProject['ssh_url_to_repo']
thisProjectPath = thisProject['path_with_namespace']
print('download: ', thisProjectURL, ' ', thisProjectPath)
if os.path.exists(thisProjectPath):
command = 'git -C "%s" pull' % (thisProjectPath)
else:
command = 'git clone %s %s' % (thisProjectURL, thisProjectPath)
os.system(command)
def clone_http(thisProject):
thisProjectURL = thisProject['http_url_to_repo']
thisProjectPath = thisProject['path_with_namespace']
print('download: ', thisProjectURL, ' ', thisProjectPath)
if os.path.exists(thisProjectPath):
command = 'git clone %s' % (thisProjectPath)
else:
command = 'git clone %s %s' % (thisProjectURL, thisProjectPath)
os.system(command)
def download(gitlabAddr, gitlabToken, ssh_or_http):
for index in range(10):
url = "%s/api/v4/projects?private_token=%s&per_page=100&page=%d&order_by=name" % (
gitlabAddr, gitlabToken, index)
print(url)
allProjects = urlopen(url)
allProjectsDict = json.loads(allProjects.read().decode())
print(allProjectsDict)
print("###################")
print("###################")
if len(allProjectsDict) == 0:
print("###################")
print("nothing")
print("###################")
break
for thisProject in allProjectsDict:
path = thisProject['http_url_to_repo'] + ' ' + thisProject['path_with_namespace']
print(path)
print("download all")
filter_str = input()
filter = []
if len(filter_str.lstrip()) != 0:
filter = filter_str.split(',')
for thisProject in allProjectsDict:
try:
# http下载
path = thisProject['http_url_to_repo'] + ' ' + thisProject['path_with_namespace']
if len(filter) == 0:
if ssh_or_http == "http":
clone_http(thisProject)
else:
clone_ssh(thisProject)
else:
for f in filter:
if f in path:
if ssh_or_http == "http":
clone_http(thisProject)
else:
clone_ssh(thisProject)
else:
print('skip download: ', path)
# time.sleep(1)
except Exception as e:
print(e)
if __name__ == '__main__':
while True:
try:
print("gitlabAddr:(eg:http://127.0.0.1)")
gitlabAddr = input()
print("gitlabToken:(eg:2yxfaBjWLpugzP7wNyZG")
gitlabToken = input()
download(gitlabAddr, gitlabToken, "http")
except Exception as e:
print(e)
![](https://img.haomeiwen.com/i6943526/673776635c3ba2fc.gif)
网友评论