[PATCH 1 of 5] templater: port formatnode filter from changeset_templater

Yuya Nishihara yuya at tcha.org
Sat Feb 25 14:51:30 UTC 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1488007618 -32400
#      Sat Feb 25 16:26:58 2017 +0900
# Node ID c56e0c043a22646371f2828d75617ee51d12bb23
# Parent  b4cb86ab4c719eb615a4308eafd8b1386a511eeb
# EXP-Topic formatter
templater: port formatnode filter from changeset_templater

This slightly reduces the difference between changeset_templater and formatter,
and helps extending formatter to support changeset templating.

New formatnode() is not a template filter, but a function since a filter
cannot access to ui. And it's marked as DEPRECATED since I think it exists
only for compatibility reasons.

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1442,8 +1442,6 @@ class changeset_templater(changeset_prin
 
     def __init__(self, ui, repo, matchfn, diffopts, tmpl, mapfile, buffered):
         changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered)
-        formatnode = ui.debugflag and (lambda x: x) or (lambda x: x[:12])
-        filters = {'formatnode': formatnode}
         defaulttempl = {
             'parent': '{rev}:{node|formatnode} ',
             'manifest': '{rev}:{node|formatnode}',
@@ -1455,11 +1453,10 @@ class changeset_templater(changeset_prin
         defaulttempl['filecopy'] = defaulttempl['file_copy']
         assert not (tmpl and mapfile)
         if mapfile:
-            self.t = templater.templater.frommapfile(mapfile, filters=filters,
+            self.t = templater.templater.frommapfile(mapfile,
                                                      cache=defaulttempl)
         else:
             self.t = formatter.maketemplater(ui, 'changeset', tmpl,
-                                             filters=filters,
                                              cache=defaulttempl)
 
         self.cache = {}
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -544,6 +544,19 @@ def fill(context, mapping, args):
 
     return templatefilters.fill(text, width, initindent, hangindent)
 
+ at templatefunc('formatnode(node)')
+def formatnode(context, mapping, args):
+    """Obtain the preferred form of a changeset hash. (DEPRECATED)"""
+    if len(args) != 1:
+        # i18n: "formatnode" is a keyword
+        raise error.ParseError(_("formatnode expects one argument"))
+
+    ui = mapping['ui']
+    node = evalstring(context, mapping, args[0])
+    if ui.debugflag:
+        return node
+    return templatefilters.short(node)
+
 @templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])')
 def pad(context, mapping, args):
     """Pad text with a


More information about the Mercurial-devel mailing list