repo-config: move rc component of repository inside `rcutil`
authorPierre-Yves David <pierre-yves.david@octobus.net>
Tue, 22 Oct 2024 23:42:15 +0200
changeset 52423 0b791c90280a
parent 52422 8a52fd131d3b
child 52424 e3b45916c375
repo-config: move rc component of repository inside `rcutil` This gather logic about where to find config file in the same location. This also reduce the amount of logic we do in dispatch.py which is a win in itself.
mercurial/configuration/rcutil.py
mercurial/dispatch.py
--- a/mercurial/configuration/rcutil.py	Wed Oct 23 00:00:17 2024 +0200
+++ b/mercurial/configuration/rcutil.py	Tue Oct 22 23:42:15 2024 +0200
@@ -19,8 +19,11 @@
 
 from .. import (
     encoding,
+    localrepo,
     pycompat,
+    requirements as requirementsmod,
     util,
+    vfs,
 )
 
 from ..utils import resourceutil
@@ -127,6 +130,37 @@
     return _rccomponents
 
 
+def _shared_source_component(path: bytes) -> List[FileRCT]:
+    """if the current repository is shared one, this tries to read
+    .hg/hgrc of shared source if we are in share-safe mode
+
+    This should be called before reading .hg/hgrc or the main repo
+    as that overrides config set in shared source"""
+    try:
+        with open(os.path.join(path, b".hg", b"requires"), "rb") as fp:
+            requirements = set(fp.read().splitlines())
+            if not (
+                requirementsmod.SHARESAFE_REQUIREMENT in requirements
+                and requirementsmod.SHARED_REQUIREMENT in requirements
+            ):
+                return []
+            hgvfs = vfs.vfs(os.path.join(path, b".hg"))
+            sharedvfs = localrepo._getsharedvfs(hgvfs, requirements)
+            return [sharedvfs.join(b"hgrc")]
+    except IOError:
+        pass
+    return []
+
+
+def repo_components(repo_path: bytes) -> List[FileRCT]:
+    """return the list of config file to read for a repository"""
+    components = []
+    components.extend(_shared_source_component(repo_path))
+    components.append(os.path.join(repo_path, b".hg", b"hgrc"))
+    components.append(os.path.join(repo_path, b".hg", b"hgrc-not-shared"))
+    return components
+
+
 def defaultpagerenv() -> Dict[bytes, bytes]:
     """return a dict of default environment variables and their values,
     intended to be set before starting a pager.
--- a/mercurial/dispatch.py	Wed Oct 23 00:00:17 2024 +0200
+++ b/mercurial/dispatch.py	Tue Oct 22 23:42:15 2024 +0200
@@ -34,15 +34,12 @@
     help,
     hg,
     hook,
-    localrepo,
     profiling,
     pycompat,
     registrar,
-    requirements as requirementsmod,
     scmutil,
     ui as uimod,
     util,
-    vfs,
 )
 
 from .configuration import rcutil
@@ -929,29 +926,6 @@
     return ret
 
 
-def _readsharedsourceconfig(ui, path):
-    """if the current repository is shared one, this tries to read
-    .hg/hgrc of shared source if we are in share-safe mode
-
-    Config read is loaded into the ui object passed
-
-    This should be called before reading .hg/hgrc or the main repo
-    as that overrides config set in shared source"""
-    try:
-        with open(os.path.join(path, b".hg", b"requires"), "rb") as fp:
-            requirements = set(fp.read().splitlines())
-            if not (
-                requirementsmod.SHARESAFE_REQUIREMENT in requirements
-                and requirementsmod.SHARED_REQUIREMENT in requirements
-            ):
-                return
-            hgvfs = vfs.vfs(os.path.join(path, b".hg"))
-            sharedvfs = localrepo._getsharedvfs(hgvfs, requirements)
-            ui.readconfig(sharedvfs.join(b"hgrc"), root=path)
-    except IOError:
-        pass
-
-
 def _getlocal(ui, rpath, wd=None):
     """Return (path, local ui object) for the given target path.
 
@@ -980,9 +954,8 @@
     else:
         lui = ui.copy()
         if rcutil.use_repo_hgrc():
-            _readsharedsourceconfig(lui, path)
-            lui.readconfig(os.path.join(path, b".hg", b"hgrc"), path)
-            lui.readconfig(os.path.join(path, b".hg", b"hgrc-not-shared"), path)
+            for rc_path in rcutil.repo_components(path):
+                lui.readconfig(rc_path, root=path)
 
     if rpath:
         # the specified path, might be defined in the [paths] section of the
@@ -992,9 +965,8 @@
         path = path_obj.rawloc
         lui = ui.copy()
         if rcutil.use_repo_hgrc():
-            _readsharedsourceconfig(lui, path)
-            lui.readconfig(os.path.join(path, b".hg", b"hgrc"), path)
-            lui.readconfig(os.path.join(path, b".hg", b"hgrc-not-shared"), path)
+            for rc_path in rcutil.repo_components(path):
+                lui.readconfig(rc_path, root=path)
 
     if oldcwd:
         os.chdir(oldcwd)