Skip to content

Instantly share code, notes, and snippets.

@thales17
Created May 14, 2019 19:01
Show Gist options
  • Save thales17/9178fe78a50ea727fc734822374a50b9 to your computer and use it in GitHub Desktop.
Save thales17/9178fe78a50ea727fc734822374a50b9 to your computer and use it in GitHub Desktop.
Migration script to upload a large set of repos to github
#!/usr/local/bin/python3
import os
from github import Github
def get_subdirs(p_dir):
return [name for name in os.listdir(p_dir)
if os.path.isdir(os.path.join(p_dir, name))]
def create_github_repo(r_name):
token = 'YOUR_TOKEN'
g = Github(token)
o = g.get_organization('forestgiant')
repo = o.create_repo(name=r_name, private=True)
print(repo)
def exec(cmd):
print(cmd)
os.system(cmd)
def clone_and_push_repo(r_dir):
r_name = os.path.splitext(os.path.basename(r_dir))[0]
template = 'git clone --mirror %s working/%s/.git'
exec(template % (r_dir, r_name))
p = 'working/%s/.git' % r_name
exec('git --git-dir %s config --bool core.bare false' % p)
exec('git --git-dir %s checkout' % p)
create_github_repo(r_name)
repo_path = 'git@github.com:forestgiant/%s.git' % r_name
exec('git --git-dir %s remote add github %s' % (p, repo_path))
exec('git --git-dir %s push --all github' % p)
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print('Please supply a repo parent directory')
else:
p_dir = sys.argv[1]
repos = get_subdirs(p_dir)
for repo in repos:
if repo in ['docs.git', 'scripts.git']:
print('Skipping %s, please do this manually' % repo)
continue
clone_and_push_repo(os.path.join(p_dir, repo))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment