Mercurial > public > mercurial-scm > hg-stable
diff mercurial/templater.py @ 22434:40ce05b50148
templater: add "diff" template function
"diff" allows to embed changes in the target revision into template
output, even if the command itself doesn't take "--patch" option
Combination of "[committemplate]" configuration and "diff" template
function can achieve the feature like issue231 ("option to have diff
displayed in commit editor buffer")
http://bz.selenic.com/show_bug.cgi?id=231
For example, templating below can be used to add each "diff" output
lines "HG: " prefix::
{splitlines(diff) % 'HG: {line}\n'}
This patch implements "diff" not as "a template keyword" but as "a
template function" to take include/exclude patterns at runtime.
It allows to specify target files of command (by -I/-X command line
options) and "diff" separately.
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Thu, 28 Aug 2014 22:45:36 +0900 |
parents | 5678b0e3608f |
children | 92b54547ac5d |
line wrap: on
line diff
--- a/mercurial/templater.py Tue Sep 16 11:08:29 2014 -0500 +++ b/mercurial/templater.py Thu Aug 28 22:45:36 2014 +0900 @@ -225,6 +225,23 @@ return util.datestr(date, fmt) return util.datestr(date) +def diff(context, mapping, args): + if len(args) > 2: + # i18n: "diff" is a keyword + raise error.ParseError(_("diff expects one, two or no arguments")) + + def getpatterns(i): + if i < len(args): + s = args[i][1].strip() + if s: + return [s] + return [] + + ctx = mapping['ctx'] + chunks = ctx.diff(match=ctx.match([], getpatterns(0), getpatterns(1))) + + return ''.join(chunks) + def fill(context, mapping, args): if not (1 <= len(args) <= 4): raise error.ParseError(_("fill expects one to four arguments")) @@ -516,6 +533,7 @@ funcs = { "date": date, + "diff": diff, "fill": fill, "get": get, "if": if_,