templater: fix ifcontains() to handle type mismatch gracefully
authorYuya Nishihara <yuya@tcha.org>
Thu, 12 Oct 2017 22:09:11 +0900
changeset 34659 3edfd472f3cb
parent 34658 dbe1f5118864
child 34660 d00ec62d156f
templater: fix ifcontains() to handle type mismatch gracefully This was unintentionally changed in ee0d74083a22. Since ifcontains() takes needle of any types, it shouldn't abort depending on the given container type.
mercurial/templater.py
tests/test-command-template.t
--- a/mercurial/templater.py	Thu Oct 12 21:56:13 2017 +0900
+++ b/mercurial/templater.py	Thu Oct 12 22:09:11 2017 +0900
@@ -797,10 +797,14 @@
         raise error.ParseError(_("ifcontains expects three or four arguments"))
 
     haystack = evalfuncarg(context, mapping, args[1])
-    needle = evalastype(context, mapping, args[0],
-                        getattr(haystack, 'keytype', None) or bytes)
+    try:
+        needle = evalastype(context, mapping, args[0],
+                            getattr(haystack, 'keytype', None) or bytes)
+        found = (needle in haystack)
+    except error.ParseError:
+        found = False
 
-    if needle in haystack:
+    if found:
         yield evalrawexp(context, mapping, args[2])
     elif len(args) == 4:
         yield evalrawexp(context, mapping, args[3])
--- a/tests/test-command-template.t	Thu Oct 12 21:56:13 2017 +0900
+++ b/tests/test-command-template.t	Thu Oct 12 22:09:11 2017 +0900
@@ -3948,6 +3948,9 @@
   1 match rev
   0 not match rev
 
+  $ hg log -T '{ifcontains(desc, revset(":"), "", "type not match")}\n' -l1
+  type not match
+
   $ hg log --template '{rev} Parents: {revset("parents(%s)", rev)}\n'
   2 Parents: 1
   1 Parents: 0