Mercurial > public > mercurial-scm > hg
diff mercurial/templater.py @ 20518:1e43f15a647f
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.
author | Durham Goode <durham@fb.com> |
---|---|
date | Tue, 11 Feb 2014 21:10:00 -0800 |
parents | 6f3fb6a974e0 |
children | cda9d2b6beab |
line wrap: on
line diff
--- a/mercurial/templater.py Thu Feb 13 17:34:09 2014 -0800 +++ b/mercurial/templater.py Tue Feb 11 21:10:00 2014 -0800 @@ -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,