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

login
register
mail settings
Submitter Kostia Balytskyi
Date March 4, 2016, 10:42 a.m.
Message ID <4fe6e433f58e2a358c08.1457088169@dev1902.lla1.facebook.com>
Download mbox | patch
Permalink /patch/13589/
State Changes Requested
Delegated to: Yuya Nishihara
Headers show

Comments

Kostia Balytskyi - March 4, 2016, 10:42 a.m.
# HG changeset patch
# User Kostia Balytskyi <ikostia@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.
Yuya Nishihara - March 5, 2016, 1:46 p.m.
On Fri, 4 Mar 2016 02:42:49 -0800, Kostia Balytskyi wrote:
> # HG changeset patch
> # User Kostia Balytskyi <ikostia@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

(FYI: I'm going to send patches that will move this function to templater)

It seems awkward that label() looks for a ui object by several names and falls
back to do nothing. Can "ui" be a mandatory parameter? Both templateformatter
and changeset_templater can pass "ui" to templater.

Regarding the name, I think mapping['ui'] is okay. At some point, we should
split the mapping dict to public keywords and internal resources, but that
will require lots of work. Until then, 'ui' sounds good as we have 'ctx' and
'repo'.

> --- 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

It has self._ui.

Patch

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 .