rcutil: add a method to convert environment variables to config items
authorJun Wu <quark@fb.com>
Sun, 26 Mar 2017 21:27:02 -0700
changeset 31684 0be96ac9199a
parent 31683 00e569a2da97
child 31685 d83e51654c8a
rcutil: add a method to convert environment variables to config items Handling config and environ priorities has been messy. Partially because we don't have config layers - you either get all configs (sys + user), or none. Ideally, environ like $EDITOR, $PAGER should be able to override the system configs "ui.editor", "pager.pager". This patch provides the ability to convert them into config items, so they can be inserted into the middle config layer between system rc and user rc.
mercurial/rcutil.py
--- a/mercurial/rcutil.py	Sun Mar 26 21:04:29 2017 -0700
+++ b/mercurial/rcutil.py	Sun Mar 26 21:27:02 2017 -0700
@@ -32,6 +32,28 @@
         return [join(p, f) for f, k in osutil.listdir(p) if f.endswith('.rc')]
     return [p]
 
+def envrcitems(env=None):
+    '''Return [(section, name, value, source)] config items.
+
+    The config items are extracted from environment variables specified by env,
+    used to override systemrc, but not userrc.
+
+    If env is not provided, encoding.environ will be used.
+    '''
+    if env is None:
+        env = encoding.environ
+    checklist = [
+        ('EDITOR', 'ui', 'editor'),
+        ('VISUAL', 'ui', 'editor'),
+        ('PAGER', 'pager', 'pager'),
+    ]
+    result = []
+    for envname, section, configname in checklist:
+        if envname not in env:
+            continue
+        result.append((section, configname, env[envname], '$%s' % envname))
+    return result
+
 def defaultrcpath():
     '''return rc paths in default.d'''
     path = []