# HG changeset patch # User Benoit Boissinot # Date 1142218463 -3600 # Node ID 8198c60f79143cb614b7d23b33c9e4edaf2d43cd # Parent 9da45de3118dda17c730889aac11574ce6240fbd refactor the bundle writing code, since we will reuse it later diff -r 9da45de3118d -r 8198c60f7914 mercurial/commands.py --- a/mercurial/commands.py Mon Mar 13 03:54:23 2006 +0100 +++ b/mercurial/commands.py Mon Mar 13 03:54:23 2006 +0100 @@ -274,6 +274,32 @@ pathname), mode) +def write_bundle(cg, filename, compress=True, fh=None): + if fh is None: + fh = open(filename, "wb") + + class nocompress(object): + def compress(self, x): + return x + def flush(self): + return "" + try: + if compress: + fh.write("HG10") + z = bz2.BZ2Compressor(9) + else: + fh.write("HG11") + z = nocompress() + while 1: + chunk = cg.read(4096) + if not chunk: + break + fh.write(z.compress(chunk)) + fh.write(z.flush()) + except: + os.unlink(filename) + raise + def dodiff(fp, ui, repo, node1, node2, files=None, match=util.always, changes=None, text=False, opts={}): if not node1: @@ -830,24 +856,11 @@ Unlike import/export, this exactly preserves all changeset contents including permissions, rename data, and revision history. """ - f = open(fname, "wb") dest = ui.expandpath(dest) other = hg.repository(ui, dest) o = repo.findoutgoing(other) cg = repo.changegroup(o, 'bundle') - - try: - f.write("HG10") - z = bz2.BZ2Compressor(9) - while 1: - chunk = cg.read(4096) - if not chunk: - break - f.write(z.compress(chunk)) - f.write(z.flush()) - except: - os.unlink(fname) - raise + write_bundle(cg, fname) def cat(ui, repo, file1, *pats, **opts): """output the latest or given revisions of files