diff hggit/compat.py @ 1104:e326b349eba6

compat: extract function for memfilectx signature variants
author Kevin Bullock <kbullock@ringworld.org>
date Tue, 06 Feb 2018 08:45:48 -0600
parents 8e61d7a29932
children 8ed6c0cae9b8
line wrap: on
line diff
--- a/hggit/compat.py	Tue Feb 06 08:37:11 2018 -0600
+++ b/hggit/compat.py	Tue Feb 06 08:45:48 2018 -0600
@@ -1,4 +1,5 @@
 from mercurial import (
+    context,
     url,
     util as hgutil,
 )
@@ -96,6 +97,26 @@
         return refs, set(server_capabilities)
 
 
+def memfilectx(repo, changectx, path, data, islink=False,
+               isexec=False, copied=None):
+    # Different versions of mercurial have different parameters to
+    # memfilectx.  Try them from newest to oldest.
+    args_to_try = (
+        (repo, changectx, path, data),  # hg 4.5+
+        (repo, path, data),             # hg 3.1 - 4.5
+        (path, data),                   # hg < 3.1
+    )
+    for args in args_to_try:
+        try:
+            return context.memfilectx(*args,
+                                      islink=islink,
+                                      isexec=isexec,
+                                      copied=copied)
+        except TypeError as ex:
+            last_ex = ex
+    raise last_ex
+
+
 CONFIG_DEFAULTS = {
     'git': {
         'authors': None,