[PATCH] Extensions: allow marking extensions as optional.

Krzysztof Pawlik krzysiek.pawlik at people.pl
Mon Nov 1 06:44:26 CDT 2010


# HG changeset patch
# User Krzysztof Pawlik (nelchael)
# Date 1288611100 -3600
# Node ID f6b08f26108b0af8da69bd827e1d03562d4ead11
# Parent  6bf8d48bec8e1ab2e0462ce14a914d06e64f7117
Extensions: allow marking extensions as optional.

Failure to load an optional extension does not result in a warning
message. Useful when single .hgrc is shared among many different
machines (or environments) where some extensions are present only on
small subset of them.

diff --git a/doc/hgrc.5.txt b/doc/hgrc.5.txt
--- a/doc/hgrc.5.txt
+++ b/doc/hgrc.5.txt
@@ -409,6 +409,12 @@ broader scope, prepend its path with ``!
 ``hgext.foo = !/ext/path`` or ``hgext.foo = !``  when path is not
 supplied.
 
+Extensions can also be optional, when marked as such failure to load
+them will not result in a warning message. To mark an extension as
+optional prepend its path with ``?``, as in
+``hgext.foo = ?/ext/path`` or ``hgext.foo = ?`` when path is not
+supplied.
+
 Example for ``~/.hgrc``::
 
   [extensions]
@@ -416,6 +422,10 @@ Example for ``~/.hgrc``::
   hgext.mq =
   # (this extension will get loaded from the file specified)
   myfeature = ~/.hgext/myfeature.py
+  # (this extension will be disabled)
+  mydisabled = !
+  # This extension is optional
+  myoptional = ?
 
 
 ``format``
diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -72,22 +72,27 @@ def loadall(ui):
     result = ui.configitems("extensions")
     newindex = len(_order)
     for (name, path) in result:
+        optional = False
         if path:
             if path[0] == '!':
                 continue
+            if path[0] == '?':
+                optional = True
+                path = path[1:]
         try:
             load(ui, name, path)
         except KeyboardInterrupt:
             raise
         except Exception, inst:
-            if path:
-                ui.warn(_("*** failed to import extension %s from %s: %s\n")
-                        % (name, path, inst))
-            else:
-                ui.warn(_("*** failed to import extension %s: %s\n")
-                        % (name, inst))
-            if ui.traceback():
-                return 1
+            if not optional:
+                if path:
+                    ui.warn(_("*** failed to import extension %s from %s: %s\n")
+                            % (name, path, inst))
+                else:
+                    ui.warn(_("*** failed to import extension %s: %s\n")
+                            % (name, inst))
+                if ui.traceback():
+                    return 1
 
     for name in _order[newindex:]:
         uisetup = getattr(_extensions[name], 'uisetup', None)
diff --git a/tests/test-extension.t b/tests/test-extension.t
--- a/tests/test-extension.t
+++ b/tests/test-extension.t
@@ -318,3 +318,14 @@ Broken disabled extension and command:
   $ hg --config extensions.path=./path.py help foo > /dev/null
   hg: unknown command 'foo'
   [255]
+
+Test optional extensions:
+
+  $ hg init optional_ext_dir
+  $ cd optional_ext_dir
+  $ touch sample
+  $ hg --config extensions.missingext= status
+  *** failed to import extension missingext: No module named missingext
+  ? sample
+  $ hg --config extensions.missingext='?' status
+  ? sample

-- 
Krzysztof Pawlik (Nelchael)     GPG Key ID: 0xF6A80E46
      http://www.linkedin.com/in/krzysiekpawlik

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 554 bytes
Desc: OpenPGP digital signature
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20101101/d68ad9ea/attachment.pgp>


More information about the Mercurial-devel mailing list