Patchwork [05,of,10,V5] rcutil: let rccomponents return different types of configs (API)

login
register
mail settings
Submitter Jun Wu
Date March 27, 2017, 6:02 a.m.
Message ID <fd9854b8f0f8ba25a237.1490594524@localhost.localdomain>
Download mbox | patch
Permalink /patch/19746/
State Accepted
Headers show

Comments

Jun Wu - March 27, 2017, 6:02 a.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1490587469 25200
#      Sun Mar 26 21:04:29 2017 -0700
# Node ID fd9854b8f0f8ba25a237eba2196209cde69106dd
# Parent  86998401114d7e4ae6d1c70b4e1b95b74dd1d6e3
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r fd9854b8f0f8
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".

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1804,6 +1804,9 @@  def config(ui, repo, *values, **opts):
     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:
diff --git a/mercurial/rcutil.py b/mercurial/rcutil.py
--- a/mercurial/rcutil.py
+++ b/mercurial/rcutil.py
@@ -44,9 +44,15 @@  def defaultrcpath():
 
 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:
@@ -56,7 +62,7 @@  def rccomponents():
                 if not p:
                     continue
-                _rccomponents.extend(_expandrcpath(p))
+                _rccomponents.extend(('path', p) for p in _expandrcpath(p))
         else:
             paths = defaultrcpath() + systemrcpath() + userrcpath()
-            _rccomponents = map(os.path.normpath, paths)
+            _rccomponents = [('path', os.path.normpath(p)) for p in paths]
     return _rccomponents
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -213,6 +213,9 @@  class ui(object):
         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