# HG changeset patch
# User Jun Wu <qu...@fb.com>
# Date 1489452890 25200
#      Mon Mar 13 17:54:50 2017 -0700
# Node ID 75f661b31640a914dd513d42b2ce1389c9b61c0a
# Parent  7775943a59cfaaae77e2143cbfa0bf0d8d95a447
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 75f661b31640
scmutil: add a rcpath-like method 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
@@ -450,4 +450,44 @@ def rcpath():
     return _rcpath
 
+_rccomponents = None
+
+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.
+    '''
+    global _rccomponents
+    if _rccomponents is None:
+        if 'HGRCPATH' in encoding.environ:
+            # assume HGRCPATH is all about user configs so environments can be
+            # overridden.
+            _rccomponents = [('items', envconfig(_sysenvlist))]
+            for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
+                if not p:
+                    continue
+                p = util.expandpath(p)
+                if os.path.isdir(p):
+                    for f, kind in osutil.listdir(p):
+                        if f.endswith('.rc'):
+                            _rccomponents.append(('path', os.path.join(p, f)))
+                else:
+                    _rccomponents.append(('path', p))
+        else:
+            def pathize(path):
+                return ('path', os.path.normpath(path))
+
+            _rccomponents = map(pathize, defaultrcpath())
+            _rccomponents.extend(map(pathize, systemrcpath()))
+            _rccomponents.append(('items', envconfig(_sysenvlist)))
+            _rccomponents.extend(map(pathize, userrcpath()))
+    return _rccomponents
+
 def intrev(rev):
     """Return integer for a given revision that can be used in comparison or
_______________________________________________
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Reply via email to