[PATCH] templatefilters: avoid traceback with bogus date|localdate (issue3344)

Christian Ebert blacktrash at gmx.net
Wed Aug 8 05:50:08 CDT 2012

# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1344422967 -3600
# Node ID 3522e88e61fa4e26949b49256fb3b8e3e67803ea
# Parent  b131e24e2984a610d77e124dd3f58b2b5eda6d1a
templatefilters: avoid traceback with bogus date|localdate (issue3344)

hg log --template '{foo|localdate}\n' now yields the current
date always, similar to {foo|shortdate}.

Do the same for the utcdate filters provided by the keyword

The rationale for not aborting is that the invalid date must have been
given explicitly by the user and does not come from Mercurial.

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -113,11 +113,19 @@
     'kwfiles.ignoredunknown': 'none'
+def _utcdate(text, fmt):
+    '''Handle omission of timezone in utcdate filters.'''
+    try:
+        return util.datestr((text[0], 0), fmt)
+    except IndexError:
+        # avoid traceback caused by bogus dates
+        return util.datestr(text, fmt)
 # date like in cvs' $Date
 def utcdate(text):
     ''':utcdate: Date. Returns a UTC-date in this format: "2009/08/18 11:00:13".
-    return util.datestr((text[0], 0), '%Y/%m/%d %H:%M:%S')
+    return _utcdate(text, '%Y/%m/%d %H:%M:%S')
 # date like in svn's $Date
 def svnisodate(text):
     ''':svnisodate: Date. Returns a date in this format: "2009-08-18 13:00:13
@@ -129,7 +137,7 @@
     ''':svnutcdate: Date. Returns a UTC-date in this format: "2009-08-18
-    return util.datestr((text[0], 0), '%Y-%m-%d %H:%M:%SZ')
+    return _utcdate(text, '%Y-%m-%d %H:%M:%SZ')
 templatefilters.filters.update({'utcdate': utcdate,
                                 'svnisodate': svnisodate,
diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -221,7 +221,10 @@
 def localdate(text):
     """:localdate: Date. Converts a date to local date."""
-    return (text[0], util.makedate()[1])
+    try:
+        return (text[0], util.makedate()[1])
+    except IndexError:
+        return util.makedate()
 def nonempty(str):
     """:nonempty: Any text. Returns '(none)' if the string is empty."""

More information about the Mercurial-devel mailing list