# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 2014-04-15 15:27:55
# Node ID c15b66a6bbb4fa9943a57dc8775c301dc9367739
# Parent  7974aa88868ea143606b00e2a6f6a8a340cb5ac9

bundle2: return a stream from exchange.getbundle

For friendliness with the wire protocol implementation, the `exchange.getbundle` now
returns a binary stream when asked for a bundle2. We detect a bundle2 request and
upgrade the binary stream to an unbundler object.

In the future the unbundler may gain feature to look like a binary stream, but
we are not quite there yet.

diff --git a/mercurial/exchange.py b/mercurial/exchange.py
--- a/mercurial/exchange.py
+++ b/mercurial/exchange.py
@@ -659,7 +659,7 @@ def getbundle(repo, source, heads=None, 
     bundler = bundle2.bundle20(repo.ui)
     part = bundle2.bundlepart('changegroup', data=cg.getchunks())
     bundler.addpart(part)
-    return bundle2.unbundle20(repo.ui, util.chunkbuffer(bundler.getchunks()))
+    return util.chunkbuffer(bundler.getchunks())
 
 class PushRaced(RuntimeError):
     """An exception raised during unbundling that indicate a push race"""
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -9,7 +9,7 @@ from i18n import _
 import peer, changegroup, subrepo, pushkey, obsolete, repoview
 import changelog, dirstate, filelog, manifest, context, bookmarks, phases
 import lock as lockmod
-import transaction, store, encoding, exchange
+import transaction, store, encoding, exchange, bundle2
 import scmutil, util, extensions, hook, error, revset
 import match as matchmod
 import merge as mergemod
@@ -106,8 +106,14 @@ class localpeer(peer.peerrepository):
 
     def getbundle(self, source, heads=None, common=None, bundlecaps=None,
                   format='HG10'):
-        return exchange.getbundle(self._repo, source, heads=heads,
-                                  common=common, bundlecaps=bundlecaps)
+        cg = exchange.getbundle(self._repo, source, heads=heads,
+                                common=common, bundlecaps=bundlecaps)
+        if bundlecaps is not None and 'HG20' in bundlecaps:
+            # When requesting a bundle2, getbundle returns a stream to make the
+            # wire level function happier. We need to build a proper object
+            # from it in local peer.
+            cg = bundle2.unbundle20(self.ui, cg)
+        return cg
 
     # TODO We might want to move the next two calls into legacypeer and add
     # unbundle instead.