changeset 2740:386f04d6ecb3

clean up hg.py: move repo constructor code into each repo module
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Mon, 31 Jul 2006 07:11:12 -0700
parents 3248aa10b388
children ae5ce3454ef5
files mercurial/bundlerepo.py mercurial/hg.py mercurial/httprepo.py mercurial/localrepo.py mercurial/sshrepo.py mercurial/statichttprepo.py mercurial/util.py
diffstat 7 files changed, 82 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bundlerepo.py	Sun Jul 30 22:52:34 2006 -0700
+++ b/mercurial/bundlerepo.py	Mon Jul 31 07:11:12 2006 -0700
@@ -237,3 +237,18 @@
             self.bundlefile.close()
         if self.tempfile is not None:
             os.unlink(self.tempfile)
+
+def instance(ui, path, create):
+    if create:
+        raise util.Abort(_('cannot create new bundle repository'))
+    path = util.drop_scheme('file', path)
+    if path.startswith('bundle:'):
+        path = util.drop_scheme('bundle', path)
+        s = path.split("+", 1)
+        if len(s) == 1:
+            repopath, bundlename = "", s[0]
+        else:
+            repopath, bundlename = s
+    else:
+        repopath, bundlename = '', path
+    return bundlerepository(ui, repopath, bundlename)
--- a/mercurial/hg.py	Sun Jul 30 22:52:34 2006 -0700
+++ b/mercurial/hg.py	Mon Jul 31 07:11:12 2006 -0700
@@ -12,86 +12,43 @@
 demandload(globals(), "localrepo bundlerepo httprepo sshrepo statichttprepo")
 demandload(globals(), "errno lock os shutil util")
 
-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):
-    if path.startswith('file:'):
-        path = path[5:]
-    if not create and os.path.isfile(path):
-        return bundlerepo.bundlerepository(ui, '', path)
-    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"))
-    return statichttprepo.statichttprepository(
-        ui, path.replace("old-http://", "http://"))
-
-def static_http(ui, path):
-    return statichttprepo.statichttprepository(
-        ui, path.replace("static-http://", "http://"))
+def _local(path):
+    return os.path.isfile(util.drop_scheme('file', path)) and bundlerepo or localrepo
 
 schemes = {
-    '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': ssh_,
-    'static-http': static_http,
+    'bundle': bundlerepo,
+    'file': _local,
+    'hg': httprepo,
+    'http': httprepo,
+    'https': httprepo,
+    'old-http': statichttprepo,
+    'ssh': sshrepo,
+    'static-http': statichttprepo,
     }
 
-remote_schemes = [
-    'bundle',
-    'hg',
-    'http',
-    'https',
-    'old-http',
-    'ssh',
-    'static-http',
-    ]
-
+def _lookup(path):
+    scheme = 'file'
+    if path:
+        c = path.find(':')
+        if c > 0:
+            scheme = path[:c]
+    thing = schemes.get(scheme) or schemes['file']
+    try:
+        return thing(path)
+    except TypeError:
+        return thing
+    
 def islocal(repo):
     '''return true if repo or path is local'''
     if isinstance(repo, str):
-        c = repo.find(':')
-        return c <= 0 or repo[:c] not in remote_schemes
+        try:
+            return _lookup(repo).islocal(repo)
+        except AttributeError:
+            return False
     return repo.local()
 
-def repository(ui, path=None, create=0):
-    scheme = None
-    if path:
-        c = path.find(':')
-        if c > 0:
-            scheme = schemes.get(path[:c])
-    else:
-        path = ''
-    ctor = scheme or schemes['file']
-    if create:
-        try:
-            return ctor(ui, path, create)
-        except TypeError:
-            raise util.Abort(_('cannot create new repository over "%s" protocol') %
-                             scheme)
-    return ctor(ui, path)
+def repository(ui, path=None, create=False):
+    return _lookup(path).instance(ui, path, create)
 
 def defaultdest(source):
     '''return default destination of clone if none is given'''
--- a/mercurial/httprepo.py	Sun Jul 30 22:52:34 2006 -0700
+++ b/mercurial/httprepo.py	Mon Jul 31 07:11:12 2006 -0700
@@ -339,3 +339,13 @@
             raise util.Abort(_('Python support for SSL and HTTPS '
                                'is not installed'))
         httprepository.__init__(self, ui, path)
+
+def instance(ui, path, create):
+    if create:
+        raise util.Abort(_('cannot create new http repository'))
+    if path.startswith('hg:'):
+        ui.warn(_("hg:// syntax is deprecated, please use http:// instead\n"))
+        path = 'http:' + path[3:]
+    if path.startswith('https:'):
+        return httpsrepository(ui, path)
+    return httprepository(ui, path)
--- a/mercurial/localrepo.py	Sun Jul 30 22:52:34 2006 -0700
+++ b/mercurial/localrepo.py	Mon Jul 31 07:11:12 2006 -0700
@@ -2271,3 +2271,8 @@
                     os.path.join(p, "undo.dirstate"))
     return a
 
+def instance(ui, path, create):
+    return localrepository(ui, util.drop_scheme('file', path), create)
+    
+def islocal(path):
+    return True
--- a/mercurial/sshrepo.py	Sun Jul 30 22:52:34 2006 -0700
+++ b/mercurial/sshrepo.py	Mon Jul 31 07:11:12 2006 -0700
@@ -204,3 +204,5 @@
 
     def stream_out(self):
         return self.do_cmd('stream_out')
+
+instance = sshrepository
--- a/mercurial/statichttprepo.py	Sun Jul 30 22:52:34 2006 -0700
+++ b/mercurial/statichttprepo.py	Mon Jul 31 07:11:12 2006 -0700
@@ -7,9 +7,10 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import demandload
+from demandload import *
+from i18n import gettext as _
 demandload(globals(), "changelog filelog httprangereader")
-demandload(globals(), "localrepo manifest os urllib urllib2")
+demandload(globals(), "localrepo manifest os urllib urllib2 util")
 
 class rangereader(httprangereader.httprangereader):
     def read(self, size=None):
@@ -50,3 +51,14 @@
 
     def local(self):
         return False
+
+def instance(ui, path, create):
+    if create:
+        raise util.Abort(_('cannot create new static-http repository'))
+    if path.startswith('old-http:'):
+        ui.warn(_("old-http:// syntax is deprecated, "
+                  "please use static-http:// instead\n"))
+        path = path[4:]
+    else:
+        path = path[7:]
+    return statichttprepository(ui, path)
--- a/mercurial/util.py	Sun Jul 30 22:52:34 2006 -0700
+++ b/mercurial/util.py	Mon Jul 31 07:11:12 2006 -0700
@@ -996,3 +996,11 @@
         if nbytes >= divisor * multiplier:
             return format % (nbytes / float(divisor))
     return units[-1][2] % nbytes
+
+def drop_scheme(scheme, path):
+    sc = scheme + ':'
+    if path.startswith(sc):
+        path = path[len(sc):]
+        if path.startswith('//'):
+            path = path[2:]
+    return path