[PATCH 4 of 4] templater: fix ifcontains() to handle type mismatch gracefully

Yuya Nishihara yuya at tcha.org
Thu Oct 12 09:55:44 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1507813751 -32400
#      Thu Oct 12 22:09:11 2017 +0900
# Node ID 26273fe219811ac0e87f030a2d9c8ed50f4f4768
# Parent  b770d50a3c6256008a6ed6614b21f27e1a09df1b
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.

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -797,10 +797,14 @@ def ifcontains(context, mapping, args):
         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])
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
@@ -3948,6 +3948,9 @@ Test revset function
   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


More information about the Mercurial-devel mailing list