[PATCH] Added ability to clone from a local repository to a (new) remote one

Sean Meiners sean.meiners at linspire.com
Thu Jun 22 17:44:10 CDT 2006


# HG changeset patch
# User Sean Meiners <sean.meiners at linspire.com>
# Node ID 8573326a08eea4ed6357a288da31e0f3bfdfb3c3
# Parent  1066c585767ecaa57dc08960a52d12d3c692b202
Added ability to clone from a local repository to a (new) remote one.

I had to rearrange the clone command a good bit to make sure it validates that
the source does exist and that the destination doesn't before doing anything.
Before I moved the source repo check it would create the destination
repository before it verified the source existed. I also changed the names of
the 'repo' and 'other' variables to 'dest_repo' and 'src_repo' to maintain my
sanity.

diff -r 1066c585767e -r 8573326a08ee mercurial/commands.py
--- a/mercurial/commands.py	Wed Jun 21 22:45:29 2006 -0700
+++ b/mercurial/commands.py	Thu Jun 22 15:43:16 2006 -0700
@@ -914,8 +914,6 @@ def clone(ui, source, dest=None, **opts)
     if os.path.exists(dest):
         raise util.Abort(_("destination '%s' already exists"), dest)
 
-    dest = os.path.realpath(dest)
-
     class Dircleanup(object):
         def __init__(self, dir_):
             self.rmtree = shutil.rmtree
@@ -933,13 +931,26 @@ def clone(ui, source, dest=None, **opts)
         ui.setconfig("ui", "remotecmd", opts['remotecmd'])
 
     source = ui.expandpath(source)
-
-    d = Dircleanup(dest)
+    src_repo = hg.repository(ui, source)
+
+    dest_repo = None
+    try:
+        dest_repo = hg.repository(ui, dest)
+        raise util.Abort(_("destination '%s' already exists." % dest))
+    except hg.RepoError:
+        dest_repo = hg.repository(ui, dest, create=1)
+
+    dest_path = None
+    d = None
+    if dest_repo.dev() != -1:
+        dest_path = os.path.realpath(dest)
+        d = Dircleanup(dest_path)
+
     abspath = source
-    other = hg.repository(ui, source)
+    src_repo = hg.repository(ui, source)
 
     copy = False
-    if other.dev() != -1:
+    if src_repo.dev() != -1 and dest_repo.dev() != -1:
         abspath = os.path.abspath(source)
         if not opts['pull'] and not opts['rev']:
             copy = True
@@ -950,47 +961,53 @@ def clone(ui, source, dest=None, **opts)
             # can end up with extra data in the cloned revlogs that's
             # not pointed to by changesets, thus causing verify to
             # fail
-            l1 = other.lock()
+            l1 = src_repo.lock()
         except lock.LockException:
             copy = False
 
     if copy:
         # we lock here to avoid premature writing to the target
-        os.mkdir(os.path.join(dest, ".hg"))
-        l2 = lock.lock(os.path.join(dest, ".hg", "lock"))
+        os.mkdir(os.path.join(dest_path, ".hg"))
+        l2 = lock.lock(os.path.join(dest_path, ".hg", "lock"))
 
         files = "data 00manifest.d 00manifest.i 00changelog.d 00changelog.i"
         for f in files.split():
             src = os.path.join(source, ".hg", f)
-            dst = os.path.join(dest, ".hg", f)
+            dst = os.path.join(dest_path, ".hg", f)
             try:
                 util.copyfiles(src, dst)
             except OSError, inst:
                 if inst.errno != errno.ENOENT:
                     raise
 
-        repo = hg.repository(ui, dest)
-
     else:
         revs = None
         if opts['rev']:
-            if not other.local():
+            if not src_repo.local():
                 error = _("clone -r not supported yet for remote repositories.")
                 raise util.Abort(error)
             else:
                 revs = [other.lookup(rev) for rev in opts['rev']]
-        repo = hg.repository(ui, dest, create=1)
-        repo.pull(other, heads = revs)
-
-    f = repo.opener("hgrc", "w", text=True)
-    f.write("[paths]\n")
-    f.write("default = %s\n" % abspath)
-    f.close()
-
-    if not opts['noupdate']:
-        doupdate(repo.ui, repo)
-
-    d.close()
+
+        if dest_repo.dev() != -1:
+            dest_repo.pull(src_repo, heads = revs)
+        elif src_repo.dev() != -1:
+            src_repo.push(dest_repo, revs = revs)
+        else:
+            error = _("clone from remote to remote not supported.")
+            raise util.Abort(error)
+
+    if dest_repo.dev() != -1:
+        f = repo.opener("hgrc", "w", text=True)
+        f.write("[paths]\n")
+        f.write("default = %s\n" % abspath)
+        f.close()
+
+        if not opts['noupdate']:
+            doupdate(repo.ui, repo)
+
+    if d:
+        d.close()
 
 def commit(ui, repo, *pats, **opts):
     """commit the specified files or all outstanding changes
diff -r 1066c585767e -r 8573326a08ee mercurial/hg.py
--- a/mercurial/hg.py	Wed Jun 21 22:45:29 2006 -0700
+++ b/mercurial/hg.py	Thu Jun 22 15:43:16 2006 -0700
@@ -33,6 +33,9 @@ def local_(ui, path, create=0):
         path = path[5:]
     return localrepo.localrepository(ui, path, create)
 
+def ssh_(ui, path, create=0):
+    return sshrepo.sshrepository(ui, path, create)
+
 def old_http(ui, path):
     ui.warn(_("old-http:// syntax is deprecated, "
               "please use static-http:// instead\n"))
@@ -50,7 +53,7 @@ schemes = {
     'http': lambda ui, path: httprepo.httprepository(ui, path),
     'https': lambda ui, path: httprepo.httpsrepository(ui, path),
     'old-http': old_http,
-    'ssh': lambda ui, path: sshrepo.sshrepository(ui, path),
+    'ssh': ssh_,
     'static-http': static_http,
     }
 
diff -r 1066c585767e -r 8573326a08ee mercurial/sshrepo.py
--- a/mercurial/sshrepo.py	Wed Jun 21 22:45:29 2006 -0700
+++ b/mercurial/sshrepo.py	Thu Jun 22 15:43:16 2006 -0700
@@ -12,7 +12,7 @@ demandload(globals(), "hg os re stat uti
 demandload(globals(), "hg os re stat util")
 
 class sshrepository(remoterepository):
-    def __init__(self, ui, path):
+    def __init__(self, ui, path, create=0):
         self.url = path
         self.ui = ui
 
@@ -30,6 +30,25 @@ class sshrepository(remoterepository):
 
         sshcmd = self.ui.config("ui", "ssh", "ssh")
         remotecmd = self.ui.config("ui", "remotecmd", "hg")
+
+        if create:
+            try:
+                self.validate_repo(ui, sshcmd, args, remotecmd)
+                return # the repo is good, nothing more to do
+            except hg.RepoError:
+                pass
+
+            cmd = '%s %s "%s init %s"'
+            cmd = cmd % (sshcmd, args, remotecmd, self.path)
+
+            ui.note('running %s\n' % cmd)
+            res = os.system(cmd)
+            if res != 0:
+                raise hg.RepoError(_("could not create remote repo"))
+
+        self.validate_repo(ui, sshcmd, args, remotecmd)
+
+    def validate_repo(self, ui, sshcmd, args, remotecmd):
         cmd = '%s %s "%s -R %s serve --stdio"'
         cmd = cmd % (sshcmd, args, remotecmd, self.path)
 


More information about the Mercurial mailing list