changeset 21899:52ab44b979f4

bundle2-push: extract changegroup logic in its own function We extract the creation of changegroup related parts into its own function. This precludes the inclusion of more diverse data during the bundle2 push. We use a closure to carry the logic that need to be perform when processing the server reply.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Wed, 02 Jul 2014 11:42:35 +0200
parents 10fcfb615fb4
children b8bd97085ec9
files mercurial/exchange.py
diffstat 1 files changed, 19 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/exchange.py	Wed Jul 02 14:09:24 2014 +0200
+++ b/mercurial/exchange.py	Wed Jul 02 11:42:35 2014 +0200
@@ -203,6 +203,23 @@
                              newbm)
     return True
 
+def _pushb2ctx(pushop, bundler):
+    """handle changegroup push through bundle2
+
+    addchangegroup result is stored in the ``pushop.ret`` attribute.
+    """
+    # Send known heads to the server for race detection.
+    if not pushop.force:
+        bundler.newpart('B2X:CHECK:HEADS', data=iter(pushop.remoteheads))
+    cg = changegroup.getlocalbundle(pushop.repo, 'push', pushop.outgoing)
+    cgpart = bundler.newpart('B2X:CHANGEGROUP', data=cg.getchunks())
+    def handlereply(op):
+        """extract addchangroup returns from server reply"""
+        cgreplies = op.records.getreplies(cgpart.id)
+        assert len(cgreplies['changegroup']) == 1
+        pushop.ret = cgreplies['changegroup'][0]['return']
+    return handlereply
+
 def _pushbundle2(pushop):
     """push data to the remote using bundle2
 
@@ -213,12 +230,8 @@
     capsblob = bundle2.encodecaps(pushop.repo.bundle2caps)
     bundler.newpart('b2x:replycaps', data=capsblob)
     extrainfo = _pushbundle2extraparts(pushop, bundler)
-    # Send known heads to the server for race detection.
-    if not pushop.force:
-        bundler.newpart('B2X:CHECK:HEADS', data=iter(pushop.remoteheads))
     # add the changegroup bundle
-    cg = changegroup.getlocalbundle(pushop.repo, 'push', pushop.outgoing)
-    cgpart = bundler.newpart('B2X:CHANGEGROUP', data=cg.getchunks())
+    cgreplyhandler = _pushb2ctx(pushop, bundler)
     stream = util.chunkbuffer(bundler.getchunks())
     try:
         reply = pushop.remote.unbundle(stream, ['force'], 'push')
@@ -228,9 +241,7 @@
         op = bundle2.processbundle(pushop.repo, reply)
     except error.BundleValueError, exc:
         raise util.Abort('missing support for %s' % exc)
-    cgreplies = op.records.getreplies(cgpart.id)
-    assert len(cgreplies['changegroup']) == 1
-    pushop.ret = cgreplies['changegroup'][0]['return']
+    cgreplyhandler(op)
     _pushbundle2extrareply(pushop, op, extrainfo)
 
 def _pushbundle2extraparts(pushop, bundler):