[PATCH 06 of 13 V3] scmutil: implement rccomponents to return multiple config sources

Jun Wu quark at fb.com
Wed Mar 22 13:23:38 EDT 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1490201209 25200
#      Wed Mar 22 09:46:49 2017 -0700
# Node ID 44c865487bfd2f081bfb322b1fb1b700d57f7adf
# Parent  0e0f8914507fc0030d66844bde77854266259603
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 44c865487bfd
scmutil: implement rccomponents to return multiple config sources

rcpath has the limitation that it only returns paths.

Now we also have raw configs generated from environ.

So rccomponents was added. It is similar to rcpath, but it can return mixed
path and raw configs (currently calculated from environment variables). The
code was basically copy-pasted from rcpath, and will be cleaned up a bit by
the next patch.

Python does not have union types or pattern matching. So we use a tuple
(type, obj) to denote things of different types.

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -452,14 +452,27 @@ def rcpath():
 _rccomponents = None
 
-def rcpath2():
-    '''return hgrc search path. if env var HGRCPATH is set, use it.
-    for each item in path, if directory, use files ending in .rc,
-    else use item.
-    make HGRCPATH empty to only look in .hg/hgrc of current repo.
-    if no HGRCPATH, use default os-specific path.'''
+def rccomponents():
+    '''return an ordered [(type, obj)] about where to load configs.
+
+    respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
+    used. if $HGRCPATH is not set, the platform default will be used.
+
+    if a directory is provided, *.rc files under it will be used.
+
+    type could be either 'path' or 'items', if type is 'path', obj is a string,
+    and is the config file path. if type is 'items', obj is a list of (section,
+    name, value, source) that should fill the config directly.
+    '''
+    def pathize(path):
+        return ('path', os.path.normpath(path))
+
+    envrc = ('items', envconfig(_sysenvlist))
+
     global _rccomponents
     if _rccomponents is None:
         if 'HGRCPATH' in encoding.environ:
-            _rccomponents = []
+            # assume HGRCPATH is all about user configs so environments can be
+            # overridden.
+            _rccomponents = [envrc]
             for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
                 if not p:
@@ -469,10 +482,11 @@ def rcpath2():
                     for f, kind in osutil.listdir(p):
                         if f.endswith('.rc'):
-                            _rccomponents.append(os.path.join(p, f))
+                            _rccomponents.append(pathize(os.path.join(p, f)))
                 else:
-                    _rccomponents.append(p)
+                    _rccomponents.append(pathize(p))
         else:
-            paths = defaultrcpath() + systemrcpath() + userrcpath()
-            _rccomponents = map(os.path.normpath, paths)
+            _rccomponents = map(pathize, defaultrcpath() + systemrcpath())
+            _rccomponents.append(envrc)
+            _rccomponents.extend(map(pathize, userrcpath()))
     return _rccomponents
 


More information about the Mercurial-devel mailing list