Mercurial > public > mercurial-scm > hg
comparison mercurial/debugcommands.py @ 47437:7a430116f639
ui: add a context manager for silencing the ui (pushbuffer+popbuffer)
We often silence the ui by calling `ui.pushbuffer()` followed (a later
in the code) by `ui.popbuffer()`. These places can be identified by
the fact that they ignore the output returned from
`ui.popbuffer()`. Let's create a context manager for these cases, to
avoid repetition, and to avoid accidentally leaving the ui silent on
exceptions. I deliberately called the new function `silent()` instead
of `buffered()`, because it's just an implementation detail that it
uses `pushbuffer()` and `popbuffer()`. We could later optimize it to
not buffer the output.
Differential Revision: https://phab.mercurial-scm.org/D10884
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Fri, 18 Jun 2021 15:48:51 -0700 |
parents | 3b9914b28133 |
children | 1be9bf3669cb |
comparison
equal
deleted
inserted
replaced
47436:6ecd0980d7f9 | 47437:7a430116f639 |
---|---|
2752 ctx = scmutil.revsingle(repo, opts.get(b'rev')) | 2752 ctx = scmutil.revsingle(repo, opts.get(b'rev')) |
2753 m = scmutil.match(ctx, pats, opts) | 2753 m = scmutil.match(ctx, pats, opts) |
2754 changedelete = opts[b'changedelete'] | 2754 changedelete = opts[b'changedelete'] |
2755 for path in ctx.walk(m): | 2755 for path in ctx.walk(m): |
2756 fctx = ctx[path] | 2756 fctx = ctx[path] |
2757 try: | 2757 with ui.silent( |
2758 if not ui.debugflag: | 2758 error=True |
2759 ui.pushbuffer(error=True) | 2759 ) if not ui.debugflag else util.nullcontextmanager(): |
2760 tool, toolpath = filemerge._picktool( | 2760 tool, toolpath = filemerge._picktool( |
2761 repo, | 2761 repo, |
2762 ui, | 2762 ui, |
2763 path, | 2763 path, |
2764 fctx.isbinary(), | 2764 fctx.isbinary(), |
2765 b'l' in fctx.flags(), | 2765 b'l' in fctx.flags(), |
2766 changedelete, | 2766 changedelete, |
2767 ) | 2767 ) |
2768 finally: | |
2769 if not ui.debugflag: | |
2770 ui.popbuffer() | |
2771 ui.write(b'%s = %s\n' % (path, tool)) | 2768 ui.write(b'%s = %s\n' % (path, tool)) |
2772 | 2769 |
2773 | 2770 |
2774 @command(b'debugpushkey', [], _(b'REPO NAMESPACE [KEY OLD NEW]'), norepo=True) | 2771 @command(b'debugpushkey', [], _(b'REPO NAMESPACE [KEY OLD NEW]'), norepo=True) |
2775 def debugpushkey(ui, repopath, namespace, *keyinfo, **opts): | 2772 def debugpushkey(ui, repopath, namespace, *keyinfo, **opts): |
4578 | 4575 |
4579 if opts[b'peer'] == b'http2': | 4576 if opts[b'peer'] == b'http2': |
4580 ui.write(_(b'creating http peer for wire protocol version 2\n')) | 4577 ui.write(_(b'creating http peer for wire protocol version 2\n')) |
4581 # We go through makepeer() because we need an API descriptor for | 4578 # We go through makepeer() because we need an API descriptor for |
4582 # the peer instance to be useful. | 4579 # the peer instance to be useful. |
4583 with ui.configoverride( | 4580 maybe_silent = ( |
4581 ui.silent() | |
4582 if opts[b'nologhandshake'] | |
4583 else util.nullcontextmanager() | |
4584 ) | |
4585 with maybe_silent, ui.configoverride( | |
4584 {(b'experimental', b'httppeer.advertise-v2'): True} | 4586 {(b'experimental', b'httppeer.advertise-v2'): True} |
4585 ): | 4587 ): |
4586 if opts[b'nologhandshake']: | |
4587 ui.pushbuffer() | |
4588 | |
4589 peer = httppeer.makepeer(ui, path, opener=opener) | 4588 peer = httppeer.makepeer(ui, path, opener=opener) |
4590 | |
4591 if opts[b'nologhandshake']: | |
4592 ui.popbuffer() | |
4593 | 4589 |
4594 if not isinstance(peer, httppeer.httpv2peer): | 4590 if not isinstance(peer, httppeer.httpv2peer): |
4595 raise error.Abort( | 4591 raise error.Abort( |
4596 _( | 4592 _( |
4597 b'could not instantiate HTTP peer for ' | 4593 b'could not instantiate HTTP peer for ' |