Git 迁移到 GitLab

项目迁移

#gitlab
import gitlab
#git
import os
import shutil
import git

url = '填写gitlaburl'
token = '填写gitlab token'

baseUrl = ""
gitfoldpath = "git 文件夹"
currentfoldpath = "当前文件夹"
#文件夹目录
nameDict = {}

#init
gl = gitlab.Gitlab(url,token)
# search myGroup
groups = gl.groups.list()
print(groups)
group_id = groups[0].id
#group_id = gl.groups.search('my-group')[0].id

print(group_id)
#project = gl.projects.create({'name': 'myrepo', 'namespace_id': group_id})


for key,value in nameDict.items():
    print(key,value)
    gitLabpath = currentfoldpath+key
    sourcepath = gitfoldpath+key
    project = gl.projects.create({'name':value,'namespace_id':group_id})
    print('create success')
    #initGit
    repo = git.Repo.clone_from(url=baseUrl+value+".git",to_path= gitLabpath)
    print('down load success')
    #findSource
    shutil.copytree(sourcepath,gitLabpath+'//'+key)
    print('path copy success')
    #makeCommit push
    index = repo.index
    index.add(repo.untracked_files)
    newcommit= index.commit('Init')
    print('Init Success')
    git2 = repo.git
    git2.push('-u','origin','master')
    print('push success')


1.先在远端建立仓库。
2.clone到本地。
3.将需要迁移的项目copy过来。(主要是项目正好进行模块化 需要一块一块的迁移)
4.将整个项目提交。
参考资料:gitlab-python gitPython
后记
基本上 git命令能做的 gitpython 也是可以做的。

你可能感兴趣的:(Git 迁移到 GitLab)