python api gitlab迁移所有项目
1、 gitlab生成token (略)
帐号后台生成toekn
2、python拉取所有的gitlab项目
代码如下: vim get_git_all.py
import os,sys
if sys.version_info < (3, 0):
import urllib
else:
from urllib.request import urlopen
import json
import subprocess, shlex
import time
import os
gitlabAddr = 'gitlab.xxx.com'
gitlabToken = 'xxx'
for index in range(10):
url = "http://%s/api/v4/projects?private_token=%s&per_page=100&page=%d&order_by=name" % (gitlabAddr, gitlabToken, index)
print(url)
if sys.version_info < (3, 0):
allProjects = urllib.urlopen(url)
else:
allProjects = urlopen(url)
allProjectsDict = json.loads(allProjects.read().decode(encoding='UTF-8'))
if len(allProjectsDict) == 0:
break
for thisProject in allProjectsDict:
try:
thisProjectURL = thisProject['http_url_to_repo']
thisProjectPath = thisProject['path_with_namespace']
print(thisProjectURL + ' ' + thisProjectPath)
if os.path.exists(thisProjectPath):
# command = shlex.split('cd "%s" && git pull' % (thisProjectPath))
os.system('cd %s && git pull' % thisProjectPath)
else:
command = shlex.split('git clone --mirror %s %s' % (thisProjectURL, thisProjectPath))
resultCode = subprocess.Popen(command)
time.sleep(3)
except Exception as e:
print("Error on %s: %s" % (thisProjectURL, e.strerror))
3 、修改git地址脚本&&上传到新的gitlab (得保证目标仓库没有同路径的仓库)
for tmp in `ls -r |sort -n`;do
if [ -d $tmp ];then
cd $tmp
for xx in `ls -r`;do
if [ -d $xx ];then
echo $tmp/$xx
cd $xx
sed -i 's#gitlab.xxx.com#gitlab.newxxx.com/lbx#g' config
git push --mirror
cd ..
fi
done
cd ..
fi
done
提示: 运维git迁移命令:
git clone --mirror xxxxx.git
cd xxxxx
sed -i 's#gitlab.xxx.com#gitlab.xxxnew.com/lbx#g' config
git push --mirror
----------当你发现自己的才华撑不起野心时,就请安静下来学习吧---------
分类:
python编程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用