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

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

Comments

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