[PATCH] hooks: allow Unix style environment variables on external Windows hooks

Yuya Nishihara yuya at tcha.org
Mon Jul 3 09:32:32 EDT 2017


On Sun, 02 Jul 2017 01:45:00 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison at yahoo.com>
> # Date 1498969929 14400
> #      Sun Jul 02 00:32:09 2017 -0400
> # Node ID 5684913c02965f4a6bb43320e8aa189333962dd1
> # Parent  61ed2cc98fd17c50c7adf634d777d946781a9fc1
> hooks: allow Unix style environment variables on external Windows hooks
> 
> This will help making common hooks between Windows and non-Windows platforms.
> The hook is launched via cmd.exe, which doesn't understand $var, nor single
> quotes.  Therefore, all that is handled is converting $var to %var%, and
> allowing a literal '$' if escaped with '\'.  Like Unix variables, Windows will
> substitute in the middle of a word (e.g. foo%bar%), so I don't think there are
> any other special cases to handle.

(+CC foozy, another Windows expert)

Perhaps we'll need to update the help. '$' could be included in a valid
filename, which now escape is required.

> --- a/mercurial/hook.py
> +++ b/mercurial/hook.py
> @@ -8,6 +8,7 @@
>  from __future__ import absolute_import
>  
>  import os
> +import re
>  import sys
>  
>  from .i18n import _
> @@ -116,6 +117,17 @@
>      return r, False
>  
>  def _exthook(ui, repo, htype, name, cmd, args, throw):
> +    if pycompat.osname == 'nt':
> +        # Replace Unix style environment variables with Windows style variables,
> +        # for replacement by cmd.exe.  Treat '\$' as a literal $.
> +        def replacer(m):
> +            prefix = m.group(1)
> +            if prefix == '\\':
> +                return '$' + m.group(2)
> +            return prefix + '%' + m.group(2) + '%'
> +
> +        cmd = re.sub(br'(.?)\$(.+?)\b', replacer, cmd)

Can you move this to windows.py, which hosts several shell-related functions.
I think the function can have some doctests.


More information about the Mercurial-devel mailing list