[PATCH 1 of 1] extensions: load all extensions first, call setup functions thereafter

Simon Heimberg simohe at besonet.ch
Wed Aug 12 06:54:09 CDT 2009


# HG changeset patch
# User Simon Heimberg <simohe at besonet.ch>
# Date 1250075746 -7200
# Node ID 3bc8dde7c38d6985d8f111165107402002983fe4
# Parent  527eddab742d5db6df5f5ac8d3c46de98004f9b3
extensions: load all extensions first, call setup functions thereafter

new order for extension calls:
- load all extensions
- uisetup(ui) for all extensions
- extsetup() for all extensions
- add commands to commandtable for all extensions
- reposetup(ui, repo) for all extensions

diff -r 527eddab742d -r 3bc8dde7c38d mercurial/dispatch.py
--- a/mercurial/dispatch.py	Mit Jul 29 14:21:18 2009 +0200
+++ b/mercurial/dispatch.py	Mit Aug 12 13:15:46 2009 +0200
@@ -320,7 +320,6 @@
               result = ret)
     return ret
 
-_loaded = set()
 def _dispatch(ui, args):
     # read --config before doing anything else
     # (e.g. to change trust settings for reading .hg/hgrc)
@@ -350,25 +349,13 @@
         lui.readconfig(os.path.join(path, ".hg", "hgrc"))
 
     extensions.loadall(lui)
-    for name, module in extensions.extensions():
-        if name in _loaded:
-            continue
-
-        # setup extensions
-        # TODO this should be generalized to scheme, where extensions can
-        #      redepend on other extensions.  then we should toposort them, and
-        #      do initialization in correct order
-        extsetup = getattr(module, 'extsetup', None)
-        if extsetup:
-            extsetup()
-
+    for name, module in extensions.setupall(lui):
         cmdtable = getattr(module, 'cmdtable', {})
         overrides = [cmd for cmd in cmdtable if cmd in commands.table]
         if overrides:
             ui.warn(_("extension '%s' overrides commands: %s\n")
                     % (name, " ".join(overrides)))
         commands.table.update(cmdtable)
-        _loaded.add(name)
 
     addaliases(lui, commands.table)
 
diff -r 527eddab742d -r 3bc8dde7c38d mercurial/extensions.py
--- a/mercurial/extensions.py	Mit Jul 29 14:21:18 2009 +0200
+++ b/mercurial/extensions.py	Mit Aug 12 13:15:46 2009 +0200
@@ -39,7 +39,7 @@
     else:
         return imp.load_source(module_name, path)
 
-def load(ui, name, path):
+def load(ui, name, path, init=False):
     if name.startswith('hgext.') or name.startswith('hgext/'):
         shortname = name[6:]
     else:
@@ -66,9 +66,14 @@
     _extensions[shortname] = mod
     _order.append(shortname)
 
+    if init:
+        return
     uisetup = getattr(mod, 'uisetup', None)
     if uisetup:
         uisetup(ui)
+    extsetup = getattr(mod, 'extsetup', None)
+    if extsetup:
+        extsetup()
 
 def loadall(ui):
     result = ui.configitems("extensions")
@@ -77,7 +82,7 @@
             if path[0] == '!':
                 continue
         try:
-            load(ui, name, path)
+            load(ui, name, path, True)
         except KeyboardInterrupt:
             raise
         except Exception, inst:
@@ -90,6 +95,24 @@
             if ui.traceback():
                 return 1
 
+_loaded = set()
+def setupall(ui):
+    unloaded = []
+    for name in _order:
+        if name in _loaded:
+            continue
+        mod = _extensions[name]
+        unloaded.append((name, mod))
+        _loaded.add(name)
+        uisetup = getattr(mod, 'uisetup', None)
+        if uisetup:
+            uisetup(ui)
+    for name, mod in unloaded:
+        extsetup = getattr(mod, 'extsetup', None)
+        if extsetup:
+            extsetup()
+    return unloaded
+
 def wrapcommand(table, command, wrapper):
     aliases, entry = cmdutil.findcmd(command, table)
     for alias, e in table.iteritems():


More information about the Mercurial-devel mailing list