[PATCH v2] formatter: fixing bug that prevented labels from working

Kostia Balytskyi ikostia at fb.com
Fri Mar 4 05:42:49 EST 2016


# HG changeset patch
# User Kostia Balytskyi <ikostia at fb.com>
# Date 1457087941 28800
#      Fri Mar 04 02:39:01 2016 -0800
# Node ID 4fe6e433f58e2a358c083e09e2e90735b289f01c
# Parent  4f7a5e4f2daff0a65aa470d9f70365ad55aaa100
formatter: fixing bug that prevented labels from working

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.

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -491,9 +491,13 @@ def templatelabel(context, mapping, args
 
     thing = args[1][0](context, mapping, args[1][1])
 
+    ui = mapping.get('__ui')
     # apparently, repo could be a string that is the favicon?
     repo = mapping.get('repo', '')
-    if isinstance(repo, str):
+    if not ui and not isinstance(repo, str):
+        ui = repo.ui
+
+    if ui is None:
         return thing
 
     label = args[0][0](context, mapping, args[0][1])
@@ -501,7 +505,7 @@ def templatelabel(context, mapping, args
     thing = templater.stringify(thing)
     label = templater.stringify(label)
 
-    return repo.ui.label(thing, label)
+    return ui.label(thing, label)
 
 def uisetup(ui):
     if ui.plain():
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -152,8 +152,9 @@ class templateformatter(baseformatter):
         baseformatter.__init__(self, ui, topic, opts)
         self._topic = topic
         self._t = gettemplater(ui, topic, opts.get('template', ''))
+        self.ui = ui
     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):
diff --git a/tests/test-status-color.t b/tests/test-status-color.t
--- a/tests/test-status-color.t
+++ b/tests/test-status-color.t
@@ -30,6 +30,15 @@ hg status in repo root:
   [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 .


More information about the Mercurial-devel mailing list