# HG changeset patch # User Pierre-Yves David # Date 2021-03-15 16:09:42 # Node ID 67a2ecea8bd9dab2f28cb05b6c900de02eaf9837 # Parent 3a8cf5b9c8204aec5212d5aaf6263c66ce823a4b 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 diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py --- a/mercurial/debugcommands.py +++ b/mercurial/debugcommands.py @@ -9,6 +9,7 @@ from __future__ import absolute_import import codecs import collections +import contextlib import difflib import errno import glob @@ -1089,8 +1090,21 @@ def debugdiscovery(ui, repo, remoteurl=b 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 @@ def debugdiscovery(ui, repo, remoteurl=b 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 diff --git a/mercurial/formatter.py b/mercurial/formatter.py --- a/mercurial/formatter.py +++ b/mercurial/formatter.py @@ -178,6 +178,11 @@ class _nullconverter(object): 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 cborformatter(baseformatter): class jsonformatter(baseformatter): + + strict_format = True + def __init__(self, ui, out, topic, opts): baseformatter.__init__(self, ui, topic, opts, _nullconverter) self._out = out diff --git a/tests/test-setdiscovery.t b/tests/test-setdiscovery.t --- a/tests/test-setdiscovery.t +++ b/tests/test-setdiscovery.t @@ -1734,13 +1734,6 @@ Test -T json output > --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 @@ Test -T json output "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 } ]