Skip to content

Instantly share code, notes, and snippets.

@jmingtan
Created April 27, 2011 09:58
Show Gist options
  • Save jmingtan/944001 to your computer and use it in GitHub Desktop.
Save jmingtan/944001 to your computer and use it in GitHub Desktop.
Import repos
import os
import os.path
import sys
def is_mercurial(directory):
"""Tests whether a given path contains a .hg folder."""
hg_path = os.path.join(directory, ".hg")
return os.path.isdir(hg_path)
def checkout(directory):
"""Recursively checkout all mercurial repositories into current folder.
Preserves the subdirectory structure as well.
"""
for d_name in os.listdir(directory):
d_path = os.path.join(directory, d_name)
if os.path.isdir(d_path):
if is_mercurial(d_path):
print
print "------------------------ checkout", d_name
os.system("hg clone -U %s" % d_path)
else:
os.mkdir(d_name)
os.chdir(d_name)
checkout(d_path)
os.chdir('..')
if __name__ == '__main__':
if len(sys.argv) < 3:
print "Import script for mercurial repositories."
print "\t- Imports mercurial repositories from one root to another."
print
print "Instructions for usage:"
print "\tpython %s <source-repo> <destination>" % sys.argv[0]
print "\t<source-repo> - SCM root folder path"
print "\t<destination> - Where to place the cloned repositories"
sys.exit(0)
directory = sys.argv[1]
output = sys.argv[2]
current_path = '.'
print "Import mercurial repos from %s to %s" % (directory, output)
os.chdir(output)
checkout(os.path.abspath(directory))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment