# HG changeset patch # User FUJIWARA Katsunori # Date 1409233536 -32400 # Node ID 40ce05b50148e23bfe9a0743f95e192f307b06c9 # Parent ae1932dce9c1df6fdacbd08ab3ca0dd8a4522582 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. diff -r ae1932dce9c1 -r 40ce05b50148 mercurial/help/templates.txt --- a/mercurial/help/templates.txt Tue Sep 16 11:08:29 2014 -0500 +++ b/mercurial/help/templates.txt Thu Aug 28 22:45:36 2014 +0900 @@ -43,6 +43,8 @@ - date(date[, fmt]) +- diff([includepattern [, excludepattern]]) + - fill(text[, width]) - get(dict, key) diff -r ae1932dce9c1 -r 40ce05b50148 mercurial/templater.py --- 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_, diff -r ae1932dce9c1 -r 40ce05b50148 tests/test-command-template.t --- a/tests/test-command-template.t Tue Sep 16 11:08:29 2014 -0500 +++ b/tests/test-command-template.t Thu Aug 28 22:45:36 2014 +0900 @@ -1794,6 +1794,61 @@ hg: parse error: unknown function 'foo' [255] +Test diff function: + + $ hg diff -c 8 + diff -r 29114dbae42b -r 95c24699272e fourth + --- /dev/null Thu Jan 01 00:00:00 1970 +0000 + +++ b/fourth Wed Jan 01 10:01:00 2020 +0000 + @@ -0,0 +1,1 @@ + +second + diff -r 29114dbae42b -r 95c24699272e second + --- a/second Mon Jan 12 13:46:40 1970 +0000 + +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 + @@ -1,1 +0,0 @@ + -second + diff -r 29114dbae42b -r 95c24699272e third + --- /dev/null Thu Jan 01 00:00:00 1970 +0000 + +++ b/third Wed Jan 01 10:01:00 2020 +0000 + @@ -0,0 +1,1 @@ + +third + + $ hg log -r 8 -T "{diff()}" + diff -r 29114dbae42b -r 95c24699272e fourth + --- /dev/null Thu Jan 01 00:00:00 1970 +0000 + +++ b/fourth Wed Jan 01 10:01:00 2020 +0000 + @@ -0,0 +1,1 @@ + +second + diff -r 29114dbae42b -r 95c24699272e second + --- a/second Mon Jan 12 13:46:40 1970 +0000 + +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 + @@ -1,1 +0,0 @@ + -second + diff -r 29114dbae42b -r 95c24699272e third + --- /dev/null Thu Jan 01 00:00:00 1970 +0000 + +++ b/third Wed Jan 01 10:01:00 2020 +0000 + @@ -0,0 +1,1 @@ + +third + + $ hg log -r 8 -T "{diff('glob:f*')}" + diff -r 29114dbae42b -r 95c24699272e fourth + --- /dev/null Thu Jan 01 00:00:00 1970 +0000 + +++ b/fourth Wed Jan 01 10:01:00 2020 +0000 + @@ -0,0 +1,1 @@ + +second + + $ hg log -r 8 -T "{diff('', 'glob:f*')}" + diff -r 29114dbae42b -r 95c24699272e second + --- a/second Mon Jan 12 13:46:40 1970 +0000 + +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 + @@ -1,1 +0,0 @@ + -second + diff -r 29114dbae42b -r 95c24699272e third + --- /dev/null Thu Jan 01 00:00:00 1970 +0000 + +++ b/third Wed Jan 01 10:01:00 2020 +0000 + @@ -0,0 +1,1 @@ + +third + $ cd ..