Patchwork [2,of,9] bundle2-push: extract changegroup logic in its own function

login
register
mail settings
Submitter Pierre-Yves David
Date July 7, 2014, 11:02 a.m.
Message ID <05fea039be398d565c1f.1404730920@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/5123/
State Accepted
Commit 52ab44b979f4c07bcc0470bb097ecc98d08524c2
Headers show

Comments

Pierre-Yves David - July 7, 2014, 11:02 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1404294155 -7200
#      Wed Jul 02 11:42:35 2014 +0200
# Node ID 05fea039be398d565c1fba9403d796ee0bb52816
# Parent  59d3193fc0e09971d7be39469875b132f1757392
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.

Patch

diff --git a/mercurial/exchange.py b/mercurial/exchange.py
--- a/mercurial/exchange.py
+++ b/mercurial/exchange.py
@@ -201,38 +201,49 @@  def _pushcheckoutgoing(pushop):
                              pushop.newbranch,
                              bool(pushop.incoming),
                              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
 
     The only currently supported type of data is changegroup but this will
     evolve in the future."""
     bundler = bundle2.bundle20(pushop.ui, bundle2.bundle2caps(pushop.remote))
     # create reply capability
     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')
     except error.BundleValueError, exc:
         raise util.Abort('missing support for %s' % exc)
     try:
         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):
     """hook function to let extensions add parts