templater: fix if() to not evaluate False as bool('False')
authorYuya Nishihara <yuya@tcha.org>
Thu, 18 Aug 2016 16:29:22 +0900
changeset 29816 034412ca28c3
parent 29815 0d5cc0c18b4e
child 29817 cc11079644fc
templater: fix if() to not evaluate False as bool('False') Before, False was True. This patch fixes the issue by processing True/False transparently. The other values (including integer 0) are tested as strings for backward compatibility, which means "if(latesttagdistance)" never be False. Should we change the behavior of "if(0)" as well?
mercurial/templater.py
tests/test-branches.t
--- a/mercurial/templater.py	Thu Aug 18 15:55:07 2016 +0900
+++ b/mercurial/templater.py	Thu Aug 18 16:29:22 2016 +0900
@@ -289,6 +289,15 @@
         thing = stringify(thing)
     return thing
 
+def evalboolean(context, mapping, arg):
+    func, data = arg
+    thing = func(context, mapping, data)
+    if isinstance(thing, bool):
+        return thing
+    # other objects are evaluated as strings, which means 0 is True, but
+    # empty dict/list should be False as they are expected to be ''
+    return bool(stringify(thing))
+
 def evalinteger(context, mapping, arg, err):
     v = evalfuncarg(context, mapping, arg)
     try:
@@ -560,7 +569,7 @@
         # i18n: "if" is a keyword
         raise error.ParseError(_("if expects two or three arguments"))
 
-    test = evalstring(context, mapping, args[0])
+    test = evalboolean(context, mapping, args[0])
     if test:
         yield args[1][0](context, mapping, args[1][1])
     elif len(args) == 3:
--- a/tests/test-branches.t	Thu Aug 18 15:55:07 2016 +0900
+++ b/tests/test-branches.t	Thu Aug 18 16:29:22 2016 +0900
@@ -516,6 +516,8 @@
    }
   ]
 
+  $ hg branches --closed -T '{if(closed, "{branch}\n")}'
+  c
 
 Tests of revision branch name caching