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

Sean Meiners sean.meiners at linspire.com
Fri Jun 30 19:58:53 CDT 2006


# HG changeset patch
# User Sean Meiners <sean.meiners at linspire.com>
# Node ID 917d84809ce13d4f1ac0d9b20e58c1bc6fdef248
# Parent  8cb8943705147202d19493ff5f46883363fbc851
Added ability to clone from a local repository to a (new) remote one.

Rearranged 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.

Moved the responsibility for creating the destination repo root directory
entirly into the localrepo class so that local to local cloning doesn't break.
This also simplifies the code a bit since it's no longer being done in both
clone and init.

Changed the names of the 'repo' and 'other' variables to 'dest_repo' and
'src_repo' to maintain my sanity.

Passes 82/83 tests. The only failure is the version number test, which I
suspect is supposed to fail since it comes from a generated file.

diff -r 8cb894370514 -r 917d84809ce1 mercurial/commands.py
--- a/mercurial/commands.py	Fri Jun 30 23:02:08 2006 +0200
+++ b/mercurial/commands.py	Fri Jun 30 17:58:06 2006 -0700
@@ -919,13 +919,10 @@ 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
             self.dir_ = dir_
-            os.mkdir(dir_)
         def close(self):
             self.dir_ = None
         def __del__(self):
@@ -938,13 +935,24 @@ 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.local():
+        dest_path = os.path.realpath(dest)
+        d = Dircleanup(dest_path)
+
     abspath = source
-    other = hg.repository(ui, source)
-
     copy = False
-    if other.dev() != -1:
+    if src_repo.local() and dest_repo.local():
         abspath = os.path.abspath(source)
         if not opts['pull'] and not opts['rev']:
             copy = True
@@ -955,47 +963,57 @@ 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"))
-
+        l2 = lock.lock(os.path.join(dest_path, ".hg", "lock"))
+
+	# we need to remove the (empty) data dir in dest so copyfiles can do it's work
+	os.rmdir( os.path.join(dest_path, ".hg", "data") )
         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)
+	# we need to re-init the repo after manually copying the data into it
+        dest_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()
+                revs = [src_repo.lookup(rev) for rev in opts['rev']]
+
+        if dest_repo.local():
+            dest_repo.pull(src_repo, heads = revs)
+        elif src_repo.local():
+            src_repo.push(dest_repo, revs = revs)
+        else:
+            error = _("clone from remote to remote not supported.")
+            raise util.Abort(error)
+
+    if dest_repo.local():
+        f = dest_repo.opener("hgrc", "w", text=True)
+        f.write("[paths]\n")
+        f.write("default = %s\n" % abspath)
+        f.close()
+
+        if not opts['noupdate']:
+            doupdate(dest_repo.ui, dest_repo)
+
+    if d:
+        d.close()
 
 def commit(ui, repo, *pats, **opts):
     """commit the specified files or all outstanding changes
@@ -1905,8 +1923,6 @@ def init(ui, dest="."):
 
     If no directory is given, the current directory is used.
     """
-    if not os.path.exists(dest):
-        os.mkdir(dest)
     hg.repository(ui, dest, create=1)
 
 def locate(ui, repo, *pats, **opts):
diff -r 8cb894370514 -r 917d84809ce1 mercurial/hg.py
--- a/mercurial/hg.py	Fri Jun 30 23:02:08 2006 +0200
+++ b/mercurial/hg.py	Fri Jun 30 17:58:06 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 8cb894370514 -r 917d84809ce1 mercurial/localrepo.py
--- a/mercurial/localrepo.py	Fri Jun 30 23:02:08 2006 +0200
+++ b/mercurial/localrepo.py	Fri Jun 30 17:58:06 2006 -0700
@@ -74,6 +74,8 @@ class localrepository(object):
         self.transhandle = None
 
         if create:
+	    if not os.path.exists(path):
+		os.mkdir(path)
             os.mkdir(self.path)
             os.mkdir(self.join("data"))
 
diff -r 8cb894370514 -r 917d84809ce1 mercurial/sshrepo.py
--- a/mercurial/sshrepo.py	Fri Jun 30 23:02:08 2006 +0200
+++ b/mercurial/sshrepo.py	Fri Jun 30 17:58:06 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