windows: replace single quote with double quote when translating to cmd.exe
authorMatt Harbison <matt_harbison@yahoo.com>
Sun, 15 Jul 2018 23:58:39 -0400
changeset 38724 02b5b5c1bba8
parent 38723 f9b2d996ffa5
child 38725 c382c19ce9bd
windows: replace single quote with double quote when translating to cmd.exe Since cmd.exe doesn't understand single quotes, single quotes to prevent $var expansion is basically unusable without this. Single quote isn't allowed in a path name, so it seems unlikely to come up otherwise.
mercurial/help/config.txt
mercurial/windows.py
tests/test-histedit-fold.t
--- a/mercurial/help/config.txt	Sun Jul 15 23:51:43 2018 -0400
+++ b/mercurial/help/config.txt	Sun Jul 15 23:58:39 2018 -0400
@@ -893,7 +893,8 @@
 
   Some basic Unix syntax can be enabled for portability, including ``$VAR``
   and ``${VAR}`` style variables.  To use a literal ``$``, it must be
-  escaped with a back slash or inside of a strong quote.
+  escaped with a back slash or inside of a strong quote.  Strong quotes
+  will be replaced by double quotes after processing.
 
   This feature is enabled by adding a prefix of ``tonative.`` to the hook
   name on a new line, and setting it to ``True``.  For example::
--- a/mercurial/windows.py	Sun Jul 15 23:51:43 2018 -0400
+++ b/mercurial/windows.py	Sun Jul 15 23:58:39 2018 -0400
@@ -268,7 +268,7 @@
     'cmd %var1% %var2% %var3% $missing ${missing} %missing%'
     >>> # Single quote prevents expansion, as does \$ escaping
     >>> shelltocmdexe(b"cmd '$var1 ${var2} %var3%' \$var1 \${var2} \\", e)
-    "cmd '$var1 ${var2} %var3%' $var1 ${var2} \\"
+    'cmd "$var1 ${var2} %var3%" $var1 ${var2} \\'
     >>> # $$ is not special. %% is not special either, but can be the end and
     >>> # start of consecutive variables
     >>> shelltocmdexe(b"cmd $$ %% %var1%%var2%", e)
@@ -277,7 +277,7 @@
     >>> shelltocmdexe(b"$var1 %var1%", {b'var1': b'%var2%', b'var2': b'boom'})
     '%var1% %var1%'
     """
-    if b'$' not in path:
+    if not any(c in path for c in b"$'"):
         return path
 
     varchars = pycompat.sysbytes(string.ascii_letters + string.digits) + b'_-'
@@ -292,7 +292,7 @@
             pathlen = len(path)
             try:
                 index = path.index(b'\'')
-                res += b'\'' + path[:index + 1]
+                res += b'"' + path[:index] + b'"'
             except ValueError:
                 res += c + path
                 index = pathlen - 1
--- a/tests/test-histedit-fold.t	Sun Jul 15 23:51:43 2018 -0400
+++ b/tests/test-histedit-fold.t	Sun Jul 15 23:58:39 2018 -0400
@@ -499,16 +499,15 @@
   > tonative.post-add = True
   > EOF
 
-TODO: Windows should output double quotes around "also $non-var"
   $ echo "foo" > amended.txt
   $ HGRCPATH=$TESTTMP/tmp.hgrc hg add -v amended.txt
   running hook pre-add: echo no variables
   no variables
   adding amended.txt
   converting hook "post-add" to native (windows !)
-  running hook post-add: echo ran %HG_ARGS%, literal $non-var, 'also $non-var', %HG_RESULT% (windows !)
+  running hook post-add: echo ran %HG_ARGS%, literal $non-var, "also $non-var", %HG_RESULT% (windows !)
   running hook post-add: echo ran $HG_ARGS, literal \$non-var, 'also $non-var', $HG_RESULT (no-windows !)
-  ran add -v amended.txt, literal $non-var, 'also $non-var', 0 (windows !)
+  ran add -v amended.txt, literal $non-var, "also $non-var", 0 (windows !)
   ran add -v amended.txt, literal $non-var, also $non-var, 0 (no-windows !)
   $ hg ci -q --config extensions.largefiles= --amend -I amended.txt
   The fsmonitor extension is incompatible with the largefiles extension and has been disabled. (fsmonitor !)