bundlespec: introduce an attr-based class for bundlespec
authorBoris Feld <boris.feld@octobus.net>
Fri, 30 Mar 2018 12:43:08 +0200
changeset 37164 b229fd9adeae
parent 37163 0fb28899e81a
child 37165 6c7a6b04b274
bundlespec: introduce an attr-based class for bundlespec We will add support of contentops in the next patch, introduce a class instead of returning a 4-items tuple. Differential Revision: https://phab.mercurial-scm.org/D2971
mercurial/commands.py
mercurial/exchange.py
--- a/mercurial/commands.py	Fri Mar 23 20:43:55 2018 +0900
+++ b/mercurial/commands.py	Fri Mar 30 12:43:08 2018 +0200
@@ -1199,12 +1199,12 @@
 
     bundletype = opts.get('type', 'bzip2').lower()
     try:
-        bcompression, cgversion, params = exchange.parsebundlespec(
-                repo, bundletype, strict=False)
+        bundlespec = exchange.parsebundlespec(repo, bundletype, strict=False)
     except error.UnsupportedBundleSpecification as e:
         raise error.Abort(pycompat.bytestr(e),
                           hint=_("see 'hg help bundlespec' for supported "
                                  "values for --type"))
+    cgversion = bundlespec.version
 
     # Packed bundles are a pseudo bundle format for now.
     if cgversion == 's1':
@@ -1246,6 +1246,7 @@
         scmutil.nochangesfound(ui, repo, not base and outgoing.excluded)
         return 1
 
+    bcompression = bundlespec.compression
     if cgversion == '01': #bundle1
         if bcompression is None:
             bcompression = 'UN'
--- a/mercurial/exchange.py	Fri Mar 23 20:43:55 2018 +0900
+++ b/mercurial/exchange.py	Fri Mar 30 12:43:08 2018 +0200
@@ -17,6 +17,9 @@
     hex,
     nullid,
 )
+from .thirdparty import (
+    attr,
+)
 from . import (
     bookmarks as bookmod,
     bundle2,
@@ -52,6 +55,12 @@
 # Compression engines allowed in version 1. THIS SHOULD NEVER CHANGE.
 _bundlespecv1compengines = {'gzip', 'bzip2', 'none'}
 
+@attr.s
+class bundlespec(object):
+    compression = attr.ib()
+    version = attr.ib()
+    params = attr.ib()
+
 def parsebundlespec(repo, spec, strict=True, externalnames=False):
     """Parse a bundle string specification into parts.
 
@@ -75,8 +84,9 @@
     If ``externalnames`` is False (the default), the human-centric names will
     be converted to their internal representation.
 
-    Returns a 3-tuple of (compression, version, parameters). Compression will
-    be ``None`` if not in strict mode and a compression isn't defined.
+    Returns a bundlespec object of (compression, version, parameters).
+    Compression will be ``None`` if not in strict mode and a compression isn't
+    defined.
 
     An ``InvalidBundleSpecification`` is raised when the specification is
     not syntactically well formed.
@@ -172,7 +182,8 @@
         engine = util.compengines.forbundlename(compression)
         compression = engine.bundletype()[1]
         version = _bundlespeccgversions[version]
-    return compression, version, params
+
+    return bundlespec(compression, version, params)
 
 def readbundle(ui, fh, fname, vfs=None):
     header = changegroup.readexactly(fh, 4)
@@ -2140,10 +2151,10 @@
             # component of the BUNDLESPEC.
             if key == 'BUNDLESPEC':
                 try:
-                    comp, version, params = parsebundlespec(repo, value,
-                                                            externalnames=True)
-                    attrs['COMPRESSION'] = comp
-                    attrs['VERSION'] = version
+                    bundlespec = parsebundlespec(repo, value,
+                                                 externalnames=True)
+                    attrs['COMPRESSION'] = bundlespec.compression
+                    attrs['VERSION'] = bundlespec.version
                 except error.InvalidBundleSpecification:
                     pass
                 except error.UnsupportedBundleSpecification:
@@ -2168,10 +2179,12 @@
         spec = entry.get('BUNDLESPEC')
         if spec:
             try:
-                comp, version, params = parsebundlespec(repo, spec, strict=True)
+                bundlespec = parsebundlespec(repo, spec, strict=True)
 
                 # If a stream clone was requested, filter out non-streamclone
                 # entries.
+                comp = bundlespec.compression
+                version = bundlespec.version
                 if streamclonerequested and (comp != 'UN' or version != 's1'):
                     repo.ui.debug('filtering %s because not a stream clone\n' %
                                   entry['URL'])