# HG changeset patch # User Pierre-Yves David # Date 2017-05-05 15:09:47 # Node ID 9dc36df7840349c7399fd7f2925d0c9d0b9a7997 # Parent e17b8466857e429d4f1261bf4c59056fc7046c74 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/bundle2.py b/mercurial/bundle2.py --- a/mercurial/bundle2.py +++ b/mercurial/bundle2.py @@ -1339,6 +1339,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, compopts=None): """Write a bundle file and return its filename. diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -1393,10 +1393,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) @command('cat', [('o', 'output', '',