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

Kostia Balytskyi ikostia at fb.com
Thu Mar 3 16:27:57 UTC 2016


# HG changeset patch
# User Kostia Balytskyi <ikostia at fb.com>
# Date 1457022116 28800
#      Thu Mar 03 08:21:56 2016 -0800
# Node ID 09dd1768aafb67bc4c342fb5317b18c085231b7e
# 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
@@ -194,7 +194,7 @@ def lookuptemplate(ui, topic, tmpl):
 
 def gettemplater(ui, topic, spec):
     tmpl, mapfile = lookuptemplate(ui, topic, spec)
-    t = templater.templater(mapfile, {})
+    t = templater.templater(mapfile, {}, ui=ui)
     if tmpl:
         t.cache[topic] = tmpl
     return t
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -863,7 +863,7 @@ class TemplateNotFound(error.Abort):
 class templater(object):
 
     def __init__(self, mapfile, filters=None, defaults=None, cache=None,
-                 minchunk=1024, maxchunk=65536):
+                 minchunk=1024, maxchunk=65536, ui=None):
         '''set up template engine.
         mapfile is name of file to read map definitions from.
         filters is dict of functions. each transforms a value into another.
@@ -886,6 +886,7 @@ class templater(object):
         self.defaults = defaults
         self.minchunk, self.maxchunk = minchunk, maxchunk
         self.ecache = {}
+        self.ui = ui
 
         if not mapfile:
             return
@@ -934,6 +935,7 @@ class templater(object):
                                                  self.filters, self.defaults)
         proc = self.ecache[ttype]
 
+        mapping['__ui'] = self.ui
         stream = proc.process(t, mapping)
         if self.minchunk:
             stream = util.increasingchunks(stream, min=self.minchunk,
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