D5221: localrepo: extract loading of hgrc files to standalone function

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Mon Nov 5 22:16:43 UTC 2018


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Various 3rd party extensions supplement where per-repo config data
  lives. Looking at their sources, they resort to unorthodox means to
  inject the config data. And the way they do it is susceptible to
  corner cases. e.g. not processing automatic extension loads,
  not reacting to new or disabled extensions in configs, etc.
  
  This commit extracts the core logic of loading hgrc files into
  a standalone function so there is a clear function that can be
  monkeypatched to inject per-repo config data at repository open
  time.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D5221

AFFECTED FILES
  mercurial/localrepo.py

CHANGE DETAILS

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -451,14 +451,8 @@
     # The .hg/hgrc file may load extensions or contain config options
     # that influence repository construction. Attempt to load it and
     # 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.
+    if loadhgrc(ui, wdirvfs, hgvfs, requirements):
         afterhgrcload(ui, wdirvfs, hgvfs, requirements)
-    except IOError:
-        pass
-    else:
         extensions.loadall(ui)
 
     # Set of module names of extensions loaded for this repository.
@@ -582,6 +576,24 @@
         features=features,
         intents=intents)
 
+def loadhgrc(ui, wdirvfs, hgvfs, requirements):
+    """Load hgrc files/content into a ui instance.
+
+    This is called during repository opening to load any additional
+    config files or settings relevant to the current repository.
+
+    Returns a bool indicating whether any additional configs were loaded.
+
+    Extensions should monkeypatch this function to modify how per-repo
+    configs are loaded. For example, an extension may wish to pull in
+    configs from alternate files or sources.
+    """
+    try:
+        ui.readconfig(hgvfs.join(b'hgrc'), root=wdirvfs.base)
+        return True
+    except IOError:
+        return False
+
 def afterhgrcload(ui, wdirvfs, hgvfs, requirements):
     """Perform additional actions after .hg/hgrc is loaded.
 



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list