[PATCH 1 of 3] bundle: extend the format of --type to support version and compression

Pierre-Yves David pierre-yves.david at ens-lyon.org
Wed Oct 7 18:54:36 UTC 2015


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1443752160 25200
#      Thu Oct 01 19:16:00 2015 -0700
# Node ID f50488cfed7c74b47fcf14764751af3db3cc2412
# Parent  f71ff13ffcc99ca3f4851de1e1cebbdb4b3de913
bundle: extend the format of --type to support version and compression

We had some basic undocumented support for uncompressed bundle2 support. We now
have an official extensible syntax to specify both format type and compression
(eg: bzip2-v2).

In practice, this changeset introduce the 'v1' and 'v2' identifier to make it
possible to combine format and compression. The default format is still 'v1'.
We'll care about picking 'v1' or 'v2' in regard with general delta in the next
changesets.

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -8,11 +8,11 @@
 from node import hex, bin, nullid, nullrev, short
 from i18n import _
 import os, sys, errno, re, tempfile, cStringIO, shutil
 import util, scmutil, templater, patch, error, templatekw, revlog, copies
 import match as matchmod
-import repair, graphmod, revset, phases, obsolete, pathutil, changegroup
+import repair, graphmod, revset, phases, obsolete, pathutil
 import changelog
 import bookmarks
 import encoding
 import formatter
 import crecord as crecordmod
@@ -3329,21 +3329,52 @@ class dirstateguard(object):
                 msg = (_("can't release already inactivated backup: %s")
                        % self._filename)
                 raise util.Abort(msg)
             self._abort()
 
-def parsebundletype(bundletype):
+_bundlecompspecs = {'none': None,
+                    'bzip2': 'BZ',
+                    'gzip': 'GZ',
+                   }
+
+_bundleversionspecs = {'v1': '01',
+                       'v2': '02',
+                       'bundle2': '02', #legacy
+                      }
+
+def parsebundletype(repo, spec):
     """return the internal bundle type to use from a user input
 
     This is parsing user specified bundle type as accepted in:
 
         'hg bundle --type TYPE'.
+
+    It accept format in the form [compression][-version]|[version]
     """
-    btypes = {'none': 'HG10UN',
-              'bzip2': 'HG10BZ',
-              'gzip': 'HG10GZ',
-              'bundle2': 'HG20'}
-    bundletype = btypes.get(bundletype)
-    if bundletype not in changegroup.bundletypes:
+    comp, version = None, None
+
+    if '-' in spec:
+        comp, version = spec.split('-', 1)
+    elif spec in _bundlecompspecs:
+        comp = spec
+    elif spec in _bundleversionspecs:
+        version = spec
+    else:
         raise util.Abort(_('unknown bundle type specified with --type'))
-    return bundletype
-
+
+    if comp is None:
+        comp = 'BZ'
+    else:
+        try:
+            comp = _bundlecompspecs[comp]
+        except KeyError:
+            raise util.Abort(_('unknown bundle type specified with --type'))
+
+    if version is None:
+        version = '01'
+    else:
+        try:
+            version = _bundleversionspecs[version]
+        except KeyError:
+            raise util.Abort(_('unknown bundle type specified with --type'))
+
+    return version, comp
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1217,13 +1217,15 @@ def bundle(ui, repo, fname, dest=None, *
     If you omit the destination repository, then hg assumes the
     destination will have all the nodes you specify with --base
     parameters. To create a bundle containing all changesets, use
     -a/--all (or --base null).
 
-    You can change compression method with the -t/--type option.
-    The available compression methods are: none, bzip2, and
-    gzip (by default, bundles are compressed using bzip2).
+    You can change bundle format with the -t/--type option. You can
+    specify a compression, a bundle version or both using a dash
+    (comp-version). The available compression methods are: none, bzip2,
+    and gzip (by default, bundles are compressed using bzip2). The
+    available format are: v1, v2 (default to v1).
 
     The bundle file can then be transferred using conventional means
     and applied to another repository with the unbundle or pull
     command. This is useful when direct push and pull are not
     available or when exporting an entire repository is undesirable.
@@ -1236,11 +1238,11 @@ def bundle(ui, repo, fname, dest=None, *
     revs = None
     if 'rev' in opts:
         revs = scmutil.revrange(repo, opts['rev'])
 
     bundletype = opts.get('type', 'bzip2').lower()
-    bundletype = cmdutil.parsebundletype(bundletype)
+    cgversion, bcompression = cmdutil.parsebundletype(repo, bundletype)
 
     if opts.get('all'):
         base = ['null']
     else:
         base = scmutil.revrange(repo, opts.get('base'))
@@ -1251,11 +1253,12 @@ def bundle(ui, repo, fname, dest=None, *
             raise util.Abort(_("--base is incompatible with specifying "
                                "a destination"))
         common = [repo.lookup(rev) for rev in base]
         heads = revs and map(repo.lookup, revs) or revs
         cg = changegroup.getchangegroup(repo, 'bundle', heads=heads,
-                                         common=common, bundlecaps=bundlecaps)
+                                         common=common, bundlecaps=bundlecaps,
+                                         version=cgversion)
         outgoing = None
     else:
         dest = ui.expandpath(dest or 'default-push', dest or 'default')
         dest, branches = hg.parseurl(dest, opts.get('branch'))
         other = hg.peer(repo, opts, dest)
@@ -1264,16 +1267,26 @@ def bundle(ui, repo, fname, dest=None, *
         outgoing = discovery.findcommonoutgoing(repo, other,
                                                 onlyheads=heads,
                                                 force=opts.get('force'),
                                                 portable=True)
         cg = changegroup.getlocalchangegroup(repo, 'bundle', outgoing,
-                                             bundlecaps)
+                                                bundlecaps, version=cgversion)
     if not cg:
         scmutil.nochangesfound(ui, repo, outgoing and outgoing.excluded)
         return 1
 
-    changegroup.writebundle(ui, cg, fname, bundletype)
+    if cgversion == '01': #bundle1
+        if bcompression is None:
+            bcompression = 'UN'
+        bversion = 'HG10' + bcompression
+        bcompression = None
+    else:
+        assert cgversion == '02'
+        bversion = 'HG20'
+
+
+    changegroup.writebundle(ui, cg, fname, bversion, compression=bcompression)
 
 @command('cat',
     [('o', 'output', '',
      _('print output to file with formatted name'), _('FORMAT')),
     ('r', 'rev', '', _('print the given revision'), _('REV')),
diff --git a/tests/test-bundle-type.t b/tests/test-bundle-type.t
--- a/tests/test-bundle-type.t
+++ b/tests/test-bundle-type.t
@@ -27,11 +27,11 @@ bundle w/o type option
   summary:     a
   $ cd ..
 
 test bundle types
 
-  $ for t in "None" "bzip2" "gzip"; do
+  $ for t in "None" "bzip2" "gzip" "none-v2" "v2" "v1" "gzip-v1"; do
   >   echo % test bundle type $t
   >   hg init t$t
   >   cd t1
   >   hg bundle -t $t ../b$t ../t$t
   >   cut -b 1-6 ../b$t | head -n 1
@@ -56,10 +56,38 @@ test bundle types
   searching for changes
   1 changesets found
   HG10GZ
   c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
   
+  % test bundle type none-v2
+  searching for changes
+  1 changesets found
+  HG20\x00\x00 (esc)
+  Stream params: {}
+  changegroup -- "{'version': '01'}"
+      c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
+  
+  % test bundle type v2
+  searching for changes
+  1 changesets found
+  HG20\x00\x00 (esc)
+  Stream params: {'Compression': 'BZ'}
+  changegroup -- "{'version': '01'}"
+      c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
+  
+  % test bundle type v1
+  searching for changes
+  1 changesets found
+  HG10BZ
+  c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
+  
+  % test bundle type gzip-v1
+  searching for changes
+  1 changesets found
+  HG10GZ
+  c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
+  
 
 test garbage file
 
   $ echo garbage > bgarbage
   $ hg init tgarbage


More information about the Mercurial-devel mailing list