Patchwork help: pass commands module by argument

login
register
mail settings
Submitter Yuya Nishihara
Date May 30, 2017, 4:03 p.m.
Message ID <d786341a32f9707143c1.1496160228@mimosa>
Download mbox | patch
Permalink /patch/21067/
State Accepted
Headers show

Comments

Yuya Nishihara - May 30, 2017, 4:03 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1495353452 -32400
#      Sun May 21 16:57:32 2017 +0900
# Node ID d786341a32f9707143c1d00a467b800034677bb6
# Parent  633c635a790ad75709e0e84256f4c1a1c7d27ca0
help: pass commands module by argument

This removes import cycle.
Augie Fackler - May 30, 2017, 9:58 p.m.
> On May 30, 2017, at 12:03 PM, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1495353452 -32400
> #      Sun May 21 16:57:32 2017 +0900
> # Node ID d786341a32f9707143c1d00a467b800034677bb6
> # Parent  633c635a790ad75709e0e84256f4c1a1c7d27ca0
> help: pass commands module by argument

Queued. Clever!

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -11,6 +11,7 @@  import difflib
 import errno
 import os
 import re
+import sys
 
 from .i18n import _
 from .node import (
@@ -2745,7 +2746,8 @@  def help_(ui, name=None, **opts):
     if ui.verbose:
         keep.append('verbose')
 
-    formatted = help.formattedhelp(ui, name, keep=keep, **opts)
+    commands = sys.modules[__name__]
+    formatted = help.formattedhelp(ui, commands, name, keep=keep, **opts)
     ui.pager('help')
     ui.write(formatted)
 
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -333,7 +333,8 @@  def _callcatch(ui, func):
         try:
             # check if the command is in a disabled extension
             # (but don't check for extensions themselves)
-            formatted = help.formattedhelp(ui, inst.args[0], unknowncmd=True)
+            formatted = help.formattedhelp(ui, commands, inst.args[0],
+                                           unknowncmd=True)
             ui.warn(nocmdmsg)
             ui.write(formatted)
         except (error.UnknownCommand, error.Abort):
diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -114,7 +114,7 @@  def filtercmd(ui, cmd, kw, doc):
         return True
     return False
 
-def topicmatch(ui, kw):
+def topicmatch(ui, commands, kw):
     """Return help topics matching kw.
 
     Returns {'section': [(name, summary), ...], ...} where section is
@@ -134,7 +134,6 @@  def topicmatch(ui, kw):
             or lowercontains(header)
             or (callable(doc) and lowercontains(doc(ui)))):
             results['topics'].append((names[0], header))
-    from . import commands # avoid cycle
     for cmd, entry in commands.table.iteritems():
         if len(entry) == 3:
             summary = entry[2]
@@ -299,13 +298,13 @@  addtopicsymbols('templates', '.. functio
 addtopicsymbols('hgweb', '.. webcommandsmarker', webcommands.commands,
                 dedent=True)
 
-def help_(ui, name, unknowncmd=False, full=True, subtopic=None, **opts):
+def help_(ui, commands, name, unknowncmd=False, full=True, subtopic=None,
+          **opts):
     '''
     Generate the help for 'name' as unformatted restructured text. If
     'name' is None, describe the commands available.
     '''
 
-    from . import commands # avoid cycle
     opts = pycompat.byteskwargs(opts)
 
     def helpcmd(name, subtopic=None):
@@ -569,7 +568,7 @@  def help_(ui, name, unknowncmd=False, fu
     rst = []
     kw = opts.get('keyword')
     if kw or name is None and any(opts[o] for o in opts):
-        matches = topicmatch(ui, name or '')
+        matches = topicmatch(ui, commands, name or '')
         helpareas = []
         if opts.get('extension'):
             helpareas += [('extensions', _('Extensions'))]
@@ -620,7 +619,8 @@  def help_(ui, name, unknowncmd=False, fu
 
     return ''.join(rst)
 
-def formattedhelp(ui, name, keep=None, unknowncmd=False, full=True, **opts):
+def formattedhelp(ui, commands, name, keep=None, unknowncmd=False, full=True,
+                  **opts):
     """get help for a given topic (as a dotted name) as rendered rst
 
     Either returns the rendered help text or raises an exception.
@@ -646,7 +646,7 @@  def formattedhelp(ui, name, keep=None, u
     termwidth = ui.termwidth() - 2
     if textwidth <= 0 or termwidth < textwidth:
         textwidth = termwidth
-    text = help_(ui, name,
+    text = help_(ui, commands, name,
                  subtopic=subtopic, unknowncmd=unknowncmd, full=full, **opts)
 
     formatted, pruned = minirst.format(text, textwidth, keep=keep,
diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -1374,7 +1374,7 @@  def help(web, req, tmpl):
         subtopic = None
 
     try:
-        doc = helpmod.help_(u, topic, subtopic=subtopic)
+        doc = helpmod.help_(u, commands, topic, subtopic=subtopic)
     except error.UnknownCommand:
         raise ErrorResponse(HTTP_NOT_FOUND)
     return tmpl('help', topic=topicname, doc=doc)