##// END OF EJS Templates
clone: fall back to pull source repo cannot be locked, 937ee88da3ef was a noop...
Benoit Boissinot -
r8649:2c097e22 default
parent child Browse files
Show More
@@ -1,299 +1,299
1 1 # hg.py - repository classes for mercurial
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2, incorporated herein by reference.
8 8
9 9 from i18n import _
10 10 from lock import release
11 11 import localrepo, bundlerepo, httprepo, sshrepo, statichttprepo
12 12 import lock, util, extensions, error
13 13 import merge as _merge
14 14 import verify as _verify
15 15 import errno, os, shutil
16 16
17 17 def _local(path):
18 18 return (os.path.isfile(util.drop_scheme('file', path)) and
19 19 bundlerepo or localrepo)
20 20
21 21 def parseurl(url, revs=[]):
22 22 '''parse url#branch, returning url, branch + revs'''
23 23
24 24 if '#' not in url:
25 25 return url, (revs or None), revs and revs[-1] or None
26 26
27 27 url, branch = url.split('#', 1)
28 28 checkout = revs and revs[-1] or branch
29 29 return url, (revs or []) + [branch], checkout
30 30
31 31 schemes = {
32 32 'bundle': bundlerepo,
33 33 'file': _local,
34 34 'http': httprepo,
35 35 'https': httprepo,
36 36 'ssh': sshrepo,
37 37 'static-http': statichttprepo,
38 38 }
39 39
40 40 def _lookup(path):
41 41 scheme = 'file'
42 42 if path:
43 43 c = path.find(':')
44 44 if c > 0:
45 45 scheme = path[:c]
46 46 thing = schemes.get(scheme) or schemes['file']
47 47 try:
48 48 return thing(path)
49 49 except TypeError:
50 50 return thing
51 51
52 52 def islocal(repo):
53 53 '''return true if repo or path is local'''
54 54 if isinstance(repo, str):
55 55 try:
56 56 return _lookup(repo).islocal(repo)
57 57 except AttributeError:
58 58 return False
59 59 return repo.local()
60 60
61 61 def repository(ui, path='', create=False):
62 62 """return a repository object for the specified path"""
63 63 repo = _lookup(path).instance(ui, path, create)
64 64 ui = getattr(repo, "ui", ui)
65 65 for name, module in extensions.extensions():
66 66 hook = getattr(module, 'reposetup', None)
67 67 if hook:
68 68 hook(ui, repo)
69 69 return repo
70 70
71 71 def defaultdest(source):
72 72 '''return default destination of clone if none is given'''
73 73 return os.path.basename(os.path.normpath(source))
74 74
75 75 def localpath(path):
76 76 if path.startswith('file://localhost/'):
77 77 return path[16:]
78 78 if path.startswith('file://'):
79 79 return path[7:]
80 80 if path.startswith('file:'):
81 81 return path[5:]
82 82 return path
83 83
84 84 def clone(ui, source, dest=None, pull=False, rev=None, update=True,
85 85 stream=False):
86 86 """Make a copy of an existing repository.
87 87
88 88 Create a copy of an existing repository in a new directory. The
89 89 source and destination are URLs, as passed to the repository
90 90 function. Returns a pair of repository objects, the source and
91 91 newly created destination.
92 92
93 93 The location of the source is added to the new repository's
94 94 .hg/hgrc file, as the default to be used for future pulls and
95 95 pushes.
96 96
97 97 If an exception is raised, the partly cloned/updated destination
98 98 repository will be deleted.
99 99
100 100 Arguments:
101 101
102 102 source: repository object or URL
103 103
104 104 dest: URL of destination repository to create (defaults to base
105 105 name of source repository)
106 106
107 107 pull: always pull from source repository, even in local case
108 108
109 109 stream: stream raw data uncompressed from repository (fast over
110 110 LAN, slow over WAN)
111 111
112 112 rev: revision to clone up to (implies pull=True)
113 113
114 114 update: update working directory after clone completes, if
115 115 destination is local repository (True means update to default rev,
116 116 anything else is treated as a revision)
117 117 """
118 118
119 119 if isinstance(source, str):
120 120 origsource = ui.expandpath(source)
121 121 source, rev, checkout = parseurl(origsource, rev)
122 122 src_repo = repository(ui, source)
123 123 else:
124 124 src_repo = source
125 125 origsource = source = src_repo.url()
126 126 checkout = rev and rev[-1] or None
127 127
128 128 if dest is None:
129 129 dest = defaultdest(source)
130 130 ui.status(_("destination directory: %s\n") % dest)
131 131
132 132 dest = localpath(dest)
133 133 source = localpath(source)
134 134
135 135 if os.path.exists(dest):
136 136 if not os.path.isdir(dest):
137 137 raise util.Abort(_("destination '%s' already exists") % dest)
138 138 elif os.listdir(dest):
139 139 raise util.Abort(_("destination '%s' is not empty") % dest)
140 140
141 141 class DirCleanup(object):
142 142 def __init__(self, dir_):
143 143 self.rmtree = shutil.rmtree
144 144 self.dir_ = dir_
145 145 def close(self):
146 146 self.dir_ = None
147 147 def cleanup(self):
148 148 if self.dir_:
149 149 self.rmtree(self.dir_, True)
150 150
151 151 src_lock = dest_lock = dir_cleanup = None
152 152 try:
153 153 if islocal(dest):
154 154 dir_cleanup = DirCleanup(dest)
155 155
156 156 abspath = origsource
157 157 copy = False
158 158 if src_repo.cancopy() and islocal(dest):
159 159 abspath = os.path.abspath(util.drop_scheme('file', origsource))
160 160 copy = not pull and not rev
161 161
162 162 if copy:
163 163 try:
164 164 # we use a lock here because if we race with commit, we
165 165 # can end up with extra data in the cloned revlogs that's
166 166 # not pointed to by changesets, thus causing verify to
167 167 # fail
168 src_lock = src_repo.lock()
168 src_lock = src_repo.lock(wait=False)
169 169 except error.LockError:
170 170 copy = False
171 171
172 172 if copy:
173 173 hgdir = os.path.realpath(os.path.join(dest, ".hg"))
174 174 if not os.path.exists(dest):
175 175 os.mkdir(dest)
176 176 else:
177 177 # only clean up directories we create ourselves
178 178 dir_cleanup.dir_ = hgdir
179 179 try:
180 180 dest_path = hgdir
181 181 os.mkdir(dest_path)
182 182 except OSError, inst:
183 183 if inst.errno == errno.EEXIST:
184 184 dir_cleanup.close()
185 185 raise util.Abort(_("destination '%s' already exists")
186 186 % dest)
187 187 raise
188 188
189 189 for f in src_repo.store.copylist():
190 190 src = os.path.join(src_repo.path, f)
191 191 dst = os.path.join(dest_path, f)
192 192 dstbase = os.path.dirname(dst)
193 193 if dstbase and not os.path.exists(dstbase):
194 194 os.mkdir(dstbase)
195 195 if os.path.exists(src):
196 196 if dst.endswith('data'):
197 197 # lock to avoid premature writing to the target
198 198 dest_lock = lock.lock(os.path.join(dstbase, "lock"))
199 199 util.copyfiles(src, dst)
200 200
201 201 # we need to re-init the repo after manually copying the data
202 202 # into it
203 203 dest_repo = repository(ui, dest)
204 204
205 205 else:
206 206 try:
207 207 dest_repo = repository(ui, dest, create=True)
208 208 except OSError, inst:
209 209 if inst.errno == errno.EEXIST:
210 210 dir_cleanup.close()
211 211 raise util.Abort(_("destination '%s' already exists")
212 212 % dest)
213 213 raise
214 214
215 215 revs = None
216 216 if rev:
217 217 if 'lookup' not in src_repo.capabilities:
218 218 raise util.Abort(_("src repository does not support revision "
219 219 "lookup and so doesn't support clone by "
220 220 "revision"))
221 221 revs = [src_repo.lookup(r) for r in rev]
222 222 checkout = revs[0]
223 223 if dest_repo.local():
224 224 dest_repo.clone(src_repo, heads=revs, stream=stream)
225 225 elif src_repo.local():
226 226 src_repo.push(dest_repo, revs=revs)
227 227 else:
228 228 raise util.Abort(_("clone from remote to remote not supported"))
229 229
230 230 if dir_cleanup:
231 231 dir_cleanup.close()
232 232
233 233 if dest_repo.local():
234 234 fp = dest_repo.opener("hgrc", "w", text=True)
235 235 fp.write("[paths]\n")
236 236 fp.write("default = %s\n" % abspath)
237 237 fp.close()
238 238
239 239 if update:
240 240 dest_repo.ui.status(_("updating working directory\n"))
241 241 if update is not True:
242 242 checkout = update
243 243 for test in (checkout, 'default', 'tip'):
244 244 try:
245 245 uprev = dest_repo.lookup(test)
246 246 break
247 247 except:
248 248 continue
249 249 _update(dest_repo, uprev)
250 250
251 251 return src_repo, dest_repo
252 252 finally:
253 253 release(src_lock, dest_lock)
254 254 if dir_cleanup is not None:
255 255 dir_cleanup.cleanup()
256 256
257 257 def _showstats(repo, stats):
258 258 stats = ((stats[0], _("updated")),
259 259 (stats[1], _("merged")),
260 260 (stats[2], _("removed")),
261 261 (stats[3], _("unresolved")))
262 262 note = ", ".join([_("%d files %s") % s for s in stats])
263 263 repo.ui.status("%s\n" % note)
264 264
265 265 def update(repo, node):
266 266 """update the working directory to node, merging linear changes"""
267 267 stats = _merge.update(repo, node, False, False, None)
268 268 _showstats(repo, stats)
269 269 if stats[3]:
270 270 repo.ui.status(_("use 'hg resolve' to retry unresolved file merges\n"))
271 271 return stats[3] > 0
272 272
273 273 # naming conflict in clone()
274 274 _update = update
275 275
276 276 def clean(repo, node, show_stats=True):
277 277 """forcibly switch the working directory to node, clobbering changes"""
278 278 stats = _merge.update(repo, node, False, True, None)
279 279 if show_stats: _showstats(repo, stats)
280 280 return stats[3] > 0
281 281
282 282 def merge(repo, node, force=None, remind=True):
283 283 """branch merge with node, resolving changes"""
284 284 stats = _merge.update(repo, node, True, force, False)
285 285 _showstats(repo, stats)
286 286 if stats[3]:
287 287 repo.ui.status(_("use 'hg resolve' to retry unresolved file merges "
288 288 "or 'hg up --clean' to abandon\n"))
289 289 elif remind:
290 290 repo.ui.status(_("(branch merge, don't forget to commit)\n"))
291 291 return stats[3] > 0
292 292
293 293 def revert(repo, node, choose):
294 294 """revert changes to revision in node without updating dirstate"""
295 295 return _merge.update(repo, node, False, True, choose)[3] > 0
296 296
297 297 def verify(repo):
298 298 """verify the consistency of a repository"""
299 299 return _verify.verify(repo)
General Comments 0
You need to be logged in to leave comments. Login now