diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py --- a/mercurial/templatefilters.py +++ b/mercurial/templatefilters.py @@ -299,7 +299,7 @@ return dateutil.datestr(text, b'%Y-%m-%d %H:%M:%S %1%2') -def indent(text, prefix): +def indent(text, prefix, firstline=b''): '''indent each non-empty line of text after first with prefix.''' lines = text.splitlines() num_lines = len(lines) @@ -308,8 +308,8 @@ def indenter(): for i in pycompat.xrange(num_lines): l = lines[i] - if i and l.strip(): - yield prefix + if l.strip(): + yield prefix if i else firstline yield l if i < num_lines - 1 or endswithnewline: yield b'\n' diff --git a/mercurial/templatefuncs.py b/mercurial/templatefuncs.py --- a/mercurial/templatefuncs.py +++ b/mercurial/templatefuncs.py @@ -310,13 +310,11 @@ text = evalstring(context, mapping, args[0]) indent = evalstring(context, mapping, args[1]) + firstline = indent if len(args) == 3: firstline = evalstring(context, mapping, args[2]) - else: - firstline = indent - # the indent function doesn't indent the first line, so we do it here - return templatefilters.indent(firstline + text, indent) + return templatefilters.indent(text, indent, firstline=firstline) @templatefunc(b'get(dict, key)') diff --git a/relnotes/next b/relnotes/next --- a/relnotes/next +++ b/relnotes/next @@ -6,6 +6,9 @@ == Bug Fixes == + * The `indent()` template function was documented to not indent empty lines, + but it still indented the first line even if it was empty. It no longer does + that. == Backwards Compatibility Changes == diff --git a/tests/test-template-functions.t b/tests/test-template-functions.t --- a/tests/test-template-functions.t +++ b/tests/test-template-functions.t @@ -1507,16 +1507,16 @@ Test indent with empty first line $ hg version -T "{indent('', '>> ')}\n" - >> + $ hg version -T "{indent(' > second', '>> ')}\n" - >> + >> second $ hg version -T "{indent(' > second', '>> ', ' > ')}\n" - > + >> second Test with non-strings like dates