comparison mercurial/hg.py @ 14463:81f559d1b9b2

hg: remove underscores in clone function
author Martin Geisler <mg@aragost.com>
date Fri, 27 May 2011 12:42:36 +0200
parents f90d5641c78b
children 3417954c42e9
comparison
equal deleted inserted replaced
14462:f6a433671c06 14463:81f559d1b9b2
207 """ 207 """
208 208
209 if isinstance(source, str): 209 if isinstance(source, str):
210 origsource = ui.expandpath(source) 210 origsource = ui.expandpath(source)
211 source, branch = parseurl(origsource, branch) 211 source, branch = parseurl(origsource, branch)
212 src_repo = repository(ui, source) 212 srcrepo = repository(ui, source)
213 else: 213 else:
214 src_repo = source 214 srcrepo = source
215 branch = (None, branch or []) 215 branch = (None, branch or [])
216 origsource = source = src_repo.url() 216 origsource = source = srcrepo.url()
217 rev, checkout = addbranchrevs(src_repo, src_repo, branch, rev) 217 rev, checkout = addbranchrevs(srcrepo, srcrepo, branch, rev)
218 218
219 if dest is None: 219 if dest is None:
220 dest = defaultdest(source) 220 dest = defaultdest(source)
221 ui.status(_("destination directory: %s\n") % dest) 221 ui.status(_("destination directory: %s\n") % dest)
222 else: 222 else:
239 self.dir_ = None 239 self.dir_ = None
240 def cleanup(self): 240 def cleanup(self):
241 if self.dir_: 241 if self.dir_:
242 self.rmtree(self.dir_, True) 242 self.rmtree(self.dir_, True)
243 243
244 src_lock = dest_lock = dir_cleanup = None 244 srclock = destlock = dircleanup = None
245 try: 245 try:
246 abspath = origsource 246 abspath = origsource
247 if islocal(origsource): 247 if islocal(origsource):
248 abspath = os.path.abspath(util.localpath(origsource)) 248 abspath = os.path.abspath(util.localpath(origsource))
249 249
250 if islocal(dest): 250 if islocal(dest):
251 dir_cleanup = DirCleanup(dest) 251 dircleanup = DirCleanup(dest)
252 252
253 copy = False 253 copy = False
254 if src_repo.cancopy() and islocal(dest): 254 if srcrepo.cancopy() and islocal(dest):
255 copy = not pull and not rev 255 copy = not pull and not rev
256 256
257 if copy: 257 if copy:
258 try: 258 try:
259 # we use a lock here because if we race with commit, we 259 # we use a lock here because if we race with commit, we
260 # can end up with extra data in the cloned revlogs that's 260 # can end up with extra data in the cloned revlogs that's
261 # not pointed to by changesets, thus causing verify to 261 # not pointed to by changesets, thus causing verify to
262 # fail 262 # fail
263 src_lock = src_repo.lock(wait=False) 263 srclock = srcrepo.lock(wait=False)
264 except error.LockError: 264 except error.LockError:
265 copy = False 265 copy = False
266 266
267 if copy: 267 if copy:
268 src_repo.hook('preoutgoing', throw=True, source='clone') 268 srcrepo.hook('preoutgoing', throw=True, source='clone')
269 hgdir = os.path.realpath(os.path.join(dest, ".hg")) 269 hgdir = os.path.realpath(os.path.join(dest, ".hg"))
270 if not os.path.exists(dest): 270 if not os.path.exists(dest):
271 os.mkdir(dest) 271 os.mkdir(dest)
272 else: 272 else:
273 # only clean up directories we create ourselves 273 # only clean up directories we create ourselves
274 dir_cleanup.dir_ = hgdir 274 dircleanup.dir_ = hgdir
275 try: 275 try:
276 dest_path = hgdir 276 destpath = hgdir
277 util.makedir(dest_path, notindexed=True) 277 util.makedir(destpath, notindexed=True)
278 except OSError, inst: 278 except OSError, inst:
279 if inst.errno == errno.EEXIST: 279 if inst.errno == errno.EEXIST:
280 dir_cleanup.close() 280 dircleanup.close()
281 raise util.Abort(_("destination '%s' already exists") 281 raise util.Abort(_("destination '%s' already exists")
282 % dest) 282 % dest)
283 raise 283 raise
284 284
285 hardlink = None 285 hardlink = None
286 num = 0 286 num = 0
287 for f in src_repo.store.copylist(): 287 for f in srcrepo.store.copylist():
288 src = os.path.join(src_repo.sharedpath, f) 288 src = os.path.join(srcrepo.sharedpath, f)
289 dst = os.path.join(dest_path, f) 289 dst = os.path.join(destpath, f)
290 dstbase = os.path.dirname(dst) 290 dstbase = os.path.dirname(dst)
291 if dstbase and not os.path.exists(dstbase): 291 if dstbase and not os.path.exists(dstbase):
292 os.mkdir(dstbase) 292 os.mkdir(dstbase)
293 if os.path.exists(src): 293 if os.path.exists(src):
294 if dst.endswith('data'): 294 if dst.endswith('data'):
295 # lock to avoid premature writing to the target 295 # lock to avoid premature writing to the target
296 dest_lock = lock.lock(os.path.join(dstbase, "lock")) 296 destlock = lock.lock(os.path.join(dstbase, "lock"))
297 hardlink, n = util.copyfiles(src, dst, hardlink) 297 hardlink, n = util.copyfiles(src, dst, hardlink)
298 num += n 298 num += n
299 if hardlink: 299 if hardlink:
300 ui.debug("linked %d files\n" % num) 300 ui.debug("linked %d files\n" % num)
301 else: 301 else:
302 ui.debug("copied %d files\n" % num) 302 ui.debug("copied %d files\n" % num)
303 303
304 # we need to re-init the repo after manually copying the data 304 # we need to re-init the repo after manually copying the data
305 # into it 305 # into it
306 dest_repo = repository(ui, dest) 306 destrepo = repository(ui, dest)
307 src_repo.hook('outgoing', source='clone', 307 srcrepo.hook('outgoing', source='clone',
308 node=node.hex(node.nullid)) 308 node=node.hex(node.nullid))
309 else: 309 else:
310 try: 310 try:
311 dest_repo = repository(ui, dest, create=True) 311 destrepo = repository(ui, dest, create=True)
312 except OSError, inst: 312 except OSError, inst:
313 if inst.errno == errno.EEXIST: 313 if inst.errno == errno.EEXIST:
314 dir_cleanup.close() 314 dircleanup.close()
315 raise util.Abort(_("destination '%s' already exists") 315 raise util.Abort(_("destination '%s' already exists")
316 % dest) 316 % dest)
317 raise 317 raise
318 318
319 revs = None 319 revs = None
320 if rev: 320 if rev:
321 if 'lookup' not in src_repo.capabilities: 321 if 'lookup' not in srcrepo.capabilities:
322 raise util.Abort(_("src repository does not support " 322 raise util.Abort(_("src repository does not support "
323 "revision lookup and so doesn't " 323 "revision lookup and so doesn't "
324 "support clone by revision")) 324 "support clone by revision"))
325 revs = [src_repo.lookup(r) for r in rev] 325 revs = [srcrepo.lookup(r) for r in rev]
326 checkout = revs[0] 326 checkout = revs[0]
327 if dest_repo.local(): 327 if destrepo.local():
328 dest_repo.clone(src_repo, heads=revs, stream=stream) 328 destrepo.clone(srcrepo, heads=revs, stream=stream)
329 elif src_repo.local(): 329 elif srcrepo.local():
330 src_repo.push(dest_repo, revs=revs) 330 srcrepo.push(destrepo, revs=revs)
331 else: 331 else:
332 raise util.Abort(_("clone from remote to remote not supported")) 332 raise util.Abort(_("clone from remote to remote not supported"))
333 333
334 if dir_cleanup: 334 if dircleanup:
335 dir_cleanup.close() 335 dircleanup.close()
336 336
337 if dest_repo.local(): 337 if destrepo.local():
338 fp = dest_repo.opener("hgrc", "w", text=True) 338 fp = destrepo.opener("hgrc", "w", text=True)
339 fp.write("[paths]\n") 339 fp.write("[paths]\n")
340 fp.write("default = %s\n" % abspath) 340 fp.write("default = %s\n" % abspath)
341 fp.close() 341 fp.close()
342 342
343 dest_repo.ui.setconfig('paths', 'default', abspath) 343 destrepo.ui.setconfig('paths', 'default', abspath)
344 344
345 if update: 345 if update:
346 if update is not True: 346 if update is not True:
347 checkout = update 347 checkout = update
348 if src_repo.local(): 348 if srcrepo.local():
349 checkout = src_repo.lookup(update) 349 checkout = srcrepo.lookup(update)
350 for test in (checkout, 'default', 'tip'): 350 for test in (checkout, 'default', 'tip'):
351 if test is None: 351 if test is None:
352 continue 352 continue
353 try: 353 try:
354 uprev = dest_repo.lookup(test) 354 uprev = destrepo.lookup(test)
355 break 355 break
356 except error.RepoLookupError: 356 except error.RepoLookupError:
357 continue 357 continue
358 bn = dest_repo[uprev].branch() 358 bn = destrepo[uprev].branch()
359 dest_repo.ui.status(_("updating to branch %s\n") % bn) 359 destrepo.ui.status(_("updating to branch %s\n") % bn)
360 _update(dest_repo, uprev) 360 _update(destrepo, uprev)
361 361
362 # clone all bookmarks 362 # clone all bookmarks
363 if dest_repo.local() and src_repo.capable("pushkey"): 363 if destrepo.local() and srcrepo.capable("pushkey"):
364 rb = src_repo.listkeys('bookmarks') 364 rb = srcrepo.listkeys('bookmarks')
365 for k, n in rb.iteritems(): 365 for k, n in rb.iteritems():
366 try: 366 try:
367 m = dest_repo.lookup(n) 367 m = destrepo.lookup(n)
368 dest_repo._bookmarks[k] = m 368 destrepo._bookmarks[k] = m
369 except error.RepoLookupError: 369 except error.RepoLookupError:
370 pass 370 pass
371 if rb: 371 if rb:
372 bookmarks.write(dest_repo) 372 bookmarks.write(destrepo)
373 elif src_repo.local() and dest_repo.capable("pushkey"): 373 elif srcrepo.local() and destrepo.capable("pushkey"):
374 for k, n in src_repo._bookmarks.iteritems(): 374 for k, n in srcrepo._bookmarks.iteritems():
375 dest_repo.pushkey('bookmarks', k, '', hex(n)) 375 destrepo.pushkey('bookmarks', k, '', hex(n))
376 376
377 return src_repo, dest_repo 377 return srcrepo, destrepo
378 finally: 378 finally:
379 release(src_lock, dest_lock) 379 release(srclock, destlock)
380 if dir_cleanup is not None: 380 if dircleanup is not None:
381 dir_cleanup.cleanup() 381 dircleanup.cleanup()
382 382
383 def _showstats(repo, stats): 383 def _showstats(repo, stats):
384 repo.ui.status(_("%d files updated, %d files merged, " 384 repo.ui.status(_("%d files updated, %d files merged, "
385 "%d files removed, %d files unresolved\n") % stats) 385 "%d files removed, %d files unresolved\n") % stats)
386 386