Patchwork [2,of,3] templater: apply "stringify()" on sub expression to get string correctly

login
register
mail settings
Submitter Katsunori FUJIWARA
Date March 9, 2014, 4:09 p.m.
Message ID <a54c0d830499da186d56.1394381364@feefifofum>
Download mbox | patch
Permalink /patch/3900/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - March 9, 2014, 4:09 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1394380903 -32400
#      Mon Mar 10 01:01:43 2014 +0900
# Branch stable
# Node ID a54c0d830499da186d56bf071c3e698eb818de0c
# Parent  7e627fe63e5e74b0b694030957c49d727ec79ba6
templater: apply "stringify()" on sub expression to get string correctly

Templating syntax allows nested expression to be specified as parts
below, but they are evaluated as a generator and don't work correctly.

  - 'sep' of 'join(list, sep)'
  - 'text' and 'chars' of 'strip(text, chars)'

In the former case, 'sep' returns expected string only for the first
separation, and empty one for the second or later, because the
generator has only one element.

In the latter case, templating is aborted by exception, because the
generator doesn't have 'strip()' method (as 'text') and can't be
passed as the argument to 'str.strip()' (as 'chars').

This patch applies "stringify()" on these sub expression to get string
correctly.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -296,7 +296,7 @@ 
 
     joiner = " "
     if len(args) > 1:
-        joiner = args[1][0](context, mapping, args[1][1])
+        joiner = stringify(args[1][0](context, mapping, args[1][1]))
 
     first = True
     for x in joinset:
@@ -328,9 +328,9 @@ 
     if not (1 <= len(args) <= 2):
         raise error.ParseError(_("strip expects one or two arguments"))
 
-    text = args[0][0](context, mapping, args[0][1])
+    text = stringify(args[0][0](context, mapping, args[0][1]))
     if len(args) == 2:
-        chars = args[1][0](context, mapping, args[1][1])
+        chars = stringify(args[1][0](context, mapping, args[1][1]))
         return text.strip(chars)
     return text.strip()
 
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -1659,3 +1659,12 @@ 
 
   $ hg log -r 0 --template '{if(branches, "yes", "no")}\n'
   no
+
+  $ cd ..
+
+Test stringify on sub expressions
+
+  $ hg log -R a -r 8 --template '{join(files, if("1", if("1", ", ")))}\n'
+  fourth, second, third
+  $ hg log -R a -r 8 --template '{strip(if("1", if("1", "-abc-")), if("1", if("1", "-")))}\n'
+  abc