hg.repository: make protocol table driven.
authorVadim Gelfer <vadim.gelfer@gmail.com>
Tue, 20 Jun 2006 18:39:52 -0700
changeset 2469 2e91ba371c4c
parent 2468 1ac0574f1768
child 2471 6904e1ef8ad1
hg.repository: make protocol table driven. allows extensions to add new protocols.
mercurial/hg.py
--- a/mercurial/hg.py	Tue Jun 20 17:51:39 2006 -0700
+++ b/mercurial/hg.py	Tue Jun 20 18:39:52 2006 -0700
@@ -11,34 +11,60 @@
 from i18n import gettext as _
 demandload(globals(), "localrepo bundlerepo httprepo sshrepo statichttprepo")
 
+def bundle(ui, path):
+    if path.startswith('bundle://'):
+        path = path[9:]
+    else:
+        path = path[7:]
+    s = path.split("+", 1)
+    if len(s) == 1:
+        repopath, bundlename = "", s[0]
+    else:
+        repopath, bundlename = s
+    return bundlerepo.bundlerepository(ui, repopath, bundlename)
+
+def hg(ui, path):
+    ui.warn(_("hg:// syntax is deprecated, please use http:// instead\n"))
+    return httprepo.httprepository(ui, path.replace("hg://", "http://"))
+
+def local_(ui, path, create=0):
+    return localrepo.localrepository(ui, path, create)
+
+def old_http(ui, path):
+    ui.warn(_("old-http:// syntax is deprecated, "
+              "please use static-http:// instead\n"))
+    return statichttprepo.statichttprepository(
+        ui, path.replace("old-http://", "http://"))
+
+def static_http(ui, path):
+    return statichttprepo.statichttprepository(
+        ui, path.replace("static-http://", "http://"))
+
+protocols = {
+    'bundle': bundle,
+    'file': local_,
+    'hg': hg,
+    '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),
+    'static-http': static_http,
+    None: local_,
+    }
+
 def repository(ui, path=None, create=0):
-    if path:
-        if path.startswith("http://"):
-            return httprepo.httprepository(ui, path)
-        if path.startswith("https://"):
-            return httprepo.httpsrepository(ui, path)
-        if path.startswith("hg://"):
-            ui.warn(_("hg:// syntax is deprecated, "
-                      "please use http:// instead\n"))
-            return httprepo.httprepository(
-                ui, path.replace("hg://", "http://"))
-        if path.startswith("old-http://"):
-            ui.warn(_("old-http:// syntax is deprecated, "
-                      "please use static-http:// instead\n"))
-            return statichttprepo.statichttprepository(
-                ui, path.replace("old-http://", "http://"))
-        if path.startswith("static-http://"):
-            return statichttprepo.statichttprepository(
-                ui, path.replace("static-http://", "http://"))
-        if path.startswith("ssh://"):
-            return sshrepo.sshrepository(ui, path)
-        if path.startswith("bundle://"):
-            path = path[9:]
-            s = path.split("+", 1)
-            if  len(s) == 1:
-                repopath, bundlename = "", s[0]
-            else:
-                repopath, bundlename = s
-            return bundlerepo.bundlerepository(ui, repopath, bundlename)
-
-    return localrepo.localrepository(ui, path, create)
+    scheme = path
+    if scheme:
+        c = scheme.find(':')
+        scheme = c >= 0 and scheme[:c]
+    if not scheme: scheme = None
+    try:
+        ctor = protocols[scheme]
+        if create:
+            return ctor(ui, path, create)
+        return ctor(ui, path)
+    except KeyError:
+        raise util.Abort(_('protocol "%s" not known') % scheme)
+    except TypeError:
+        raise util.Abort(_('cannot create new repository over "%s" protocol') %
+                         (scheme or 'file'))