changeset 1092:3799bf885c1d

compat: use newer read_pkt_refs from dulwich if possible Beginning with dulwich 0.18, it now supports reporting the symrefs so we no longer need to monkey patch (will require future patches to use the new code, though).
author Sean Farley <sean@farley.io>
date Mon, 27 Nov 2017 17:45:51 -0500
parents 367f9e0dc198
children 8e61d7a29932
files hggit/compat.py
diffstat 1 files changed, 30 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/hggit/compat.py	Mon Nov 27 17:40:59 2017 -0500
+++ b/hggit/compat.py	Mon Nov 27 17:45:51 2017 -0500
@@ -52,30 +52,38 @@
         # compat with hg < 3.9
         return url.passwordmgr(ui)
 
-# dulwich doesn't return the symref where remote HEAD points, so we monkey
-# patch it here
-from dulwich.errors import GitProtocolError
-from dulwich.protocol import extract_capabilities
+
+try:
+    import dulwich.client
+    FetchPackResult = dulwich.client.FetchPackResult
+    read_pkt_refs = dulwich.client.read_pkt_refs
+except (AttributeError, ImportError):
+    # older dulwich doesn't return the symref where remote HEAD points, so we
+    # monkey patch it here
+    from dulwich.errors import GitProtocolError
+    from dulwich.protocol import extract_capabilities
+
+
 
-def read_pkt_refs(proto):
-    server_capabilities = None
-    refs = {}
-    # Receive refs from server
-    for pkt in proto.read_pkt_seq():
-        (sha, ref) = pkt.rstrip('\n').split(None, 1)
-        if sha == 'ERR':
-            raise GitProtocolError(ref)
-        if server_capabilities is None:
-            (ref, server_capabilities) = extract_capabilities(ref)
-            symref = 'symref=HEAD:'
-            for cap in server_capabilities:
-                if cap.startswith(symref):
-                    sha = cap.replace(symref, '')
-        refs[ref] = sha
+    def read_pkt_refs(proto):
+        server_capabilities = None
+        refs = {}
+        # Receive refs from server
+        for pkt in proto.read_pkt_seq():
+            (sha, ref) = pkt.rstrip('\n').split(None, 1)
+            if sha == 'ERR':
+                raise GitProtocolError(ref)
+            if server_capabilities is None:
+                (ref, server_capabilities) = extract_capabilities(ref)
+                symref = 'symref=HEAD:'
+                for cap in server_capabilities:
+                    if cap.startswith(symref):
+                        sha = cap.replace(symref, '')
+            refs[ref] = sha
 
-    if len(refs) == 0:
-        return None, set([])
-    return refs, set(server_capabilities)
+        if len(refs) == 0:
+            return None, set([])
+        return refs, set(server_capabilities)
 
 
 CONFIG_DEFAULTS = {