[PATCH 1 of 3 V2] template: add ifcontains template function

Durham Goode durham at fb.com
Fri Feb 14 14:23:55 CST 2014


# HG changeset patch
# User Durham Goode <durham at fb.com>
# Date 1392181800 28800
#      Tue Feb 11 21:10:00 2014 -0800
# Node ID b796fd0082a7712047f99324ef4b8b5df8843f2e
# Parent  23dc778741914b72f2bcdbe1b7bdb6607498eee7
template: add ifcontains template function

Adds a template function with the signature 'ifcontains(item, set, then[,
else])'.  It can be used to do things like '{ifcontains('.hgignore',
file_mods, label(...), ...)}' to color commits that edit the .hgignore file.
A future patch will add the revset() function which will combine with
ifcontains to allow us to color commits if they are in the revset.

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -301,6 +301,19 @@
     elif len(args) == 3:
         yield _evalifliteral(args[2], context, mapping)
 
+def ifcontains(context, mapping, args):
+    if not (3 <= len(args) <= 4):
+        # i18n: "ifcontains" is a keyword
+        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])
+
+    if item in items:
+        yield _evalifliteral(args[2], context, mapping)
+    elif len(args) == 4:
+        yield _evalifliteral(args[3], context, mapping)
+
 def ifeq(context, mapping, args):
     if not (3 <= len(args) <= 4):
         # i18n: "ifeq" is a keyword
@@ -436,6 +449,7 @@
     "fill": fill,
     "get": get,
     "if": if_,
+    "ifcontains": ifcontains,
     "ifeq": ifeq,
     "join": join,
     "label": label,
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
@@ -1651,3 +1651,9 @@
   $ hg log --template '{pad(rev, 20, "-", False)} {author|user}\n'
   1------------------- test
   0------------------- test
+
+Test ifcontains function
+
+  $ hg log --template '{rev} {ifcontains("a", file_adds, "added a", "did not add a")}\n'
+  1 did not add a
+  0 added a


More information about the Mercurial-devel mailing list