[PATCH 02 of 10] templatekw: switch most of showlist template keywords to new API (issue5779)

Yuya Nishihara yuya at tcha.org
Thu Mar 1 20:51:52 EST 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1519544744 -32400
#      Sun Feb 25 16:45:44 2018 +0900
# Node ID d82d2b2d91a5742b57f0b29d7ce3bcb0cd5da90b
# Parent  503e54575af502019158b1bcbf877029a6b601fe
templatekw: switch most of showlist template keywords to new API (issue5779)

Non-trivial changes will follow.

diff --git a/hgext/remotenames.py b/hgext/remotenames.py
--- a/hgext/remotenames.py
+++ b/hgext/remotenames.py
@@ -32,7 +32,6 @@ from mercurial.node import (
 from mercurial import (
     logexchange,
     namespaces,
-    pycompat,
     registrar,
     revsetlang,
     smartset,
@@ -225,11 +224,11 @@ def reposetup(ui, repo):
                 repo._remotenames.nodetobranch().get(node, []))
         repo.names.addnamespace(remotebranchns)
 
- at templatekeyword('remotenames')
-def remotenameskw(**args):
+ at templatekeyword('remotenames', requires={'repo', 'ctx', 'templ'})
+def remotenameskw(context, mapping):
     """List of strings. Remote names associated with the changeset."""
-    args = pycompat.byteskwargs(args)
-    repo, ctx = args['repo'], args['ctx']
+    repo = context.resource(mapping, 'repo')
+    ctx = context.resource(mapping, 'ctx')
 
     remotenames = []
     if 'remotebookmarks' in repo.names:
@@ -238,34 +237,34 @@ def remotenameskw(**args):
     if 'remotebranches' in repo.names:
         remotenames += repo.names['remotebranches'].names(repo, ctx.node())
 
-    return templatekw.showlist('remotename', remotenames, args,
-                               plural='remotenames')
+    return templatekw.compatlist(context, mapping, 'remotename', remotenames,
+                                 plural='remotenames')
 
- at templatekeyword('remotebookmarks')
-def remotebookmarkskw(**args):
+ at templatekeyword('remotebookmarks', requires={'repo', 'ctx', 'templ'})
+def remotebookmarkskw(context, mapping):
     """List of strings. Remote bookmarks associated with the changeset."""
-    args = pycompat.byteskwargs(args)
-    repo, ctx = args['repo'], args['ctx']
+    repo = context.resource(mapping, 'repo')
+    ctx = context.resource(mapping, 'ctx')
 
     remotebmarks = []
     if 'remotebookmarks' in repo.names:
         remotebmarks = repo.names['remotebookmarks'].names(repo, ctx.node())
 
-    return templatekw.showlist('remotebookmark', remotebmarks, args,
-                               plural='remotebookmarks')
+    return templatekw.compatlist(context, mapping, 'remotebookmark',
+                                 remotebmarks, plural='remotebookmarks')
 
- at templatekeyword('remotebranches')
-def remotebrancheskw(**args):
+ at templatekeyword('remotebranches', requires={'repo', 'ctx', 'templ'})
+def remotebrancheskw(context, mapping):
     """List of strings. Remote branches associated with the changeset."""
-    args = pycompat.byteskwargs(args)
-    repo, ctx = args['repo'], args['ctx']
+    repo = context.resource(mapping, 'repo')
+    ctx = context.resource(mapping, 'ctx')
 
     remotebranches = []
     if 'remotebranches' in repo.names:
         remotebranches = repo.names['remotebranches'].names(repo, ctx.node())
 
-    return templatekw.showlist('remotebranch', remotebranches, args,
-                               plural='remotebranches')
+    return templatekw.compatlist(context, mapping, 'remotebranch',
+                                 remotebranches, plural='remotebranches')
 
 def _revsetutil(repo, subset, x, rtypes):
     """utility function to return a set of revs based on the rtypes"""
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -383,17 +383,18 @@ def showbranch(context, mapping):
     ctx = context.resource(mapping, 'ctx')
     return ctx.branch()
 
- at templatekeyword('branches')
-def showbranches(**args):
+ at templatekeyword('branches', requires={'ctx', 'templ'})
+def showbranches(context, mapping):
     """List of strings. The name of the branch on which the
     changeset was committed. Will be empty if the branch name was
     default. (DEPRECATED)
     """
-    args = pycompat.byteskwargs(args)
-    branch = args['ctx'].branch()
+    ctx = context.resource(mapping, 'ctx')
+    branch = ctx.branch()
     if branch != 'default':
-        return showlist('branch', [branch], args, plural='branches')
-    return showlist('branch', [], args, plural='branches')
+        return compatlist(context, mapping, 'branch', [branch],
+                          plural='branches')
+    return compatlist(context, mapping, 'branch', [], plural='branches')
 
 @templatekeyword('bookmarks')
 def showbookmarks(**args):
@@ -480,18 +481,19 @@ def showextras(**args):
     return _hybrid(f, extras, makemap,
                    lambda k: '%s=%s' % (k, util.escapestr(extras[k])))
 
-def _showfilesbystat(args, name, index):
-    repo, ctx, revcache = args['repo'], args['ctx'], args['revcache']
+def _showfilesbystat(context, mapping, name, index):
+    repo = context.resource(mapping, 'repo')
+    ctx = context.resource(mapping, 'ctx')
+    revcache = context.resource(mapping, 'revcache')
     if 'files' not in revcache:
         revcache['files'] = repo.status(ctx.p1(), ctx)[:3]
     files = revcache['files'][index]
-    return showlist(name, files, args, element='file')
+    return compatlist(context, mapping, name, files, element='file')
 
- at templatekeyword('file_adds')
-def showfileadds(**args):
+ at templatekeyword('file_adds', requires={'repo', 'ctx', 'revcache', 'templ'})
+def showfileadds(context, mapping):
     """List of strings. Files added by this changeset."""
-    args = pycompat.byteskwargs(args)
-    return _showfilesbystat(args, 'file_add', 1)
+    return _showfilesbystat(context, mapping, 'file_add', 1)
 
 @templatekeyword('file_copies',
                  requires={'repo', 'ctx', 'cache', 'revcache', 'templ'})
@@ -532,25 +534,23 @@ def showfilecopiesswitch(context, mappin
                       key='name', value='source', fmt='%s (%s)',
                       plural='file_copies')
 
- at templatekeyword('file_dels')
-def showfiledels(**args):
+ at templatekeyword('file_dels', requires={'repo', 'ctx', 'revcache', 'templ'})
+def showfiledels(context, mapping):
     """List of strings. Files removed by this changeset."""
-    args = pycompat.byteskwargs(args)
-    return _showfilesbystat(args, 'file_del', 2)
+    return _showfilesbystat(context, mapping, 'file_del', 2)
 
- at templatekeyword('file_mods')
-def showfilemods(**args):
+ at templatekeyword('file_mods', requires={'repo', 'ctx', 'revcache', 'templ'})
+def showfilemods(context, mapping):
     """List of strings. Files modified by this changeset."""
-    args = pycompat.byteskwargs(args)
-    return _showfilesbystat(args, 'file_mod', 0)
+    return _showfilesbystat(context, mapping, 'file_mod', 0)
 
- at templatekeyword('files')
-def showfiles(**args):
+ at templatekeyword('files', requires={'ctx', 'templ'})
+def showfiles(context, mapping):
     """List of strings. All files modified, added, or removed by this
     changeset.
     """
-    args = pycompat.byteskwargs(args)
-    return showlist('file', args['ctx'].files(), args)
+    ctx = context.resource(mapping, 'ctx')
+    return compatlist(context, mapping, 'file', ctx.files())
 
 @templatekeyword('graphnode', requires={'repo', 'ctx'})
 def showgraphnode(context, mapping):
@@ -911,14 +911,13 @@ def showrevslist(name, revs, **args):
                    lambda x: {name: x, 'ctx': repo[x], 'revcache': {}},
                    pycompat.identity, keytype=int)
 
- at templatekeyword('subrepos')
-def showsubrepos(**args):
+ at templatekeyword('subrepos', requires={'ctx', 'templ'})
+def showsubrepos(context, mapping):
     """List of strings. Updated subrepositories in the changeset."""
-    args = pycompat.byteskwargs(args)
-    ctx = args['ctx']
+    ctx = context.resource(mapping, 'ctx')
     substate = ctx.substate
     if not substate:
-        return showlist('subrepo', [], args)
+        return compatlist(context, mapping, 'subrepo', [])
     psubstate = ctx.parents()[0].substate or {}
     subrepos = []
     for sub in substate:
@@ -927,7 +926,7 @@ def showsubrepos(**args):
     for sub in psubstate:
         if sub not in substate:
             subrepos.append(sub) # removed in ctx
-    return showlist('subrepo', sorted(subrepos), args)
+    return compatlist(context, mapping, 'subrepo', sorted(subrepos))
 
 # don't remove "showtags" definition, even though namespaces will put
 # a helper function for "tags" keyword into "keywords" map automatically,
@@ -943,14 +942,14 @@ def showtermwidth(context, mapping):
     ui = context.resource(mapping, 'ui')
     return ui.termwidth()
 
- at templatekeyword('instabilities')
-def showinstabilities(**args):
+ at templatekeyword('instabilities', requires={'ctx', 'templ'})
+def showinstabilities(context, mapping):
     """List of strings. Evolution instabilities affecting the changeset.
     (EXPERIMENTAL)
     """
-    args = pycompat.byteskwargs(args)
-    return showlist('instability', args['ctx'].instabilities(), args,
-                    plural='instabilities')
+    ctx = context.resource(mapping, 'ctx')
+    return compatlist(context, mapping, 'instability', ctx.instabilities(),
+                      plural='instabilities')
 
 @templatekeyword('verbosity', requires={'ui'})
 def showverbosity(context, mapping):
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -722,10 +722,7 @@ def files(context, mapping, args):
     ctx = context.resource(mapping, 'ctx')
     m = ctx.match([raw])
     files = list(ctx.matches(m))
-    # TODO: pass (context, mapping) pair to keyword function
-    props = context._resources.copy()
-    props.update(mapping)
-    return templatekw.showlist("file", files, props)
+    return templatekw.compatlist(context, mapping, "file", files)
 
 @templatefunc('fill(text[, width[, initialident[, hangindent]]])')
 def fill(context, mapping, args):


More information about the Mercurial-devel mailing list