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

Paolo Bonzini pbonzini at redhat.com
Tue Jun 23 07:09:40 CDT 2009


# HG changeset patch
# User Paolo Bonzini <pbonzini at redhat.com>
# Date 1245752617 -7200
# Node ID a7f47d6a203387ba5bcb4021a2de01634af47242
# Parent  d244ee52ac30c4f8b557140616f992d8726780e8
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
@@ -498,6 +498,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
@@ -76,6 +76,13 @@
         return text.splitlines(1)[0].rstrip('\r\n')
     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.'''
@@ -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