simple4server: backport the bundle2 exchange
authorPierre-Yves David <pierre-yves.david@fb.com>
Wed, 21 May 2014 12:01:28 -0700
changeset 963 f80e8e3c3726
parent 962 d04a52f40f41
child 964 c768f7666106
child 967 b4fa7cc040d6
simple4server: backport the bundle2 exchange
hgext/simple4server.py
tests/test-simple4server.t
--- a/hgext/simple4server.py	Tue May 20 22:36:47 2014 -0700
+++ b/hgext/simple4server.py	Wed May 21 12:01:28 2014 -0700
@@ -8,7 +8,7 @@
 For client side usages it is recommended to use the evolve extension for
 improved user interface.'''
 
-testedwith = '2.9.1'
+testedwith = '3.0.1'
 buglink = 'https://bitbucket.org/marmoute/mutable-history/issues'
 
 import mercurial.obsolete
@@ -22,8 +22,23 @@
 from cStringIO import StringIO
 from mercurial import node
 from mercurial.hgweb import hgweb_mod
+from mercurial import bundle2
+from mercurial import localrepo
+from mercurial import exchange
 _pack = struct.pack
 
+gboptslist = gboptsmap = None
+try:
+    from mercurial import obsolete
+    if not obsolete._enabled:
+        obsolete._enabled = True
+    from mercurial import wireproto
+    gboptslist = getattr(wireproto, 'gboptslist', None)
+    gboptsmap = getattr(wireproto, 'gboptsmap', None)
+except (ImportError, AttributeError):
+    raise util.Abort('Your Mercurial is too old for this version of Evolve\n'
+                     'requires version 3.0.1 or above')
+
 # Start of simple4server specific content
 
 from mercurial import pushkey
@@ -228,11 +243,22 @@
         caps += ' _evoext_pushobsmarkers_0'
         caps += ' _evoext_pullobsmarkers_0'
         caps += ' _evoext_obshash_0'
+        caps += ' _evoext_b2x_obsmarkers_0'
     return caps
 
+
 # from evolve extension: 10867a8e27c6
 # heavily modified
 def extsetup(ui):
+    localrepo.moderncaps.add('_evoext_b2x_obsmarkers_0')
+    if gboptsmap is not None:
+        gboptsmap['evo_obsmarker'] = 'plain'
+        gboptsmap['evo_obscommon'] = 'plain'
+        gboptsmap['evo_obsheads'] = 'plain'
+    else:
+        gboptslist.append('evo_obsheads')
+        gboptslist.append('evo_obscommon')
+        gboptslist.append('evo_obsmarker')
     obsolete.obsstore = pruneobsstore
     obsolete.obsstore.relevantmarkers = relevantmarkers
     hgweb_mod.perms['evoext_pushobsmarkers_0'] = 'push'
@@ -241,6 +267,7 @@
     wireproto.commands['evoext_pushobsmarkers_0'] = (srv_pushobsmarkers, '')
     wireproto.commands['evoext_pullobsmarkers_0'] = (srv_pullobsmarkers, '*')
     # wrap module content
+    extensions.wrapfunction(exchange, '_getbundleextrapart', _getbundleextrapart)
     extensions.wrapfunction(wireproto, 'capabilities', capabilities)
     # wrap command content
     oldcap, args = wireproto.commands['capabilities']
@@ -251,3 +278,48 @@
     # specific simple4server content
     extensions.wrapfunction(pushkey, '_nslist', _nslist)
     pushkey._namespaces['namespaces'] = (lambda *x: False, pushkey._nslist)
+
+
+#from evolve extension
+@bundle2.parthandler('evolve:b2x:obsmarkerv1')
+def handleobsmarkerv1(op, inpart):
+    """add a stream of obsmarker to the repo"""
+    tr = op.gettransaction()
+    advparams = dict(inpart.advisoryparams)
+    length = advparams.get('totalbytes')
+    if length is None:
+        obsdata = inpart.read()
+    else:
+        length = int(length)
+        data = StringIO()
+        current = 0
+        op.ui.progress('OBSEXC', current, unit="bytes", total=length)
+        while current < length:
+            readsize = min(length-current, 4096)
+            data.write(inpart.read(readsize))
+            current += readsize
+            op.ui.progress('OBSEXC', current, unit="bytes", total=length)
+        op.ui.progress('OBSEXC', None)
+        obsdata = data.getvalue()
+    totalsize = len(obsdata)
+    old = len(op.repo.obsstore._all)
+    op.repo.obsstore.mergemarkers(tr, obsdata)
+    new = len(op.repo.obsstore._all) - old
+    op.records.add('evo_obsmarkers', {'new': new, 'bytes': totalsize})
+    tr.hookargs['evolve_new_obsmarkers'] = str(new)
+
+#from evolve extension
+def _getbundleextrapart(orig, bundler, repo, source, **kwargs):
+    if int(kwargs.pop('evo_obsmarker', False)):
+        common = kwargs.pop('evo_obscommon')
+        common = wireproto.decodelist(common)
+        heads = kwargs.pop('evo_obsheads')
+        heads = wireproto.decodelist(heads)
+        obsdata = _getobsmarkersstream(repo, common=common, heads=heads)
+        if len(obsdata.getvalue()) > 5:
+            advparams = [('totalbytes', str(len(obsdata.getvalue())))]
+            obspart = bundle2.bundlepart('EVOLVE:B2X:OBSMARKERV1',
+                                         advisoryparams=advparams,
+                                         data=obsdata)
+            bundler.addpart(obspart)
+    orig(bundler, repo, source)
--- a/tests/test-simple4server.t	Tue May 20 22:36:47 2014 -0700
+++ b/tests/test-simple4server.t	Wed May 21 12:01:28 2014 -0700
@@ -76,9 +76,9 @@
 ===================
 
   $ curl --silent http://localhost:$HGPORT/?cmd=hello
-  capabilities: lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0
+  capabilities: lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0 _evoext_b2x_obsmarkers_0
   $ curl --silent http://localhost:$HGPORT/?cmd=capabilities
-  lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0 (no-eol)
+  lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0 _evoext_b2x_obsmarkers_0 (no-eol)
 
   $ curl --silent "http://localhost:$HGPORT/?cmd=listkeys&namespace=namespaces" | sort
   bookmarks	
@@ -151,9 +151,9 @@
   obsolete	
   phases	
   $ curl --silent http://localhost:$HGPORT/?cmd=hello
-  capabilities: lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0
+  capabilities: lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0 _evoext_b2x_obsmarkers_0
   $ curl --silent http://localhost:$HGPORT/?cmd=capabilities
-  lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0 (no-eol)
+  lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0 _evoext_b2x_obsmarkers_0 (no-eol)
 
   $ echo '[__temporary__]' >> server/.hg/hgrc
   $ echo 'advertiseobsolete=False' >> server/.hg/hgrc
@@ -181,6 +181,6 @@
   obsolete	
   phases	
   $ curl --silent http://localhost:$HGPORT/?cmd=hello
-  capabilities: lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0
+  capabilities: lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0 _evoext_b2x_obsmarkers_0
   $ curl --silent http://localhost:$HGPORT/?cmd=capabilities
-  lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0 (no-eol)
+  lookup changegroupsubset branchmap pushkey known getbundle unbundlehash batch stream unbundle=HG10GZ,HG10BZ,HG10UN httpheader=1024 _evoext_pushobsmarkers_0 _evoext_pullobsmarkers_0 _evoext_obshash_0 _evoext_b2x_obsmarkers_0 (no-eol)