changeset 32599:1b90036f42f0

help: pass commands module by argument This removes import cycle.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 21 May 2017 16:57:32 +0900
parents 5313d98089f5
children 4b9b87930cb3
files mercurial/commands.py mercurial/dispatch.py mercurial/help.py mercurial/hgweb/webcommands.py
diffstat 4 files changed, 13 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Mon May 29 06:06:13 2017 -0700
+++ b/mercurial/commands.py	Sun May 21 16:57:32 2017 +0900
@@ -11,6 +11,7 @@
 import errno
 import os
 import re
+import sys
 
 from .i18n import _
 from .node import (
@@ -2745,7 +2746,8 @@
     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)
 
--- a/mercurial/dispatch.py	Mon May 29 06:06:13 2017 -0700
+++ b/mercurial/dispatch.py	Sun May 21 16:57:32 2017 +0900
@@ -333,7 +333,8 @@
         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):
--- a/mercurial/help.py	Mon May 29 06:06:13 2017 -0700
+++ b/mercurial/help.py	Sun May 21 16:57:32 2017 +0900
@@ -114,7 +114,7 @@
         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 @@
             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('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 @@
     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 @@
 
     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 @@
     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,
--- a/mercurial/hgweb/webcommands.py	Mon May 29 06:06:13 2017 -0700
+++ b/mercurial/hgweb/webcommands.py	Sun May 21 16:57:32 2017 +0900
@@ -1374,7 +1374,7 @@
         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)