changeset 2549:e1831f06eef1

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.
author Sean Meiners <sean.meiners@linspire.com>
date Fri, 30 Jun 2006 19:24:02 -0700
parents 8cb894370514
children 45235e492cc6
files mercurial/commands.py mercurial/hg.py mercurial/localrepo.py mercurial/sshrepo.py
diffstat 4 files changed, 72 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Fri Jun 30 23:02:08 2006 +0200
+++ b/mercurial/commands.py	Fri Jun 30 19:24:02 2006 -0700
@@ -919,13 +919,10 @@
     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 @@
         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 @@
             # 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 @@
 
     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):
--- a/mercurial/hg.py	Fri Jun 30 23:02:08 2006 +0200
+++ b/mercurial/hg.py	Fri Jun 30 19:24:02 2006 -0700
@@ -33,6 +33,9 @@
         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 @@
     '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,
     }
 
--- a/mercurial/localrepo.py	Fri Jun 30 23:02:08 2006 +0200
+++ b/mercurial/localrepo.py	Fri Jun 30 19:24:02 2006 -0700
@@ -74,6 +74,8 @@
         self.transhandle = None
 
         if create:
+	    if not os.path.exists(path):
+		os.mkdir(path)
             os.mkdir(self.path)
             os.mkdir(self.join("data"))
 
--- a/mercurial/sshrepo.py	Fri Jun 30 23:02:08 2006 +0200
+++ b/mercurial/sshrepo.py	Fri Jun 30 19:24:02 2006 -0700
@@ -12,7 +12,7 @@
 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 @@
 
         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)