comparison mercurial/commands.py @ 1974:0d54675cd566

Delete bundle file of hg incoming in case of errors, preserve existing files. Let write_bundle always create the bundle file, check if target doesn't exist and delete it in case of errors. This way incoming only has to delete the bundle file if it was meant to be a temporary file.
author Thomas Arendsen Hein <thomas@intevation.de>
date Sat, 18 Mar 2006 14:36:45 +0100
parents 9103902a5a8d
children d545fa1426b9
comparison
equal deleted inserted replaced
1973:9103902a5a8d 1974:0d54675cd566
272 return pat 272 return pat
273 return open(make_filename(repo, r, pat, node, total, seqno, revwidth, 273 return open(make_filename(repo, r, pat, node, total, seqno, revwidth,
274 pathname), 274 pathname),
275 mode) 275 mode)
276 276
277 def write_bundle(cg, filename, compress=True, fh=None): 277 def write_bundle(cg, filename=None, compress=True):
278 """Write a bundle file, optionally without bz2 compression. 278 """Write a bundle file and return its filename.
279 279
280 A file handle (fh) may be passed and is guaranteed to be closed. 280 Existing files will not be overwritten.
281 """ 281 If no filename is specified, a temporary file is created.
282 if fh is None: 282 bz2 compression can be turned off.
283 fh = open(filename, "wb") 283 The bundle file will be deleted in case of errors.
284 284 """
285 class nocompress(object): 285 class nocompress(object):
286 def compress(self, x): 286 def compress(self, x):
287 return x 287 return x
288 def flush(self): 288 def flush(self):
289 return "" 289 return ""
290
291 fh = None
292 cleanup = None
290 try: 293 try:
294 if filename:
295 if os.path.exists(filename):
296 raise util.Abort(_("file '%s' already exists"), filename)
297 fh = open(filename, "wb")
298 else:
299 fd, filename = tempfile.mkstemp(suffix=".hg", prefix="hg-bundle-")
300 fh = os.fdopen(fd, "wb")
301 cleanup = filename
302
291 if compress: 303 if compress:
292 fh.write("HG10") 304 fh.write("HG10")
293 z = bz2.BZ2Compressor(9) 305 z = bz2.BZ2Compressor(9)
294 else: 306 else:
295 fh.write("HG11") 307 fh.write("HG11")
298 chunk = cg.read(4096) 310 chunk = cg.read(4096)
299 if not chunk: 311 if not chunk:
300 break 312 break
301 fh.write(z.compress(chunk)) 313 fh.write(z.compress(chunk))
302 fh.write(z.flush()) 314 fh.write(z.flush())
303 except: 315 cleanup = None
304 fh.close() 316 return filename
305 os.unlink(filename) 317 finally:
306 raise 318 if fh is not None:
307 fh.close() 319 fh.close()
320 if cleanup is not None:
321 os.unlink(cleanup)
308 322
309 def dodiff(fp, ui, repo, node1, node2, files=None, match=util.always, 323 def dodiff(fp, ui, repo, node1, node2, files=None, match=util.always,
310 changes=None, text=False, opts={}): 324 changes=None, text=False, opts={}):
311 if not node1: 325 if not node1:
312 node1 = repo.dirstate.parents()[0] 326 node1 = repo.dirstate.parents()[0]
1780 incoming = repo.findincoming(other, force=opts["force"]) 1794 incoming = repo.findincoming(other, force=opts["force"])
1781 if not incoming: 1795 if not incoming:
1782 return 1796 return
1783 1797
1784 cleanup = None 1798 cleanup = None
1785 fname = opts["bundle"] 1799 try:
1786 if fname: 1800 fname = opts["bundle"]
1787 # create a bundle (uncompressed if other repo is not local) 1801 if fname or not other.local():
1788 f = open(fname, "wb") 1802 # create a bundle (uncompressed if other repo is not local)
1789 elif not other.local(): 1803 cg = other.changegroup(incoming, "incoming")
1790 # create an uncompressed temporary bundle 1804 fname = cleanup = write_bundle(cg, fname, compress=other.local())
1791 fd, fname = tempfile.mkstemp(suffix=".hg", prefix="hg-incoming-") 1805 # keep written bundle?
1792 f = os.fdopen(fd, "wb") 1806 if opts["bundle"]:
1793 cleanup = fname 1807 cleanup = None
1794 1808 if not other.local():
1795 if fname: 1809 # use the created uncompressed bundlerepo
1796 cg = other.changegroup(incoming, "incoming") 1810 other = bundlerepo.bundlerepository(ui, repo.root, fname)
1797 write_bundle(cg, fname, compress=other.local(), fh=f) 1811
1798 # write_bundle closed f for us. 1812 o = other.changelog.nodesbetween(incoming)[0]
1799 if not other.local(): 1813 if opts['newest_first']:
1800 # use the created uncompressed bundlerepo 1814 o.reverse()
1801 other = bundlerepo.bundlerepository(ui, repo.root, fname) 1815 displayer = show_changeset(ui, other, opts)
1802 1816 for n in o:
1803 o = other.changelog.nodesbetween(incoming)[0] 1817 parents = [p for p in other.changelog.parents(n) if p != nullid]
1804 if opts['newest_first']: 1818 if opts['no_merges'] and len(parents) == 2:
1805 o.reverse() 1819 continue
1806 displayer = show_changeset(ui, other, opts) 1820 displayer.show(changenode=n)
1807 for n in o: 1821 if opts['patch']:
1808 parents = [p for p in other.changelog.parents(n) if p != nullid] 1822 prev = (parents and parents[0]) or nullid
1809 if opts['no_merges'] and len(parents) == 2: 1823 dodiff(ui, ui, other, prev, n)
1810 continue 1824 ui.write("\n")
1811 displayer.show(changenode=n) 1825 finally:
1812 if opts['patch']: 1826 if hasattr(other, 'close'):
1813 prev = (parents and parents[0]) or nullid 1827 other.close()
1814 dodiff(ui, ui, other, prev, n) 1828 if cleanup:
1815 ui.write("\n") 1829 os.unlink(cleanup)
1816
1817 if cleanup:
1818 other.close() # explicit close for unlink
1819 os.unlink(cleanup)
1820 1830
1821 def init(ui, dest="."): 1831 def init(ui, dest="."):
1822 """create a new repository in the given directory 1832 """create a new repository in the given directory
1823 1833
1824 Initialize a new repository in the given directory. If the given 1834 Initialize a new repository in the given directory. If the given