templater: implement _hybrid.__contains__ so that ifcontains can accept dict
authorYuya Nishihara <yuya@tcha.org>
Wed, 18 Feb 2015 23:01:33 +0900
changeset 24240 bd504d90588d
parent 24239 31f9b1b16d1e
child 24241 e7baf88c29c3
templater: implement _hybrid.__contains__ so that ifcontains can accept dict d8fb835376d1 is fine for "{revset()}", but "i.values()[0]" does not work if each item has more than one values such as "{bookmarks}". This fixes the problem by using list.__contains__ or dict.__contains__ appropriately.
mercurial/templatekw.py
mercurial/templater.py
tests/test-command-template.t
--- a/mercurial/templatekw.py	Sun Mar 08 14:46:42 2015 +0900
+++ b/mercurial/templatekw.py	Wed Feb 18 23:01:33 2015 +0900
@@ -12,6 +12,8 @@
 # This helper class allows us to handle both:
 #  "{files}" (legacy command-line-specific list hack) and
 #  "{files % '{file}\n'}" (hgweb-style with inlining and function support)
+# and to access raw values:
+#  "{ifcontains(file, files, ...)}", "{ifcontains(key, extras, ...)}"
 
 class _hybrid(object):
     def __init__(self, gen, values, makemap, joinfmt=None):
@@ -28,6 +30,8 @@
         makemap = self._makemap
         for x in self.values:
             yield makemap(x)
+    def __contains__(self, x):
+        return x in self.values
     def __len__(self):
         return len(self.values)
 
--- a/mercurial/templater.py	Sun Mar 08 14:46:42 2015 +0900
+++ b/mercurial/templater.py	Wed Feb 18 23:01:33 2015 +0900
@@ -330,10 +330,7 @@
     item = stringify(args[0][0](context, mapping, args[0][1]))
     items = args[1][0](context, mapping, args[1][1])
 
-    # Iterating over items gives a formatted string, so we iterate
-    # directly over the raw values.
-    if ((callable(items) and item in [i.values()[0] for i in items()]) or
-        (isinstance(items, str) and item in items)):
+    if item in items:
         yield _evalifliteral(args[2], context, mapping)
     elif len(args) == 4:
         yield _evalifliteral(args[3], context, mapping)
--- a/tests/test-command-template.t	Sun Mar 08 14:46:42 2015 +0900
+++ b/tests/test-command-template.t	Wed Feb 18 23:01:33 2015 +0900
@@ -47,6 +47,9 @@
   fourth (second)
   $ hg log -T '{file_copies % "{source} -> {name}\n"}' -r .
   second -> fourth
+  $ hg log -T '{rev} {ifcontains("fourth", file_copies, "t", "f")}\n' -r .:7
+  8 t
+  7 f
 
 Quoting for ui.logtemplate
 
@@ -2386,6 +2389,10 @@
   2 bar foo
   1 baz
   0 
+  $ hg log --template "{rev} {ifcontains('foo', bookmarks, 't', 'f')}\n"
+  2 t
+  1 f
+  0 f
 
 Test stringify on sub expressions