Skip to content

Instantly share code, notes, and snippets.

@fopina
Created April 1, 2015 13:45
Show Gist options
  • Save fopina/2582f4a21ea1de8ba0dc to your computer and use it in GitHub Desktop.
Save fopina/2582f4a21ea1de8ba0dc to your computer and use it in GitHub Desktop.
Python script to push list of repositories to Gitlab
'''
I wanted to migrate all my gitosis repositories to gitlab.com.
Compile a list with the repositories in text file and run this script with your GITLAB token, the name of the text file and a directory to be used to clone the repositories temporarily.
Example:
python repo2gitlab.py x1xxxxxxxxxxX1Xx1xXX repos.txt /tmp/
'''
# annoying warnings
import requests
requests.packages.urllib3.disable_warnings()
import gitlab3, re
import os, sys, subprocess
def main():
if len(sys.argv) < 4:
print 'Missing arguments'
print 'Usage: %s TOKEN REPO_FILE TEMPORARY_DIR' % sys.argv[0]
sys.exit(1)
TOKEN = sys.argv[1]
REPO_FILE = sys.argv[2]
REPO_DIR = sys.argv[3]
gl = gitlab3.GitLab('https://gitlab.com/', TOKEN)
f = open(REPO_FILE,'r')
repos = {}
for line in f:
repo = line.strip()
m = re.match('.*?[:\/]([\w\-\.]+)$',repo)
if not m:
raise Exception('Invalid repository', repo)
name = m.group(1)
if name[-4:] == '.git':
name = name[:-4]
if name in repos:
raise Exception('Duplicated name', name, repo)
repos[name] = repo
os.chdir(REPO_DIR)
for name, repo in repos.iteritems():
print 'Creating %s (from %s)' % (name, repo)
p = gl.add_project(name, name, public=False)
new_repo = p.ssh_url_to_repo
subprocess.call([
'git',
'clone',
repo,
name
])
os.chdir(name)
subprocess.call([
'git',
'remote',
'add',
'gitlab',
new_repo
])
subprocess.call([
'git',
'push',
'gitlab',
'--mirror'
])
os.chdir('..')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment