Patchwork [07,of,10] templatekw: switch latesttags template keywords to new API

login
register
mail settings
Submitter Yuya Nishihara
Date March 2, 2018, 1:51 a.m.
Message ID <69d82c25f62751f65cbc.1519955517@mimosa>
Download mbox | patch
Permalink /patch/28651/
State Accepted
Headers show

Comments

Yuya Nishihara - March 2, 2018, 1:51 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1519554186 -32400
#      Sun Feb 25 19:23:06 2018 +0900
# Node ID 69d82c25f62751f65cbc40f225f7a1b3f5326a1f
# Parent  2918050dfb1503e9ebcba830e4584288e4ab4a40
templatekw: switch latesttags template keywords to new API

Patch

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -238,8 +238,11 @@  def _showlist(name, values, templ, mappi
     if endname in templ:
         yield templ(endname, **strmapping)
 
-def getlatesttags(repo, ctx, cache, pattern=None):
+def getlatesttags(context, mapping, pattern=None):
     '''return date, distance and name for the latest tag of rev'''
+    repo = context.resource(mapping, 'repo')
+    ctx = context.resource(mapping, 'ctx')
+    cache = context.resource(mapping, 'cache')
 
     cachename = 'latesttags'
     if pattern is not None:
@@ -587,20 +590,17 @@  def showindex(context, mapping):
     # just hosts documentation; should be overridden by template mapping
     raise error.Abort(_("can't use index in this context"))
 
-@templatekeyword('latesttag')
-def showlatesttag(**args):
+@templatekeyword('latesttag', requires={'repo', 'ctx', 'cache', 'templ'})
+def showlatesttag(context, mapping):
     """List of strings. The global tags on the most recent globally
     tagged ancestor of this changeset.  If no such tags exist, the list
     consists of the single string "null".
     """
-    return showlatesttags(None, **args)
+    return showlatesttags(context, mapping, None)
 
-def showlatesttags(pattern, **args):
+def showlatesttags(context, mapping, pattern):
     """helper method for the latesttag keyword and function"""
-    args = pycompat.byteskwargs(args)
-    repo, ctx = args['repo'], args['ctx']
-    cache = args['cache']
-    latesttags = getlatesttags(repo, ctx, cache, pattern)
+    latesttags = getlatesttags(context, mapping, pattern)
 
     # latesttag[0] is an implementation detail for sorting csets on different
     # branches in a stable manner- it is the date the tagged cset was created,
@@ -613,25 +613,28 @@  def showlatesttags(pattern, **args):
     }
 
     tags = latesttags[2]
-    f = _showlist('latesttag', tags, args['templ'], args, separator=':')
+    templ = context.resource(mapping, 'templ')
+    f = _showlist('latesttag', tags, templ, mapping, separator=':')
     return _hybrid(f, tags, makemap, pycompat.identity)
 
-@templatekeyword('latesttagdistance')
-def showlatesttagdistance(repo, ctx, templ, cache, **args):
+@templatekeyword('latesttagdistance', requires={'repo', 'ctx', 'cache'})
+def showlatesttagdistance(context, mapping):
     """Integer. Longest path to the latest tag."""
-    return getlatesttags(repo, ctx, cache)[1]
+    return getlatesttags(context, mapping)[1]
 
-@templatekeyword('changessincelatesttag')
-def showchangessincelatesttag(repo, ctx, templ, cache, **args):
+@templatekeyword('changessincelatesttag', requires={'repo', 'ctx', 'cache'})
+def showchangessincelatesttag(context, mapping):
     """Integer. All ancestors not in the latest tag."""
-    latesttag = getlatesttags(repo, ctx, cache)[2][0]
+    mapping = mapping.copy()
+    mapping['tag'] = getlatesttags(context, mapping)[2][0]
+    return _showchangessincetag(context, mapping)
 
-    return _showchangessincetag(repo, ctx, tag=latesttag, **args)
-
-def _showchangessincetag(repo, ctx, **args):
+def _showchangessincetag(context, mapping):
+    repo = context.resource(mapping, 'repo')
+    ctx = context.resource(mapping, 'ctx')
     offset = 0
     revs = [ctx.rev()]
-    tag = args[r'tag']
+    tag = context.symbol(mapping, 'tag')
 
     # The only() revset doesn't currently support wdir()
     if ctx.rev() is None:
@@ -640,6 +643,9 @@  def _showchangessincetag(repo, ctx, **ar
 
     return len(repo.revs('only(%ld, %s)', revs, tag)) + offset
 
+# teach templater latesttags.changes is switched to (context, mapping) API
+_showchangessincetag._requires = {'repo', 'ctx'}
+
 @templatekeyword('manifest')
 def showmanifest(**args):
     repo, ctx, templ = args[r'repo'], args[r'ctx'], args[r'templ']
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -942,11 +942,7 @@  def latesttag(context, mapping, args):
     pattern = None
     if len(args) == 1:
         pattern = evalstring(context, mapping, args[0])
-
-    # TODO: pass (context, mapping) pair to keyword function
-    props = context._resources.copy()
-    props.update(mapping)
-    return templatekw.showlatesttags(pattern, **pycompat.strkwargs(props))
+    return templatekw.showlatesttags(context, mapping, pattern)
 
 @templatefunc('localdate(date[, tz])')
 def localdate(context, mapping, args):