diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -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. diff --git a/tests/test-lfs.t b/tests/test-lfs.t --- a/tests/test-lfs.t +++ b/tests/test-lfs.t @@ -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