comparison mercurial/hg.py @ 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 400a4a502001
children b1b1aa6f275c
comparison
equal deleted inserted replaced
2739:3248aa10b388 2740:386f04d6ecb3
10 from demandload import * 10 from demandload import *
11 from i18n import gettext as _ 11 from i18n import gettext as _
12 demandload(globals(), "localrepo bundlerepo httprepo sshrepo statichttprepo") 12 demandload(globals(), "localrepo bundlerepo httprepo sshrepo statichttprepo")
13 demandload(globals(), "errno lock os shutil util") 13 demandload(globals(), "errno lock os shutil util")
14 14
15 def bundle(ui, path): 15 def _local(path):
16 if path.startswith('bundle://'): 16 return os.path.isfile(util.drop_scheme('file', path)) and bundlerepo or localrepo
17 path = path[9:]
18 else:
19 path = path[7:]
20 s = path.split("+", 1)
21 if len(s) == 1:
22 repopath, bundlename = "", s[0]
23 else:
24 repopath, bundlename = s
25 return bundlerepo.bundlerepository(ui, repopath, bundlename)
26
27 def hg(ui, path):
28 ui.warn(_("hg:// syntax is deprecated, please use http:// instead\n"))
29 return httprepo.httprepository(ui, path.replace("hg://", "http://"))
30
31 def local_(ui, path, create=0):
32 if path.startswith('file:'):
33 path = path[5:]
34 if not create and os.path.isfile(path):
35 return bundlerepo.bundlerepository(ui, '', path)
36 return localrepo.localrepository(ui, path, create)
37
38 def ssh_(ui, path, create=0):
39 return sshrepo.sshrepository(ui, path, create)
40
41 def old_http(ui, path):
42 ui.warn(_("old-http:// syntax is deprecated, "
43 "please use static-http:// instead\n"))
44 return statichttprepo.statichttprepository(
45 ui, path.replace("old-http://", "http://"))
46
47 def static_http(ui, path):
48 return statichttprepo.statichttprepository(
49 ui, path.replace("static-http://", "http://"))
50 17
51 schemes = { 18 schemes = {
52 'bundle': bundle, 19 'bundle': bundlerepo,
53 'file': local_, 20 'file': _local,
54 'hg': hg, 21 'hg': httprepo,
55 'http': lambda ui, path: httprepo.httprepository(ui, path), 22 'http': httprepo,
56 'https': lambda ui, path: httprepo.httpsrepository(ui, path), 23 'https': httprepo,
57 'old-http': old_http, 24 'old-http': statichttprepo,
58 'ssh': ssh_, 25 'ssh': sshrepo,
59 'static-http': static_http, 26 'static-http': statichttprepo,
60 } 27 }
61 28
62 remote_schemes = [ 29 def _lookup(path):
63 'bundle', 30 scheme = 'file'
64 'hg', 31 if path:
65 'http', 32 c = path.find(':')
66 'https', 33 if c > 0:
67 'old-http', 34 scheme = path[:c]
68 'ssh', 35 thing = schemes.get(scheme) or schemes['file']
69 'static-http', 36 try:
70 ] 37 return thing(path)
71 38 except TypeError:
39 return thing
40
72 def islocal(repo): 41 def islocal(repo):
73 '''return true if repo or path is local''' 42 '''return true if repo or path is local'''
74 if isinstance(repo, str): 43 if isinstance(repo, str):
75 c = repo.find(':') 44 try:
76 return c <= 0 or repo[:c] not in remote_schemes 45 return _lookup(repo).islocal(repo)
46 except AttributeError:
47 return False
77 return repo.local() 48 return repo.local()
78 49
79 def repository(ui, path=None, create=0): 50 def repository(ui, path=None, create=False):
80 scheme = None 51 return _lookup(path).instance(ui, path, create)
81 if path:
82 c = path.find(':')
83 if c > 0:
84 scheme = schemes.get(path[:c])
85 else:
86 path = ''
87 ctor = scheme or schemes['file']
88 if create:
89 try:
90 return ctor(ui, path, create)
91 except TypeError:
92 raise util.Abort(_('cannot create new repository over "%s" protocol') %
93 scheme)
94 return ctor(ui, path)
95 52
96 def defaultdest(source): 53 def defaultdest(source):
97 '''return default destination of clone if none is given''' 54 '''return default destination of clone if none is given'''
98 return os.path.basename(os.path.normpath(source)) 55 return os.path.basename(os.path.normpath(source))
99 56