changeset 27751:a40e2f7fe49d

changegroup: hide packermap behind methods This is to prepare for hiding changegroup3 behind a config option.
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 12 Jan 2016 21:01:06 -0800
parents 443848eece18
children 29cfc474c5fd
files mercurial/bundle2.py mercurial/bundlerepo.py mercurial/changegroup.py mercurial/commands.py mercurial/exchange.py
diffstat 5 files changed, 24 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bundle2.py	Wed Jan 13 15:47:37 2016 -0600
+++ b/mercurial/bundle2.py	Tue Jan 12 21:01:06 2016 -0800
@@ -1240,7 +1240,7 @@
     Exists to allow extensions (like evolution) to mutate the capabilities.
     """
     caps = capabilities.copy()
-    caps['changegroup'] = tuple(sorted(changegroup.packermap.keys()))
+    caps['changegroup'] = tuple(sorted(changegroup.supportedversions(repo)))
     if obsolete.isenabled(repo, obsolete.exchangeopt):
         supportedformat = tuple('V%i' % v for v in obsolete.formats)
         caps['obsmarkers'] = supportedformat
@@ -1277,8 +1277,7 @@
     op.gettransaction()
     unpackerversion = inpart.params.get('version', '01')
     # We should raise an appropriate exception here
-    unpacker = changegroup.packermap[unpackerversion][1]
-    cg = unpacker(inpart, None)
+    cg = changegroup.getunbundler(unpackerversion, inpart, None)
     # the source and url passed here are overwritten by the one contained in
     # the transaction.hookargs argument. So 'bundle2' is a placeholder
     nbchangesets = None
--- a/mercurial/bundlerepo.py	Wed Jan 13 15:47:37 2016 -0600
+++ b/mercurial/bundlerepo.py	Tue Jan 12 21:01:06 2016 -0800
@@ -285,7 +285,7 @@
                                                   "multiple changegroups")
                     cgstream = part
                     version = part.params.get('version', '01')
-                    if version not in changegroup.packermap:
+                    if version not in changegroup.supportedversions(self):
                         msg = _('Unsupported changegroup version: %s')
                         raise error.Abort(msg % version)
                     if self.bundle.compressed():
@@ -296,7 +296,7 @@
                 raise error.Abort('No changegroups found')
             cgstream.seek(0)
 
-            self.bundle = changegroup.packermap[version][1](cgstream, 'UN')
+            self.bundle = changegroup.getunbundler(version, cgstream, 'UN')
 
         elif self.bundle.compressed():
             f = _writetempbundle(self.bundle.read, '.hg10un', header='HG10UN')
--- a/mercurial/changegroup.py	Wed Jan 13 15:47:37 2016 -0600
+++ b/mercurial/changegroup.py	Tue Jan 12 21:01:06 2016 -0800
@@ -914,13 +914,23 @@
         return struct.pack(
             self.deltaheader, node, p1n, p2n, basenode, linknode, flags)
 
-packermap = {'01': (cg1packer, cg1unpacker),
+_packermap = {'01': (cg1packer, cg1unpacker),
              # cg2 adds support for exchanging generaldelta
              '02': (cg2packer, cg2unpacker),
              # cg3 adds support for exchanging treemanifests
              '03': (cg3packer, cg3unpacker),
 }
 
+def supportedversions(repo):
+    return _packermap.keys()
+
+def getbundler(version, repo, bundlecaps=None):
+    assert version in supportedversions(repo)
+    return _packermap[version][0](repo, bundlecaps)
+
+def getunbundler(version, fh, alg):
+    return _packermap[version][1](fh, alg)
+
 def _changegroupinfo(repo, nodes, source):
     if repo.ui.verbose or source == 'bundle':
         repo.ui.status(_("%d changesets found\n") % len(nodes))
@@ -947,7 +957,7 @@
 
 def getsubset(repo, outgoing, bundler, source, fastpath=False):
     gengroup = getsubsetraw(repo, outgoing, bundler, source, fastpath)
-    return packermap[bundler.version][1](util.chunkbuffer(gengroup), None)
+    return getunbundler(bundler.version, util.chunkbuffer(gengroup), None)
 
 def changegroupsubset(repo, roots, heads, source, version='01'):
     """Compute a changegroup consisting of all the nodes that are
@@ -973,7 +983,7 @@
     included = set(csets)
     discbases = [n for n in discbases if n not in included]
     outgoing = discovery.outgoing(cl, discbases, heads)
-    bundler = packermap[version][0](repo)
+    bundler = getbundler(version, repo)
     return getsubset(repo, outgoing, bundler, source)
 
 def getlocalchangegroupraw(repo, source, outgoing, bundlecaps=None,
@@ -984,7 +994,7 @@
     precomputed sets in outgoing. Returns a raw changegroup generator."""
     if not outgoing.missing:
         return None
-    bundler = packermap[version][0](repo, bundlecaps)
+    bundler = getbundler(version, repo, bundlecaps)
     return getsubsetraw(repo, outgoing, bundler, source)
 
 def getlocalchangegroup(repo, source, outgoing, bundlecaps=None,
@@ -995,7 +1005,7 @@
     precomputed sets in outgoing."""
     if not outgoing.missing:
         return None
-    bundler = packermap[version][0](repo, bundlecaps)
+    bundler = getbundler(version, repo, bundlecaps)
     return getsubset(repo, outgoing, bundler, source)
 
 def computeoutgoing(repo, heads, common):
--- a/mercurial/commands.py	Wed Jan 13 15:47:37 2016 -0600
+++ b/mercurial/commands.py	Tue Jan 12 21:01:06 2016 -0800
@@ -2078,7 +2078,7 @@
         ui.write('%s -- %r\n' % (part.type, repr(part.params)))
         if part.type == 'changegroup':
             version = part.params.get('version', '01')
-            cg = changegroup.packermap[version][1](part, 'UN')
+            cg = changegroup.getunbundler(version, part, 'UN')
             chunkdata = cg.changelogheader()
             chain = None
             while True:
--- a/mercurial/exchange.py	Wed Jan 13 15:47:37 2016 -0600
+++ b/mercurial/exchange.py	Tue Jan 12 21:01:06 2016 -0800
@@ -653,7 +653,8 @@
         cg = changegroup.getlocalchangegroupraw(pushop.repo, 'push',
                                                 pushop.outgoing)
     else:
-        cgversions = [v for v in cgversions if v in changegroup.packermap]
+        cgversions = [v for v in cgversions
+                      if v in changegroup.supportedversions(pushop.repo)]
         if not cgversions:
             raise ValueError(_('no common changegroup version'))
         version = max(cgversions)
@@ -1505,7 +1506,8 @@
         cgversions = b2caps.get('changegroup')
         getcgkwargs = {}
         if cgversions:  # 3.1 and 3.2 ship with an empty value
-            cgversions = [v for v in cgversions if v in changegroup.packermap]
+            cgversions = [v for v in cgversions
+                          if v in changegroup.supportedversions(repo)]
             if not cgversions:
                 raise ValueError(_('no common changegroup version'))
             version = getcgkwargs['version'] = max(cgversions)