rcutil: let rccomponents return different types of configs (API)
authorJun Wu <quark@fb.com>
Sun, 26 Mar 2017 21:04:29 -0700
changeset 31683 00e569a2da97
parent 31682 07d62fa518a4
child 31684 0be96ac9199a
rcutil: let rccomponents return different types of configs (API) The next patches will convert environ to raw config items, and insert the config items between systemrcpath and userrcpath. This patch teaches rccomponents to return the type information so the caller could distinguish between "path" and raw config "items".
mercurial/commands.py
mercurial/rcutil.py
mercurial/ui.py
--- a/mercurial/commands.py	Sun Mar 26 20:48:00 2017 -0700
+++ b/mercurial/commands.py	Sun Mar 26 21:04:29 2017 -0700
@@ -1804,8 +1804,11 @@
         return
     ui.pager('config')
     fm = ui.formatter('config', opts)
-    for f in rcutil.rccomponents():
-        ui.debug('read config from: %s\n' % f)
+    for t, f in rcutil.rccomponents():
+        if t == 'path':
+            ui.debug('read config from: %s\n' % f)
+        else:
+            raise error.ProgrammingError('unknown rctype: %s' % t)
     untrusted = bool(opts.get('untrusted'))
     if values:
         sections = [v for v in values if '.' not in v]
--- a/mercurial/rcutil.py	Sun Mar 26 20:48:00 2017 -0700
+++ b/mercurial/rcutil.py	Sun Mar 26 21:04:29 2017 -0700
@@ -43,11 +43,17 @@
 _rccomponents = None
 
 def rccomponents():
-    '''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.'''
+    '''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:
@@ -55,8 +61,8 @@
             for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
                 if not p:
                     continue
-                _rccomponents.extend(_expandrcpath(p))
+                _rccomponents.extend(('path', p) for p in _expandrcpath(p))
         else:
             paths = defaultrcpath() + systemrcpath() + userrcpath()
-            _rccomponents = pycompat.maplist(os.path.normpath, paths)
+            _rccomponents = [('path', os.path.normpath(p)) for p in paths]
     return _rccomponents
--- a/mercurial/ui.py	Sun Mar 26 20:48:00 2017 -0700
+++ b/mercurial/ui.py	Sun Mar 26 21:04:29 2017 -0700
@@ -212,8 +212,11 @@
         """Create a ui and load global and user configs"""
         u = cls()
         # we always trust global config files
-        for f in rcutil.rccomponents():
-            u.readconfig(f, trust=True)
+        for t, f in rcutil.rccomponents():
+            if t == 'path':
+                u.readconfig(f, trust=True)
+            else:
+                raise error.ProgrammingError('unknown rctype: %s' % t)
         return u
 
     def copy(self):