Patchwork [2,of,2,pager-dispatch-tweaks] dispatch: rearrange 'unknown command' code to better employ pager

login
register
mail settings
Submitter Augie Fackler
Date Feb. 21, 2017, 9:14 p.m.
Message ID <52083816be40edfac2e3.1487711693@arthedain.pit.corp.google.com>
Download mbox | patch
Permalink /patch/18694/
State Accepted
Headers show

Comments

Augie Fackler - Feb. 21, 2017, 9:14 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1487704805 18000
#      Tue Feb 21 14:20:05 2017 -0500
# Node ID 52083816be40edfac2e3e052ebe4d828c6c4a1c6
# Parent  f489d1ec2070f25a09f571177b97d8284b3b1932
dispatch: rearrange 'unknown command' code to better employ pager

dispatch calls help like a ninja if you give it a truly unknown
command, and that might want to be paged. If it gets paged, then the
'hg: unknown command' text gets eaten by a grue, unless we call the
pager first. This change rearranges the codepaths so we can safely
only invoke the pager in the case where we'll have long output from
the help command code, rather than just a short message like "did you
mean stat instead of start" or "fetch is provided by the fetch
extension".
Yuya Nishihara - Feb. 22, 2017, 2:05 p.m.
On Tue, 21 Feb 2017 16:14:53 -0500, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <augie@google.com>
> # Date 1487704805 18000
> #      Tue Feb 21 14:20:05 2017 -0500
> # Node ID 52083816be40edfac2e3e052ebe4d828c6c4a1c6
> # Parent  f489d1ec2070f25a09f571177b97d8284b3b1932
> dispatch: rearrange 'unknown command' code to better employ pager

> +                ui.pager('help')
> +                ui.warn(nocmdmsg)
>                  commands.help_(ui, 'shortlist')

We have two more commands.help_() in error.CommandError branch.

Patch

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -33,6 +33,7 @@  from . import (
     extensions,
     fancyopts,
     fileset,
+    help,
     hg,
     hook,
     profiling,
@@ -242,19 +243,24 @@  def callcatch(ui, func):
         _formatparse(ui.warn, inst)
         return -1
     except error.UnknownCommand as inst:
-        ui.warn(_("hg: unknown command '%s'\n") % inst.args[0])
+        nocmdmsg = _("hg: unknown command '%s'\n") % inst.args[0]
         try:
             # check if the command is in a disabled extension
             # (but don't check for extensions themselves)
-            commands.help_(ui, inst.args[0], unknowncmd=True)
+            formatted = help.formattedhelp(ui, inst.args[0], unknowncmd=True)
+            ui.warn(nocmdmsg)
+            ui.write(formatted)
         except (error.UnknownCommand, error.Abort):
             suggested = False
             if len(inst.args) == 2:
                 sim = _getsimilar(inst.args[1], inst.args[0])
                 if sim:
+                    ui.warn(nocmdmsg)
                     _reportsimilar(ui.warn, sim)
                     suggested = True
             if not suggested:
+                ui.pager('help')
+                ui.warn(nocmdmsg)
                 commands.help_(ui, 'shortlist')
     except IOError:
         raise