Created
January 18, 2013 23:57
-
-
Save claytron/4569713 to your computer and use it in GitHub Desktop.
Script to mirror repos from an organization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
# this script gets the JSON from api.github for a user | |
# and mirrors (or fetches, for existing) that user's repos | |
import os | |
import sys | |
import json | |
import urllib2 | |
import subprocess | |
GIT_BINARY="/usr/local/bin/git" | |
OAUTH_KEY="AUTH_KEY_HERE" | |
if (len(sys.argv) != 3): | |
print "usage: mirror-github-repos <username> </full/base/path/for/cloned-repos>" | |
quit() | |
url = "https://api.github.com/orgs/%s/repos?access_token=%s&page=1&per_page=100" % (sys.argv[1], OAUTH_KEY) | |
req = urllib2.Request(url) | |
res = urllib2.urlopen(req) | |
json_object = json.load(res) | |
for object in json_object: | |
full_repo_name = '%s/%s.git' % (sys.argv[2], object["name"]) | |
if (os.path.isdir(full_repo_name)): | |
# EXISTING - update repo | |
os.chdir(full_repo_name) | |
subprocess.call([GIT_BINARY, "fetch", "-q", "--all", "--tags"]) | |
else: | |
# NEW - do full clone into dir | |
subprocess.call([GIT_BINARY, "clone", "--mirror", object["ssh_url"], full_repo_name]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment