changeset 39852:2c2fadbc9851

localrepo: automatically load lfs extension when required (BC) If an unrecognized requirement is present (possibly due to an unloaded extension), the user will get an error message telling them to go to https://mercurial-scm.org/wiki/MissingRequirement for more info. And some requirements clearly map to known extensions shipped by Mercurial. This commit teaches repository loading to automatically map requirements to extensions. We implement support for loading the lfs extension when the "lfs" requirement is present. This behavior feels more user-friendly to me and I'm having trouble coming up with a compelling reason to not do it. The strongest argument I have against is that - strictly speaking - requirements are general repository features and there could be N providers of that feature. e.g. in the case of LFS, there could be another extension implementing LFS support. And the user would want to use this non-official extension rather than the built-in one. The way this patch implements things, the non-official extension could be missing and Mercurial would load the official lfs extension, leading to unexpected behavior. But this feels like a highly marginal use case to me and doesn't outweigh the user benefit of "it just works." If someone really wanted to e.g. use a custom LFS extension, they could prevent the built-in one from being loaded by either defining "extensions.lfs=/path/to/custom/extension" or "extensions.lfs=!", as the automatic extension loading only occurs if there is no config entry for that extension. Differential Revision: https://phab.mercurial-scm.org/D4711
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 20 Sep 2018 15:06:43 -0700
parents 1f7b3b980af8
children bcf72d7b1524
files mercurial/localrepo.py tests/test-lfs.t
diffstat 2 files changed, 74 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/localrepo.py	Wed Sep 19 13:48:59 2018 -0700
+++ b/mercurial/localrepo.py	Thu Sep 20 15:06:43 2018 -0700
@@ -446,6 +446,9 @@
     # process any new extensions that it may have pulled in.
     try:
         ui.readconfig(hgvfs.join(b'hgrc'), root=wdirvfs.base)
+        # Run this before extensions.loadall() so extensions can be
+        # automatically enabled.
+        afterhgrcload(ui, wdirvfs, hgvfs, requirements)
     except IOError:
         pass
     else:
@@ -572,6 +575,30 @@
         features=features,
         intents=intents)
 
+def afterhgrcload(ui, wdirvfs, hgvfs, requirements):
+    """Perform additional actions after .hg/hgrc is loaded.
+
+    This function is called during repository loading immediately after
+    the .hg/hgrc file is loaded and before per-repo extensions are loaded.
+
+    The function can be used to validate configs, automatically add
+    options (including extensions) based on requirements, etc.
+    """
+
+    # Map of requirements to list of extensions to load automatically when
+    # requirement is present.
+    autoextensions = {
+        b'lfs': [b'lfs'],
+    }
+
+    for requirement, names in sorted(autoextensions.items()):
+        if requirement not in requirements:
+            continue
+
+        for name in names:
+            if not ui.hasconfig(b'extensions', name):
+                ui.setconfig(b'extensions', name, b'', source='autoload')
+
 def gathersupportedrequirements(ui):
     """Determine the complete set of recognized requirements."""
     # Start with all requirements supported by this file.
--- a/tests/test-lfs.t	Wed Sep 19 13:48:59 2018 -0700
+++ b/tests/test-lfs.t	Thu Sep 20 15:06:43 2018 -0700
@@ -1,5 +1,52 @@
 #require no-reposimplestore no-chg
 
+  $ hg init requirements
+  $ cd requirements
+
+# LFS not loaded by default.
+
+  $ hg config extensions
+  [1]
+
+# Adding lfs to requires file will auto-load lfs extension.
+
+  $ echo lfs >> .hg/requires
+  $ hg config extensions
+  extensions.lfs=
+
+# But only if there is no config entry for the extension already.
+
+  $ cat > .hg/hgrc << EOF
+  > [extensions]
+  > lfs=!
+  > EOF
+
+  $ hg config extensions
+  abort: repository requires features unknown to this Mercurial: lfs!
+  (see https://mercurial-scm.org/wiki/MissingRequirement for more information)
+  [255]
+
+  $ cat > .hg/hgrc << EOF
+  > [extensions]
+  > lfs=
+  > EOF
+
+  $ hg config extensions
+  extensions.lfs=
+
+  $ cat > .hg/hgrc << EOF
+  > [extensions]
+  > lfs = missing.py
+  > EOF
+
+  $ hg config extensions
+  *** failed to import extension lfs from missing.py: [Errno 2] $ENOENT$: 'missing.py'
+  abort: repository requires features unknown to this Mercurial: lfs!
+  (see https://mercurial-scm.org/wiki/MissingRequirement for more information)
+  [255]
+
+  $ cd ..
+
 # Initial setup
 
   $ cat >> $HGRCPATH << EOF