Patchwork [5,of,8,V2] perf: define formatter locally for Mercurial earlier than 2.2

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Oct. 8, 2016, 4:18 p.m.
Message ID <1fb97069cef8454d4658.1475943505@feefifofum>
Download mbox | patch
Permalink /patch/16946/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - Oct. 8, 2016, 4:18 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1475942598 -32400
#      Sun Oct 09 01:03:18 2016 +0900
# Node ID 1fb97069cef8454d4658592e44edbaab11d5e34d
# Parent  00309872ed26d0dea89abe417a97f6d838de1158
perf: define formatter locally for Mercurial earlier than 2.2

Before this patch, using ui.formatter() prevents perf.py from
measuring performance with Mercurial earlier than 2.2 (or
ae5f92e154d3), because ui.formatter() isn't available in such
Mercurial, even though there are some code paths for Mercurial earlier
than 2.2 in perf.py.

For example, setting "_prereadsize" attribute in perfindex() and
perfnodelookup() is effective only with hg earlier than 1.8 (or
61c9bc3da402).

This patch defines formatter class locally, and use it instead of the
value returned by ui.formatter(), if perf.py is used with Mercurial
earlier than 2.2.

In this case, we don't need to think about -T/--template option for
formatter, because previous patch made -T/--template disabled for
perf.py with Mercurial earlier than 3.2 (or 7a7eed5176a4).

Patch

diff --git a/contrib/perf.py b/contrib/perf.py
--- a/contrib/perf.py
+++ b/contrib/perf.py
@@ -138,8 +138,42 @@  def gettimer(ui, opts=None):
     # redirect all to stderr
     ui = ui.copy()
     ui.fout = ui.ferr
+
     # get a formatter
-    fm = ui.formatter('perf', opts)
+    uiformatter = getattr(ui, 'formatter', None)
+    if uiformatter:
+        fm = uiformatter('perf', opts)
+    else:
+        # for "historical portability":
+        # define formatter locally, because ui.formatter has been
+        # available since 2.2 (or ae5f92e154d3)
+        from mercurial import node
+        class defaultformatter(object):
+            """Minimized composition of baseformatter and plainformatter
+            """
+            def __init__(self, ui, topic, opts):
+                self._ui = ui
+                if ui.debugflag:
+                    self.hexfunc = node.hex
+                else:
+                    self.hexfunc = node.short
+            def __nonzero__(self):
+                return False
+            def startitem(self):
+                pass
+            def data(self, **data):
+                pass
+            def write(self, fields, deftext, *fielddata, **opts):
+                self._ui.write(deftext % fielddata, **opts)
+            def condwrite(self, cond, fields, deftext, *fielddata, **opts):
+                if cond:
+                    self._ui.write(deftext % fielddata, **opts)
+            def plain(self, text, **opts):
+                self._ui.write(text, **opts)
+            def end(self):
+                pass
+        fm = defaultformatter(ui, 'perf', opts)
+
     # stub function, runs code only once instead of in a loop
     # experimental config: perf.stub
     if ui.configbool("perf", "stub"):