diff hgext3rd/topic/server.py @ 6572:ef04d887c9a0

topic: compatibility for tns_heads peer command with Mercurial 5.9 and older
author Anton Shestakov <av6@dwimlabs.net>
date Fri, 13 Oct 2023 15:16:18 -0300
parents 2d3771d61068
children d511eba4cdb0
line wrap: on
line diff
--- a/hgext3rd/topic/server.py	Thu Oct 12 14:37:31 2023 -0300
+++ b/hgext3rd/topic/server.py	Fri Oct 13 15:16:18 2023 -0300
@@ -10,6 +10,7 @@
     extensions,
     localrepo,
     repoview,
+    util,
     wireprototypes,
     wireprotov1peer,
     wireprotov1server,
@@ -103,16 +104,31 @@
     wireprotov1server.wireprotocommand(b'tns_heads', b'namespaces', permission=b'pull')(tns_heads)
     extensions.wrapfunction(wireprotov1server, '_capabilities', wireprotocaps)
 
-    class tnspeer(wireprotov1peer.wirepeer):
-        @wireprotov1peer.batchable
-        def tns_heads(self, namespaces):
-            def decode(d):
+    if util.safehasattr(wireprotov1peer, 'future'):
+        # hg <= 5.9 (c424ff4807e6)
+        class tnspeer(wireprotov1peer.wirepeer):
+            """ wirepeer that uses `future` class from before c424ff4807e6 """
+            @wireprotov1peer.batchable
+            def tns_heads(self, namespaces):
+                f = wireprotov1peer.future()
+                yield {b'namespaces': wireprototypes.encodelist(namespaces)}, f
+                d = f.value
                 try:
-                    return wireprototypes.decodelist(d[:-1])
+                    yield wireprototypes.decodelist(d[:-1])
                 except ValueError:
                     self._abort(error.ResponseError(_(b"unexpected response:"), d))
+    else:
+        class tnspeer(wireprotov1peer.wirepeer):
+            """ wirepeer that uses newer batchable scheme from c424ff4807e6 """
+            @wireprotov1peer.batchable
+            def tns_heads(self, namespaces):
+                def decode(d):
+                    try:
+                        return wireprototypes.decodelist(d[:-1])
+                    except ValueError:
+                        self._abort(error.ResponseError(_(b"unexpected response:"), d))
 
-            return {b'namespaces': wireprototypes.encodelist(namespaces)}, decode
+                return {b'namespaces': wireprototypes.encodelist(namespaces)}, decode
 
     wireprotov1peer.wirepeer = tnspeer