Patchwork [2,of,4] templater: fix ifcontains() to evaluate items argument eagerly

login
register
mail settings
Submitter Yuya Nishihara
Date March 2, 2016, 3:35 p.m.
Message ID <272349484b283018a01c.1456932902@mimosa>
Download mbox | patch
Permalink /patch/13535/
State Accepted
Headers show

Comments

Yuya Nishihara - March 2, 2016, 3:35 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1455376692 -32400
#      Sun Feb 14 00:18:12 2016 +0900
# Node ID 272349484b283018a01c3c580eee3e35c9b2d541
# Parent  fa4f3eb5bd1ea8682cba631c824fb4e6838ed297
templater: fix ifcontains() to evaluate items argument eagerly

See the previous patch for why. An "items" argument may be a string,
a generator, or an arbitrary container object.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -466,7 +466,7 @@  def ifcontains(context, mapping, args):
         raise error.ParseError(_("ifcontains expects three or four arguments"))
 
     item = stringify(args[0][0](context, mapping, args[0][1]))
-    items = args[1][0](context, mapping, args[1][1])
+    items = evalfuncarg(context, mapping, args[1])
 
     if item in items:
         yield args[2][0](context, mapping, args[2][1])
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
@@ -3248,6 +3248,11 @@  Test ifcontains function
   1 is not
   0 is in the string
 
+  $ hg log -T '{rev} {ifcontains(rev, "2 two{" 0"}", "is in the string", "is not")}\n'
+  2 is in the string
+  1 is not
+  0 is in the string
+
   $ hg log --template '{rev} {ifcontains("a", file_adds, "added a", "did not add a")}\n'
   2 did not add a
   1 did not add a