# HG changeset patch # User Vadim Gelfer # Date 1154355072 25200 # Node ID 386f04d6ecb3b852ad77182b9f152ab7c990ae36 # Parent 3248aa10b388e237fe828712600069544c1db9f3 clean up hg.py: move repo constructor code into each repo module diff --git a/mercurial/bundlerepo.py b/mercurial/bundlerepo.py --- a/mercurial/bundlerepo.py +++ b/mercurial/bundlerepo.py @@ -237,3 +237,18 @@ class bundlerepository(localrepo.localre 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) diff --git a/mercurial/hg.py b/mercurial/hg.py --- a/mercurial/hg.py +++ b/mercurial/hg.py @@ -12,86 +12,43 @@ from i18n import gettext as _ 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''' diff --git a/mercurial/httprepo.py b/mercurial/httprepo.py --- a/mercurial/httprepo.py +++ b/mercurial/httprepo.py @@ -339,3 +339,13 @@ class httpsrepository(httprepository): 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) diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -2271,3 +2271,8 @@ def aftertrans(base): 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 diff --git a/mercurial/sshrepo.py b/mercurial/sshrepo.py --- a/mercurial/sshrepo.py +++ b/mercurial/sshrepo.py @@ -204,3 +204,5 @@ class sshrepository(remoterepository): def stream_out(self): return self.do_cmd('stream_out') + +instance = sshrepository diff --git a/mercurial/statichttprepo.py b/mercurial/statichttprepo.py --- a/mercurial/statichttprepo.py +++ b/mercurial/statichttprepo.py @@ -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 @@ class statichttprepository(localrepo.loc 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) diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -996,3 +996,11 @@ def bytecount(nbytes): 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