templater: add '{envvars}' to access environment variables
authorMatt Harbison <matt_harbison@yahoo.com>
Tue, 17 Jan 2017 23:12:54 -0500
changeset 30833 bd5e9647f646
parent 30832 da5fa0f13a41
child 30834 64a9160bf150
templater: add '{envvars}' to access environment variables Since the option for ui.exportableenviron is experimental, so is this template until the underlying API is sorted out.
mercurial/cmdutil.py
mercurial/templatekw.py
tests/test-cat.t
--- a/mercurial/cmdutil.py	Tue Jan 17 23:05:12 2017 -0500
+++ b/mercurial/cmdutil.py	Tue Jan 17 23:12:54 2017 -0500
@@ -1448,6 +1448,7 @@
             'parent': '{rev}:{node|formatnode} ',
             'manifest': '{rev}:{node|formatnode}',
             'file_copy': '{name} ({source})',
+            'envvar': '{key}={value}',
             'extra': '{key}={value|stringescape}'
             }
         # filecopy is preserved for compatibility reasons
--- a/mercurial/templatekw.py	Tue Jan 17 23:05:12 2017 -0500
+++ b/mercurial/templatekw.py	Tue Jan 17 23:12:54 2017 -0500
@@ -303,6 +303,18 @@
     maxname, maxtotal, adds, removes, binary = patch.diffstatsum(stats)
     return '%s: +%s/-%s' % (len(stats), adds, removes)
 
+@templatekeyword('envvars')
+def showenvvars(repo, **args):
+    """A dictionary of environment variables. (EXPERIMENTAL)"""
+
+    env = repo.ui.exportableenviron()
+    env = util.sortdict((k, env[k]) for k in sorted(env))
+    makemap = lambda k: {'key': k, 'value': env[k]}
+    c = [makemap(k) for k in env]
+    f = _showlist('envvar', c, plural='envvars', **args)
+    return _hybrid(f, env, makemap,
+                   lambda x: '%s=%s' % (x['key'], x['value']))
+
 @templatekeyword('extras')
 def showextras(**args):
     """List of dicts with key, value entries of the 'extras'
--- a/tests/test-cat.t	Tue Jan 17 23:05:12 2017 -0500
+++ b/tests/test-cat.t	Tue Jan 17 23:12:54 2017 -0500
@@ -68,3 +68,14 @@
   $ echo b-wdir > b
   $ hg cat -r 'wdir()' b
   b-wdir
+
+Environment variables are not visible by default
+
+  $ PATTERN='t4' hg log -r '.' -T "{ifcontains('PATTERN', envvars, 'yes', 'no')}\n"
+  no
+
+Environment variable visibility can be explicit
+
+  $ PATTERN='t4' hg log -r '.' -T "{envvars % '{key} -> {value}\n'}" \
+  >                 --config "experimental.exportableenviron=PATTERN"
+  PATTERN -> t4