Last active
August 29, 2015 14:07
-
-
Save rnewson/7a2591892183ebb5ece4 to your computer and use it in GitHub Desktop.
git wrapper to optimize network use by maintaining a local mirror
This file contains hidden or 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/bin/env python | |
# Put me in /usr/local/bin for magic caching (but set cache_root too!) | |
# Note this expects to run inside Jenkins, so remove line 20 if you don't do that. | |
import os | |
import sys | |
import urlparse | |
real_git = '/usr/bin/git' | |
cache_root = '/gitdata' | |
def find_url(args): | |
for arg in args: | |
if urlparse.urlparse(arg).scheme != '': | |
return arg | |
raise Exception("No url in argument list!") | |
def cache_path(url): | |
return os.path.join(cache_root, | |
os.environ['JOB_NAME'], | |
os.path.basename(url)) | |
def update_mirror(url): | |
path = cache_path(url) | |
if os.path.isdir(path): | |
print 'updating mirror of %s' % url | |
os.environ['GIT_DIR'] = path | |
os.system('%s remote update' % real_git) | |
else: | |
print 'creating mirror of %s' % url | |
os.system('%s clone --mirror %s %s' % (real_git, url, path)) | |
def inject_reference(args): | |
return [real_git, 'clone', '--reference', | |
cache_path(find_url(args))] + args[2:] | |
if __name__ == "__main__": | |
if sys.argv[1] == 'clone': | |
url = find_url(sys.argv) | |
update_mirror(url) | |
os.execv(real_git, inject_reference(sys.argv)) | |
else: | |
os.execv(real_git, sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment