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

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 12, 2017, 1:55 p.m.
Message ID <26273fe219811ac0e87f.1507816544@mimosa>
Download mbox | patch
Permalink /patch/24781/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 12, 2017, 1:55 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@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.
Augie Fackler - Oct. 13, 2017, 4:27 p.m.
On Thu, Oct 12, 2017 at 10:55:44PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@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

queued, thanks

Patch

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