diff extensions/kwarchive.py @ 304:71288cb43ba9

kwarchive: Make it work with Mercurial 4.6+ (really tested on 4.8.1). 1. There is an API change when calling functions from templatekw 2. Some date specific utility functions have been moved into a new subpackage utils.dateutil
author Franz Glasner <hg@dom66.de>
date Wed, 30 Jan 2019 11:12:57 +0100
parents 56df37d7878e
children f8aa592b344e
line wrap: on
line diff
--- a/extensions/kwarchive.py	Wed Jan 30 09:08:21 2019 +0100
+++ b/extensions/kwarchive.py	Wed Jan 30 11:12:57 2019 +0100
@@ -94,6 +94,19 @@
     from mercurial import registrar
 except ImportError:
     registrar = None
+# check for new template function API with `(context, mapping)`
+try:
+    from mercurial import templatefuncs
+except ImportError:
+    context_mapping_api = False
+else:
+    # >= 4.6
+    context_mapping_api = True
+# some date specific util functions moved to new mercurial.utils.dateutil
+try:
+    from mercurial.utils import dateutil as _dateutil
+except ImportError:
+    _dateutil = util
 
 
 testedwith = "4.3.1 4.3.2 4.4.1 4.4.2 4.5.2 4.6.1 4.8.1"
@@ -706,16 +719,23 @@
     elif path_uri.startswith(b'/'):
         # an absolute POSIX path
         path_uri = b"file://" + path_uri
-    tagcache={}
-    latesttags = templatekw.getlatesttags(ctx.repo(), ctx, tagcache)
+    if context_mapping_api:
+        mapping = {
+            'repo': ctx.repo(),
+            'ctx': ctx,
+            'cache': {}
+        }
+        latesttags = templatekw.getlatesttags(_FakeRenderContext(), mapping)
+    else:
+        latesttags = templatekw.getlatesttags(ctx.repo(), ctx, {})
     keywords = {
         "HGpath": path_uri,     # XXX FIXME: Should Archive an alias of this
         "HGbranch": ctx.branch(),
         "HGtags": " ".join([tag for tag in ctx.tags() if tag != "tip"]),
         "HGlatesttags": " ".join(latesttags[2]),
         "HGlatesttagdistance": latesttags[1],
-        "HGlatesttagdate": templatefilters.isodatesec(util.makedate(latesttags[0])),
-        "HGlatesttagjustdate": templatefilters.shortdate(util.makedate(latesttags[0])),
+        "HGlatesttagdate": templatefilters.isodatesec(_dateutil.makedate(latesttags[0])),
+        "HGlatesttagjustdate": templatefilters.shortdate(_dateutil.makedate(latesttags[0])),
         "HGbookmarks": " ".join([bm if not bm.startswith('*') else bm[1:]
                                  for bm in ctx.bookmarks() if bm != "@"]),
         "State": ctx.phasestr(),
@@ -802,3 +822,12 @@
 
     def makefileobj_compat(ctx, pat, **props):
         return cmdutil.makefileobj(ctx.repo(), pat, ctx.node(), **props)
+
+
+if context_mapping_api:
+
+    class _FakeRenderContext(object):
+
+        @staticmethod
+        def resource(mapping, name):
+            return mapping[name]