D7432: templates: make {indent("", " ")} be empty

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Mon Nov 18 11:52:09 EST 2019


Closed by commit rHGfa246ada356b: templates: make {indent("", "  ")} be empty (authored by martinvonz).
This revision was automatically updated to reflect the committed changes.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7432?vs=18208&id=18218

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7432/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7432

AFFECTED FILES
  mercurial/templatefilters.py
  mercurial/templatefuncs.py
  relnotes/next
  tests/test-template-functions.t

CHANGE DETAILS

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
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/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/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'



To: martinvonz, #hg-reviewers, pulkit
Cc: mercurial-devel


More information about the Mercurial-devel mailing list