Patchwork [4,of,6] bundle: introduce an higher level function to write bundle on disk

mail settings
Submitter Pierre-Yves David
Date May 6, 2017, 8:04 a.m.
Message ID <>
Download mbox | patch
Permalink /patch/20482/
State Accepted
Headers show


Pierre-Yves David - May 6, 2017, 8:04 a.m.
# HG changeset patch
# User Pierre-Yves David <>
# Date 1493996987 -7200
#      Fri May 05 17:09:47 2017 +0200
# Node ID 54c7d588194032831969d2f68eec650ac2d2ff40
# Parent  ee3c4426ef5dde60a48c4133d65234ed96c832de
# EXP-Topic bundle.cleanup
# Available At
#              hg pull -r 54c7d5881940
bundle: introduce an higher level function to write bundle on disk

The current function ('writebundle') is focussing on getting an existing
changegroup to disk. It is no easy ways to includes more part in the generated
bundle2. So we introduce a slightly higher level function that is fed the
'outgoing' object (that defines the bundled spec) and the bundlespec parameters
(to control the changegroup generation and inclusion of other parts).

This is creating the third logic dedicated to create a consistent bundle2 (the
other 2 are the push code and the getbundle code). We should probably reconcile
them at some points but they all takes different types of input. So we need to
introduce an intermediate "object" that each different input could be converted
to. Such unified "bundle2 specification" could be fed to some unified code.

We start by having the `hg bundle` related code on its own to helps defines its
specific needs first. Once the common and specific parts of each logic will be
known we can start unification.


diff --git a/mercurial/ b/mercurial/
--- a/mercurial/
+++ b/mercurial/
@@ -1342,6 +1342,42 @@  def obsmarkersversion(caps):
     obscaps = caps.get('obsmarkers', ())
     return [int(c[1:]) for c in obscaps if c.startswith('V')]
+def writenewbundle(ui, repo, source, filename, bundletype, outgoing, opts,
+                   vfs=None, compression=None, compopts=None):
+    if bundletype.startswith('HG10'):
+        cg = changegroup.getchangegroup(repo, source, outgoing, version='01')
+        return writebundle(ui, cg, filename, bundletype, vfs=vfs,
+                           compression=compression, compopts=compopts)
+    elif not bundletype.startswith('HG20'):
+        raise error.ProgrammingError('unknown bundle type: %s' % bundletype)
+    bundle = bundle20(ui)
+    bundle.setcompression(compression, compopts)
+    _addpartsfromopts(ui, repo, bundle, source, outgoing, opts)
+    chunkiter = bundle.getchunks()
+    return changegroup.writechunks(ui, chunkiter, filename, vfs=vfs)
+def _addpartsfromopts(ui, repo, bundler, source, outgoing, opts):
+    # We should eventually reconcile this logic with the one behind
+    # 'exchange.getbundle2partsgenerator'.
+    #
+    # The type of input from 'getbundle' and 'writenewbundle' are a bit
+    # different right now. So we keep them separated for now for the sake of
+    # simplicity.
+    # we always want a changegroup in such bundle
+    cgversion = opts.get('cg.version')
+    if cgversion is None:
+        cgversion = changegroup.safeversion(repo)
+    cg = changegroup.getchangegroup(repo, source, outgoing,
+                                    version=cgversion)
+    part = bundler.newpart('changegroup', data=cg.getchunks())
+    part.addparam('version', cg.version)
+    if 'clcount' in cg.extras:
+        part.addparam('nbchanges', str(cg.extras['clcount']),
+                      mandatory=False)
 def writebundle(ui, cg, filename, bundletype, vfs=None, compression=None,
     """Write a bundle file and return its filename.
diff --git a/mercurial/ b/mercurial/
--- a/mercurial/
+++ b/mercurial/
@@ -1392,10 +1392,11 @@  def bundle(ui, repo, fname, dest=None, *
     if complevel is not None:
         compopts['level'] = complevel
-    cg = changegroup.getchangegroup(repo, 'bundle', outgoing, version=cgversion)
-    bundle2.writebundle(ui, cg, fname, bversion, compression=bcompression,
-                        compopts=compopts)
+    contentopts = {'cg.version': cgversion}
+    bundle2.writenewbundle(ui, repo, 'bundle', fname, bversion, outgoing,
+                           contentopts, compression=bcompression,
+                           compopts=compopts)
     [('o', 'output', '',