Patchwork [06,of,10,V4] rcutil: add a method to convert environment variables to config items

login
register
mail settings
Submitter Jun Wu
Date March 27, 2017, 4:49 a.m.
Message ID <9ba047e9e8b929c0fc38.1490590159@localhost.localdomain>
Download mbox | patch
Permalink /patch/19738/
State Superseded
Headers show

Comments

Jun Wu - March 27, 2017, 4:49 a.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1490588822 25200
#      Sun Mar 26 21:27:02 2017 -0700
# Node ID 9ba047e9e8b929c0fc38d0606b4c02438a8aef29
# Parent  9b02412194004dc012e7f3119520138483c22644
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 9ba047e9e8b9
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.

Patch

diff --git a/mercurial/rcutil.py b/mercurial/rcutil.py
--- a/mercurial/rcutil.py
+++ b/mercurial/rcutil.py
@@ -33,4 +33,26 @@  def _expandrcpath(path):
     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'''