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
标签:git,python,gitlab,cd,api,thisProjectPath,import,com
From: https://www.cnblogs.com/Qing-840/p/16730042.html