formatter: make labels work with templated output
authorKostia Balytskyi <ikostia@fb.com>
Tue, 08 Mar 2016 04:08:33 -0800
changeset 28384 3356bf61fa25
parent 28383 e13e0e189990
child 28385 3f9e25a42e69
formatter: make labels work with templated output To describe the bug this fix is addressing, one can do ``$ hg status -T "{label('red', path)}\n" --color=debug`` and observe that the label is not applied before my fix and applied with it.
mercurial/cmdutil.py
mercurial/formatter.py
mercurial/templater.py
tests/test-status-color.t
--- a/mercurial/cmdutil.py	Tue Mar 08 14:32:03 2016 +0000
+++ b/mercurial/cmdutil.py	Tue Mar 08 04:08:33 2016 -0800
@@ -1502,6 +1502,7 @@
         props['templ'] = self.t
         props['ctx'] = ctx
         props['repo'] = self.repo
+        props['ui'] = self.repo.ui
         props['revcache'] = {'copies': copies}
         props['cache'] = self.cache
 
--- a/mercurial/formatter.py	Tue Mar 08 14:32:03 2016 +0000
+++ b/mercurial/formatter.py	Tue Mar 08 04:08:33 2016 -0800
@@ -153,7 +153,7 @@
         self._topic = topic
         self._t = gettemplater(ui, topic, opts.get('template', ''))
     def _showitem(self):
-        g = self._t(self._topic, **self._item)
+        g = self._t(self._topic, ui=self._ui, **self._item)
         self._ui.write(templater.stringify(g))
 
 def lookuptemplate(ui, topic, tmpl):
--- a/mercurial/templater.py	Tue Mar 08 14:32:03 2016 +0000
+++ b/mercurial/templater.py	Tue Mar 08 04:08:33 2016 -0800
@@ -549,16 +549,15 @@
 
     thing = evalstring(context, mapping, args[1])
 
-    # apparently, repo could be a string that is the favicon?
-    repo = mapping.get('repo', '')
-    if isinstance(repo, str):
+    ui = mapping.get('ui', '')
+    if isinstance(ui, str):
         return thing
 
     # preserve unknown symbol as literal so effects like 'red', 'bold',
     # etc. don't need to be quoted
     label = evalstringliteral(context, mapping, args[0])
 
-    return repo.ui.label(thing, label)
+    return ui.label(thing, label)
 
 def latesttag(context, mapping, args):
     """:latesttag([pattern]): The global tags matching the given pattern on the
--- a/tests/test-status-color.t	Tue Mar 08 14:32:03 2016 +0000
+++ b/tests/test-status-color.t	Tue Mar 08 04:08:33 2016 -0800
@@ -30,6 +30,15 @@
   [status.unknown|? ][status.unknown|b/in_b]
   [status.unknown|? ][status.unknown|in_root]
 
+hg status with template
+  $ hg status -T "{label('red', path)}\n" --color=debug
+  [red|a/1/in_a_1]
+  [red|a/in_a]
+  [red|b/1/in_b_1]
+  [red|b/2/in_b_2]
+  [red|b/in_b]
+  [red|in_root]
+
 hg status . in repo root:
 
   $ hg status --color=always .