diff mercurial/windows.py @ 38725:c382c19ce9bd

windows: expand '~/' and '~\' to %USERPROFILE% when translating to cmd.exe It's convenient to be able to reference hooks in a portable location on any platform.
author Matt Harbison <matt_harbison@yahoo.com>
date Mon, 16 Jul 2018 00:32:33 -0400
parents 02b5b5c1bba8
children 47ac5d93d708
line wrap: on
line diff
--- a/mercurial/windows.py	Sun Jul 15 23:58:39 2018 -0400
+++ b/mercurial/windows.py	Mon Jul 16 00:32:33 2018 -0400
@@ -276,8 +276,11 @@
     >>> # No double substitution
     >>> shelltocmdexe(b"$var1 %var1%", {b'var1': b'%var2%', b'var2': b'boom'})
     '%var1% %var1%'
+    >>> # Tilde expansion
+    >>> shelltocmdexe(b"~/dir ~\dir2 ~tmpfile \~/", {})
+    '%USERPROFILE%/dir %USERPROFILE%\\dir2 ~tmpfile ~/'
     """
-    if not any(c in path for c in b"$'"):
+    if not any(c in path for c in b"$'~"):
         return path
 
     varchars = pycompat.sysbytes(string.ascii_letters + string.digits) + b'_-'
@@ -344,9 +347,13 @@
 
                 if c != '':
                     index -= 1
-        elif c == b'\\' and index + 1 < pathlen and path[index + 1] == b'$':
-            # Skip '\', but only if it is escaping $
-            res += b'$'
+        elif (c == b'~' and index + 1 < pathlen
+              and path[index + 1] in (b'\\', b'/')):
+            res += "%USERPROFILE%"
+        elif (c == b'\\' and index + 1 < pathlen
+              and path[index + 1] in (b'$', b'~')):
+            # Skip '\', but only if it is escaping $ or ~
+            res += path[index + 1]
             index += 1
         else:
             res += c