[PATCH 5 of 6] Add some more smart when initializing destination repository

Edouard Gomez ed.gomez at free.fr
Thu Jun 7 16:50:22 CDT 2007


# HG changeset patch
# User Edouard Gomez <ed.gomez at free.fr>
# Date 1181250970 -7200
# Node ID c5f1b02be4b37a21dd76f8365cd77e8813a3a82f
# Parent  537e4260a38347d32765c8fdfb6510288c0b0698
Add some more smart when initializing destination repository

diff --git a/hgext/convert/__init__.py b/hgext/convert/__init__.py
--- a/hgext/convert/__init__.py
+++ b/hgext/convert/__init__.py
@@ -6,7 +6,7 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 import sys, os, zlib, sha, time, re, locale, socket
-from mercurial import hg, ui, util, commands
+from mercurial import hg, ui, util, commands, repo
 
 commands.norepo += " convert"
 
@@ -751,9 +751,28 @@ def _convert(ui, src, dest=None, mapfile
     if not dest:
         dest = src + "-hg"
         ui.status("assuming destination %s\n" % dest)
-        if not os.path.isdir(dest):
-            ui.status("creating repository %s\n" % dest)
-            os.system("hg init " + dest)
+
+    # Try to be smart and initalize things when required
+    if os.path.isdir(dest):
+        if len(os.listdir(dest)) > 0:
+            try:
+                hg.repository(ui, dest)
+                ui.status("destination %s is a Mercurial repository\n" % dest)
+            except repo.RepoError:
+                raise util.Abort(
+"""destination directory %s is not empty.
+Please specify an empty directory to be initialized or an already initialized
+mercurial repository
+""" % dest)
+        else:
+            ui.status("initializing destination %s repository\n" % dest)
+            hg.repository(ui, dest, create=True)
+    elif os.path.exists(dest):
+        raise util.Abort("destination %s exists and is not a directory\n" % dest)
+    else:
+        ui.status("initializing destination %s repository\n" % dest)
+        hg.repository(ui, dest, create=True)
+  
     destc = converter(ui, dest)
     if not hasattr(destc, "putcommit"):
         raise util.Abort("%s: can't write to this repo type\n" % src)


More information about the Mercurial-devel mailing list