[PATCH 1 of 2] add `first' template filter

Paolo Bonzini pbonzini at redhat.com
Thu Jul 2 01:37:56 CDT 2009


# HG changeset patch
# User Paolo Bonzini <pbonzini at redhat.com>
# Date 1246515264 -7200
# Node ID 189b8fdabf1a0fb54277ee7ff6244fa27f04ab18
# Parent  b81baf9e4dd6fe5e475fb431977e838761632ef2
add `first' template filter

The `first' filter operates on lists and returns the first item.
It can be useful to extract the first parent, for example.

diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -424,6 +424,7 @@
     - fill68: Any text. Wraps the text to fit in 68 columns.
     - fill76: Any text. Wraps the text to fit in 76 columns.
     - firstline: Any text. Returns the first line of text.
+    - first: Any list. Returns the first non-empty item of the list.
     - nonempty: Any text. Returns '(none)' if the string is empty.
     - hgdate: Date. Returns the date as a pair of numbers:
           "1157407993 25200" (Unix timestamp, timezone offset).
diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -77,6 +77,13 @@
     except IndexError:
         return ''
 
+def first(list):
+    '''return the first non-empty item of list'''
+    for i in list:
+        if i is not None:
+            return i
+    return None
+
 def nl2br(text):
     '''replace raw newlines with xhtml line breaks.'''
     return text.replace('\n', '<br/>\n')
@@ -187,6 +194,7 @@
     "fill68": lambda x: fill(x, width=68),
     "fill76": lambda x: fill(x, width=76),
     "firstline": firstline,
+    "first": first,
     "tabindent": lambda x: indent(x, '\t'),
     "hgdate": lambda x: "%d %d" % x,
     "isodate": lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2'),
diff --git a/tests/test-command-template b/tests/test-command-template
--- a/tests/test-command-template
+++ b/tests/test-command-template
@@ -110,6 +110,7 @@
 hg log --template '{date|isodatesec}\n'
 hg log --template '{date|rfc822date}\n'
 hg log --template '{desc|firstline}\n'
+hg log --debug --template '{parents|first}\n'
 hg log --template '{node|short}\n'
 hg log --template '<changeset author="{author|xmlescape}"/>\n'
 
diff --git a/tests/test-command-template.out b/tests/test-command-template.out
--- a/tests/test-command-template.out
+++ b/tests/test-command-template.out
@@ -643,6 +643,15 @@
 no person
 other 1
 line 1
+7:29114dbae42b9f078cf2714dbe3a86bba8ec7453 
+-1:0000000000000000000000000000000000000000 
+5:13207e5a10d9fd28ec424934298e176197f2c67f 
+3:10e46f2dcbf4823578cf180f33ecf0b957964c47 
+3:10e46f2dcbf4823578cf180f33ecf0b957964c47 
+2:97054abb4ab824450e9164180baf491ae0078465 
+1:b608e9d1a3f0273ccf70fb85fd6866b3482bf965 
+0:1e4e1b8f71e05681d422154f5421e385fec3454f 
+-1:0000000000000000000000000000000000000000 
 946e2bd9c565
 29114dbae42b
 c7b487c6c50e




More information about the Mercurial-devel mailing list