[PATCH 2 of 5] extensions: extract closure that looks for commands from disabled module

Yuya Nishihara yuya at tcha.org
Fri May 11 09:29:29 EDT 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1525338943 -32400
#      Thu May 03 18:15:43 2018 +0900
# Node ID ad36b4eb9903d325bc157831a482c8e30fe6e819
# Parent  7eca762c4cb88d42c9239c60fee743327f477c1a
extensions: extract closure that looks for commands from disabled module

I'll rewrite this function to not load disabled extensions.

diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -655,6 +655,28 @@ def disabledext(name):
     if name in paths:
         return _disabledhelp(paths[name])
 
+def _finddisabledcmd(ui, cmd, name, path, strict):
+    try:
+        mod = loadpath(path, 'hgext.%s' % name)
+    except Exception:
+        return
+    try:
+        aliases, entry = cmdutil.findcmd(cmd,
+            getattr(mod, 'cmdtable', {}), strict)
+    except (error.AmbiguousCommand, error.UnknownCommand):
+        return
+    except Exception:
+        ui.warn(_('warning: error finding commands in %s\n') % path)
+        ui.traceback()
+        return
+    for c in aliases:
+        if c.startswith(cmd):
+            cmd = c
+            break
+    else:
+        cmd = aliases[0]
+    return (cmd, name, mod)
+
 def disabledcmd(ui, cmd, strict=False):
     '''import disabled extensions until cmd is found.
     returns (cmdname, extname, module)'''
@@ -663,37 +685,15 @@ def disabledcmd(ui, cmd, strict=False):
     if not paths:
         raise error.UnknownCommand(cmd)
 
-    def findcmd(cmd, name, path):
-        try:
-            mod = loadpath(path, 'hgext.%s' % name)
-        except Exception:
-            return
-        try:
-            aliases, entry = cmdutil.findcmd(cmd,
-                getattr(mod, 'cmdtable', {}), strict)
-        except (error.AmbiguousCommand, error.UnknownCommand):
-            return
-        except Exception:
-            ui.warn(_('warning: error finding commands in %s\n') % path)
-            ui.traceback()
-            return
-        for c in aliases:
-            if c.startswith(cmd):
-                cmd = c
-                break
-        else:
-            cmd = aliases[0]
-        return (cmd, name, mod)
-
     ext = None
     # first, search for an extension with the same name as the command
     path = paths.pop(cmd, None)
     if path:
-        ext = findcmd(cmd, cmd, path)
+        ext = _finddisabledcmd(ui, cmd, cmd, path, strict=strict)
     if not ext:
         # otherwise, interrogate each extension until there's a match
         for name, path in paths.iteritems():
-            ext = findcmd(cmd, name, path)
+            ext = _finddisabledcmd(ui, cmd, name, path, strict=strict)
             if ext:
                 break
     if ext:


More information about the Mercurial-devel mailing list