Patchwork [3,of,3,STABLE] formatter: fix handling of None value in templater mapping

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 27, 2019, 4:12 a.m.
Message ID <684cd0743012b7240416.1572149575@mimosa>
Download mbox | patch
Permalink /patch/42597/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 27, 2019, 4:12 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1572148149 -32400
#      Sun Oct 27 12:49:09 2019 +0900
# Branch stable
# Node ID 684cd0743012b7240416b02bac65c494c344bf25
# Parent  a3e8a9303f3eef7f529c0695d24d81b7c01a5344
formatter: fix handling of None value in templater mapping

For historical reasons, None in mapping dict means there's no such keyword,
and falls back to b"". That's fine in log templates where mapping item is
generally a callable returning a value (which may be None,) but the formatter
directly puts an "evaluated" value in the mapping. So the None value has
to be lifted to wrappedvalue(None) to avoid confusion in the template engine.
Augie Fackler - Oct. 28, 2019, 7:19 p.m.
On Sun, Oct 27, 2019 at 01:12:55PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1572148149 -32400
> #      Sun Oct 27 12:49:09 2019 +0900
> # Branch stable
> # Node ID 684cd0743012b7240416b02bac65c494c344bf25
> # Parent  a3e8a9303f3eef7f529c0695d24d81b7c01a5344
> formatter: fix handling of None value in templater mapping

queued, thanks

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -515,6 +515,10 @@  class templateformatter(baseformatter):
         if part not in self._parts:
             return
         ref = self._parts[part]
+        # None can't be put in the mapping dict since it means <unset>
+        for k, v in item.items():
+            if v is None:
+                item[k] = templateutil.wrappedvalue(v)
         self._out.write(self._t.render(ref, item))
 
     @util.propertycache
diff --git a/tests/test-config.t b/tests/test-config.t
--- a/tests/test-config.t
+++ b/tests/test-config.t
@@ -122,7 +122,7 @@  Test config default of various types:
   ]
   $ hg config --config auth.cookiefile= auth -T'json(defaultvalue)'
   [
-   {"defaultvalue": ""}
+   {"defaultvalue": null}
   ]
   $ hg config --config auth.cookiefile= auth -T'{defaultvalue}\n'