changeset 49334:6d15a8971e30

bundlespec: fix the generation of bundlespec for `cg.version` If the value is non-default, we display it.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Wed, 18 May 2022 12:07:50 +0100
parents 1fd7520e4961
children 04cdb442a892
files mercurial/exchange.py tests/test-bundle-type.t
diffstat 2 files changed, 22 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/exchange.py	Wed May 18 11:55:21 2022 +0100
+++ b/mercurial/exchange.py	Wed May 18 12:07:50 2022 +0100
@@ -80,6 +80,14 @@
         )
 
 
+def _format_params(params):
+    parts = []
+    for key, value in sorted(params.items()):
+        value = urlreq.quote(value)
+        parts.append(b"%s=%s" % (key, value))
+    return b';'.join(parts)
+
+
 def getbundlespec(ui, fh):
     """Infer the bundlespec from a bundle file handle.
 
@@ -93,6 +101,8 @@
         except KeyError:
             return None
 
+    params = {}
+
     b = readbundle(ui, fh, None)
     if isinstance(b, changegroup.cg1unpacker):
         alg = b._type
@@ -115,9 +125,12 @@
         version = None
         for part in b.iterparts():
             if part.type == b'changegroup':
-                version = part.params[b'version']
-                if version in (b'01', b'02'):
+                cgversion = part.params[b'version']
+                if cgversion in (b'01', b'02'):
                     version = b'v2'
+                elif cgversion in (b'03',):
+                    version = b'v2'
+                    params[b'cg.version'] = cgversion
                 else:
                     raise error.Abort(
                         _(
@@ -138,8 +151,12 @@
             raise error.Abort(
                 _(b'could not identify changegroup version in bundle')
             )
-
-        return b'%s-%s' % (comp, version)
+        spec = b'%s-%s' % (comp, version)
+        if params:
+            spec += b';'
+            spec += _format_params(params)
+        return spec
+
     elif isinstance(b, streamclone.streamcloneapplier):
         requirements = streamclone.readbundle1header(fh)[2]
         formatted = bundle2._formatrequirementsparams(requirements)
--- a/tests/test-bundle-type.t	Wed May 18 11:55:21 2022 +0100
+++ b/tests/test-bundle-type.t	Wed May 18 12:07:50 2022 +0100
@@ -265,6 +265,4 @@
   changegroup -- {nbchanges: 1, version: 03} (mandatory: True)
       c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
   $ hg debugbundle ./v2-cg-03.hg --spec
-  abort: changegroup version 03 does not have a known bundlespec (known-bad-output !)
-  (try upgrading your Mercurial client) (known-bad-output !)
-  [255]
+  bzip2-v2;cg.version=03