changegroup: pass function to build delta header into constructor
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 02 Aug 2018 17:44:56 -0700
changeset 38897 bd64b8b8f0dd
parent 38896 271854adc3a6
child 38898 67f37e8a5490
changegroup: pass function to build delta header into constructor Previously, the delta header struct format was defined on each class and each class had a separate function for building the delta header. We replace both of these with an argument to __init__ containing a callable that can format a delta header given a revisiondelta instance. Differential Revision: https://phab.mercurial-scm.org/D4079
mercurial/changegroup.py
--- a/mercurial/changegroup.py	Thu Aug 02 17:52:21 2018 -0700
+++ b/mercurial/changegroup.py	Thu Aug 02 17:44:56 2018 -0700
@@ -520,14 +520,16 @@
     deltachunks = attr.ib()
 
 class cg1packer(object):
-    deltaheader = _CHANGEGROUPV1_DELTA_HEADER
-
-    def __init__(self, repo, filematcher, version, bundlecaps=None):
+    def __init__(self, repo, filematcher, version, builddeltaheader,
+                 bundlecaps=None):
         """Given a source repo, construct a bundler.
 
         filematcher is a matcher that matches on files to include in the
         changegroup. Used to facilitate sparse changegroups.
 
+        builddeltaheader is a callable that constructs the header for a group
+        delta.
+
         bundlecaps is optional and can be used to specify the set of
         capabilities which can be used to build the bundle. While bundlecaps is
         unused in core Mercurial, extensions rely on this feature to communicate
@@ -537,6 +539,7 @@
         self._filematcher = filematcher
 
         self.version = version
+        self._builddeltaheader = builddeltaheader
 
         # Set of capabilities we can use to build the bundle.
         if bundlecaps is None:
@@ -933,9 +936,7 @@
         if not delta:
             return
 
-        meta = self.builddeltaheader(delta.node, delta.p1node, delta.p2node,
-                                     delta.basenode, delta.linknode,
-                                     delta.flags)
+        meta = self._builddeltaheader(delta)
         l = len(meta) + sum(len(x) for x in delta.deltachunks)
 
         yield chunkheader(l)
@@ -1096,16 +1097,11 @@
             deltachunks=(diffheader, data),
         )
 
-    def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags):
-        # do nothing with basenode, it is implicitly the previous one in HG10
-        # do nothing with flags, it is implicitly 0 for cg1 and cg2
-        return self.deltaheader.pack(node, p1n, p2n, linknode)
-
 class cg2packer(cg1packer):
-    deltaheader = _CHANGEGROUPV2_DELTA_HEADER
-
-    def __init__(self, repo, filematcher, version, bundlecaps=None):
+    def __init__(self, repo, filematcher, version, builddeltaheader,
+                 bundlecaps=None):
         super(cg2packer, self).__init__(repo, filematcher, version,
+                                        builddeltaheader,
                                         bundlecaps=bundlecaps)
 
         if self._reorder is None:
@@ -1153,13 +1149,7 @@
             base = nullrev
         return base
 
-    def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags):
-        # Do nothing with flags, it is implicitly 0 in cg1 and cg2
-        return self.deltaheader.pack(node, p1n, p2n, basenode, linknode)
-
 class cg3packer(cg2packer):
-    deltaheader = _CHANGEGROUPV3_DELTA_HEADER
-
     def _packmanifests(self, dir, mfnodes, lookuplinknode):
         if dir:
             yield self.fileheader(dir)
@@ -1172,17 +1162,26 @@
     def _manifestsdone(self):
         return self.close()
 
-    def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags):
-        return self.deltaheader.pack(node, p1n, p2n, basenode, linknode, flags)
+def _makecg1packer(repo, filematcher, bundlecaps):
+    builddeltaheader = lambda d: _CHANGEGROUPV1_DELTA_HEADER.pack(
+        d.node, d.p1node, d.p2node, d.linknode)
 
-def _makecg1packer(repo, filematcher, bundlecaps):
-    return cg1packer(repo, filematcher, b'01', bundlecaps=bundlecaps)
+    return cg1packer(repo, filematcher, b'01', builddeltaheader,
+                     bundlecaps=bundlecaps)
 
 def _makecg2packer(repo, filematcher, bundlecaps):
-    return cg2packer(repo, filematcher, b'02', bundlecaps=bundlecaps)
+    builddeltaheader = lambda d: _CHANGEGROUPV2_DELTA_HEADER.pack(
+        d.node, d.p1node, d.p2node, d.basenode, d.linknode)
+
+    return cg2packer(repo, filematcher, b'02', builddeltaheader,
+                     bundlecaps=bundlecaps)
 
 def _makecg3packer(repo, filematcher, bundlecaps):
-    return cg3packer(repo, filematcher, b'03', bundlecaps=bundlecaps)
+    builddeltaheader = lambda d: _CHANGEGROUPV3_DELTA_HEADER.pack(
+        d.node, d.p1node, d.p2node, d.basenode, d.linknode, d.flags)
+
+    return cg3packer(repo, filematcher, b'03', builddeltaheader,
+                     bundlecaps=bundlecaps)
 
 _packermap = {'01': (_makecg1packer, cg1unpacker),
              # cg2 adds support for exchanging generaldelta