perf: define formatter locally for Mercurial earlier than 2.2
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Sun, 09 Oct 2016 01:03:18 +0900
changeset 30147 423bf74d2e5b
parent 30146 148ccd1d9f2f
child 30148 04f2b980df3c
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).
contrib/perf.py
--- a/contrib/perf.py	Sun Oct 09 01:03:18 2016 +0900
+++ b/contrib/perf.py	Sun Oct 09 01:03:18 2016 +0900
@@ -138,8 +138,42 @@
     # 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"):