diff mercurial/extensions.py @ 48361:0d0ce2529540

extension: add a `required` suboption to enforce the use of an extensions If `required` is set, failing to load an extensions will abort. See the test and documentation for details. Differential Revision: https://phab.mercurial-scm.org/D11822
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Fri, 26 Nov 2021 17:17:49 +0100
parents e4acdf5d94a2
children 7e6488aa1261
line wrap: on
line diff
--- a/mercurial/extensions.py	Mon Nov 29 13:09:04 2021 +0100
+++ b/mercurial/extensions.py	Fri Nov 26 17:17:49 2021 +0100
@@ -314,10 +314,23 @@
                 else:
                     error_msg = _(b'failed to import extension "%s": %s')
                     error_msg %= (name, msg)
-                ui.warn((b"*** %s\n") % error_msg)
-                if isinstance(inst, error.Hint) and inst.hint:
-                    ui.warn(_(b"*** (%s)\n") % inst.hint)
-                ui.traceback()
+
+                ext_options = ui.configsuboptions(b"extensions", name)[1]
+                if stringutil.parsebool(ext_options.get(b"required", b'no')):
+                    hint = None
+                    if isinstance(inst, error.Hint) and inst.hint:
+                        hint = inst.hint
+                    if hint is None:
+                        hint = _(
+                            b"loading of this extension was required, "
+                            b"see `hg help config.extensions` for details"
+                        )
+                    raise error.Abort(error_msg, hint=hint)
+                else:
+                    ui.warn((b"*** %s\n") % error_msg)
+                    if isinstance(inst, error.Hint) and inst.hint:
+                        ui.warn(_(b"*** (%s)\n") % inst.hint)
+                    ui.traceback()
 
     ui.log(
         b'extension',