debugdiscovery: also integrate the discovery output in the json one
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 15 Mar 2021 17:09:42 +0100
changeset 46769 67a2ecea8bd9
parent 46768 3a8cf5b9c820
child 46770 b6ac612445e0
debugdiscovery: also integrate the discovery output in the json one We add a way for formatter to informs code that free output is unwanted, and we incorporate it in the json output. Differential Revision: https://phab.mercurial-scm.org/D10224
mercurial/debugcommands.py
mercurial/formatter.py
tests/test-setdiscovery.t
--- a/mercurial/debugcommands.py	Mon Mar 15 16:55:03 2021 +0100
+++ b/mercurial/debugcommands.py	Mon Mar 15 17:09:42 2021 +0100
@@ -9,6 +9,7 @@
 
 import codecs
 import collections
+import contextlib
 import difflib
 import errno
 import glob
@@ -1089,8 +1090,21 @@
 
     remoterevs, _checkout = hg.addbranchrevs(repo, remote, branches, revs=None)
     localrevs = opts[b'rev']
-    with util.timedcm('debug-discovery') as t:
-        common, hds = doit(localrevs, remoterevs)
+
+    fm = ui.formatter(b'debugdiscovery', opts)
+    if fm.strict_format:
+
+        @contextlib.contextmanager
+        def may_capture_output():
+            ui.pushbuffer()
+            yield
+            data[b'output'] = ui.popbuffer()
+
+    else:
+        may_capture_output = util.nullcontextmanager
+    with may_capture_output():
+        with util.timedcm('debug-discovery') as t:
+            common, hds = doit(localrevs, remoterevs)
 
     # compute all statistics
     heads_common = set(common)
@@ -1141,7 +1155,6 @@
     data[b'nb-ini_und-common'] = len(common_initial_undecided)
     data[b'nb-ini_und-missing'] = len(missing_initial_undecided)
 
-    fm = ui.formatter(b'debugdiscovery', opts)
     fm.startitem()
     fm.data(**pycompat.strkwargs(data))
     # display discovery summary
--- a/mercurial/formatter.py	Mon Mar 15 16:55:03 2021 +0100
+++ b/mercurial/formatter.py	Mon Mar 15 17:09:42 2021 +0100
@@ -178,6 +178,11 @@
 
 
 class baseformatter(object):
+
+    # set to True if the formater output a strict format that does not support
+    # arbitrary output in the stream.
+    strict_format = False
+
     def __init__(self, ui, topic, opts, converter):
         self._ui = ui
         self._topic = topic
@@ -418,6 +423,9 @@
 
 
 class jsonformatter(baseformatter):
+
+    strict_format = True
+
     def __init__(self, ui, out, topic, opts):
         baseformatter.__init__(self, ui, topic, opts, _nullconverter)
         self._out = out
--- a/tests/test-setdiscovery.t	Mon Mar 15 16:55:03 2021 +0100
+++ b/tests/test-setdiscovery.t	Mon Mar 15 17:09:42 2021 +0100
@@ -1734,13 +1734,6 @@
   > --local-as-revs 'first(heads(all()), 25)' \
   > --remote-as-revs 'last(heads(all()), 25)' \
   > --config devel.discovery.randomize=false
-  query 1; heads
-  searching for changes
-  taking quick initial sample
-  query 2; still undecided: 375, sample size is: 81
-  sampling from both directions
-  query 3; still undecided: 3, sample size is: 3
-  3 total queries in *s (glob)
   [
    {
     "elapsed": *, (glob)
@@ -1763,6 +1756,7 @@
     "nb-revs": 400,
     "nb-revs-common": 300,
     "nb-revs-missing": 100,
+    "output": "query 1; heads\nsearching for changes\ntaking quick initial sample\nquery 2; still undecided: 375, sample size is: 81\nsampling from both directions\nquery 3; still undecided: 3, sample size is: 3\n3 total queries in *s\n", (glob)
     "total-roundtrips": 3
    }
   ]