Patchwork [2,of,2,V2] templater: add '{envvars}' to access environment variables

login
register
mail settings
Submitter Matt Harbison
Date Jan. 18, 2017, 4:50 a.m.
Message ID <fce42f9dba7bab71463c.1484715047@Envy>
Download mbox | patch
Permalink /patch/18242/
State Accepted
Headers show

Comments

Matt Harbison - Jan. 18, 2017, 4:50 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1484712774 18000
#      Tue Jan 17 23:12:54 2017 -0500
# Node ID fce42f9dba7bab71463c0bcec44e6d87fdf275b2
# Parent  5a03e25ec0c0417e915b2014995bd83443ef97ec
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.
Yuya Nishihara - Jan. 18, 2017, 1:44 p.m.
On Tue, 17 Jan 2017 23:50:47 -0500, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1484712774 18000
> #      Tue Jan 17 23:12:54 2017 -0500
> # Node ID fce42f9dba7bab71463c0bcec44e6d87fdf275b2
> # Parent  5a03e25ec0c0417e915b2014995bd83443ef97ec
> 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.
> 
> diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
> --- a/mercurial/cmdutil.py
> +++ b/mercurial/cmdutil.py
> @@ -1448,7 +1448,8 @@
>              'parent': '{rev}:{node|formatnode} ',
>              'manifest': '{rev}:{node|formatnode}',
>              'file_copy': '{name} ({source})',
> -            'extra': '{key}={value|stringescape}'
> +            'extra': '{key}={value|stringescape}',
> +            'envvar': '{key}={value|stringescape}'

Dropped '|stringescape' since {envvar} should be readable text in general.

> --- a/mercurial/templatekw.py
> +++ b/mercurial/templatekw.py
> @@ -303,6 +303,18 @@
>      maxname, maxtotal, adds, removes, binary = patch.diffstatsum(stats)
>      return '%s: +%s/-%s' % (len(stats), adds, removes)
>  
> +@templatekeyword('envvars')
> +def showenviron(repo, **args):

s/showenviron/showenvvars/
Matt Harbison - Jan. 19, 2017, 2:39 a.m.
On Wed, 18 Jan 2017 08:44:55 -0500, Yuya Nishihara <yuya@tcha.org> wrote:

> On Tue, 17 Jan 2017 23:50:47 -0500, Matt Harbison wrote:
>> # HG changeset patch
>> # User Matt Harbison <matt_harbison@yahoo.com>
>> # Date 1484712774 18000
>> #      Tue Jan 17 23:12:54 2017 -0500
>> # Node ID fce42f9dba7bab71463c0bcec44e6d87fdf275b2
>> # Parent  5a03e25ec0c0417e915b2014995bd83443ef97ec
>> 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.
>>
>> diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
>> --- a/mercurial/cmdutil.py
>> +++ b/mercurial/cmdutil.py
>> @@ -1448,7 +1448,8 @@
>>              'parent': '{rev}:{node|formatnode} ',
>>              'manifest': '{rev}:{node|formatnode}',
>>              'file_copy': '{name} ({source})',
>> -            'extra': '{key}={value|stringescape}'
>> +            'extra': '{key}={value|stringescape}',
>> +            'envvar': '{key}={value|stringescape}'
>
> Dropped '|stringescape' since {envvar} should be readable text in  
> general.

The only reason I thought it was necessary is because MSYS is apparently  
putting a couple '\n' in $PS1.

$ ../hg log -r . -T "{get(envvars, 'PS1')}" --config  
"experimental.exportableenviron=*"
\[\033]0;$MSYSTEM:\w\007
\033[32m\]\u@\h \[\033[33m\w\033[0m\]

vs

$ echo $PS1
\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u@\h \[\033[33m\w\033[0m\] $

Maybe that's too much of a corner case to care.  Dropping the escaping  
also prevents the Windows paths from showing as 'C:\\Windows\\..', so  
maybe that's a good thing.


>> --- a/mercurial/templatekw.py
>> +++ b/mercurial/templatekw.py
>> @@ -303,6 +303,18 @@
>>      maxname, maxtotal, adds, removes, binary = patch.diffstatsum(stats)
>>      return '%s: +%s/-%s' % (len(stats), adds, removes)
>>
>> +@templatekeyword('envvars')
>> +def showenviron(repo, **args):
>
> s/showenviron/showenvvars/
Yuya Nishihara - Jan. 19, 2017, 3 p.m.
On Wed, 18 Jan 2017 21:39:47 -0500, Matt Harbison wrote:
> On Wed, 18 Jan 2017 08:44:55 -0500, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> > On Tue, 17 Jan 2017 23:50:47 -0500, Matt Harbison wrote:
> >> # HG changeset patch
> >> # User Matt Harbison <matt_harbison@yahoo.com>
> >> # Date 1484712774 18000
> >> #      Tue Jan 17 23:12:54 2017 -0500
> >> # Node ID fce42f9dba7bab71463c0bcec44e6d87fdf275b2
> >> # Parent  5a03e25ec0c0417e915b2014995bd83443ef97ec
> >> 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.
> >>
> >> diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
> >> --- a/mercurial/cmdutil.py
> >> +++ b/mercurial/cmdutil.py
> >> @@ -1448,7 +1448,8 @@
> >>              'parent': '{rev}:{node|formatnode} ',
> >>              'manifest': '{rev}:{node|formatnode}',
> >>              'file_copy': '{name} ({source})',
> >> -            'extra': '{key}={value|stringescape}'
> >> +            'extra': '{key}={value|stringescape}',
> >> +            'envvar': '{key}={value|stringescape}'
> >
> > Dropped '|stringescape' since {envvar} should be readable text in  
> > general.
> 
> The only reason I thought it was necessary is because MSYS is apparently  
> putting a couple '\n' in $PS1.
> 
> $ ../hg log -r . -T "{get(envvars, 'PS1')}" --config  
> "experimental.exportableenviron=*"
> \[\033]0;$MSYSTEM:\w\007
> \033[32m\]\u@\h \[\033[33m\w\033[0m\]
> 
> vs
> 
> $ echo $PS1
> \[\033]0;$MSYSTEM:\w\007 \033[32m\]\u@\h \[\033[33m\w\033[0m\] $
> 
> Maybe that's too much of a corner case to care.  Dropping the escaping  
> also prevents the Windows paths from showing as 'C:\\Windows\\..', so  
> maybe that's a good thing.

Ah, thanks for the clarification. I think the Windows paths is more important,
so let's go without stringescape.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1448,7 +1448,8 @@ 
             'parent': '{rev}:{node|formatnode} ',
             'manifest': '{rev}:{node|formatnode}',
             'file_copy': '{name} ({source})',
-            'extra': '{key}={value|stringescape}'
+            'extra': '{key}={value|stringescape}',
+            'envvar': '{key}={value|stringescape}'
             }
         # filecopy is preserved for compatibility reasons
         defaulttempl['filecopy'] = defaulttempl['file_copy']
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -303,6 +303,18 @@ 
     maxname, maxtotal, adds, removes, binary = patch.diffstatsum(stats)
     return '%s: +%s/-%s' % (len(stats), adds, removes)
 
+@templatekeyword('envvars')
+def showenviron(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'
diff --git a/tests/test-cat.t b/tests/test-cat.t
--- a/tests/test-cat.t
+++ b/tests/test-cat.t
@@ -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