comparison mercurial/windows.py @ 38724:02b5b5c1bba8

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.
author Matt Harbison <matt_harbison@yahoo.com>
date Sun, 15 Jul 2018 23:58:39 -0400
parents 93ed193bc03e
children c382c19ce9bd
comparison
equal deleted inserted replaced
38723:f9b2d996ffa5 38724:02b5b5c1bba8
266 >>> shelltocmdexe(b'cmd $var1 ${var2} %var3% $missing ${missing} %missing%', 266 >>> shelltocmdexe(b'cmd $var1 ${var2} %var3% $missing ${missing} %missing%',
267 ... e) 267 ... e)
268 'cmd %var1% %var2% %var3% $missing ${missing} %missing%' 268 'cmd %var1% %var2% %var3% $missing ${missing} %missing%'
269 >>> # Single quote prevents expansion, as does \$ escaping 269 >>> # Single quote prevents expansion, as does \$ escaping
270 >>> shelltocmdexe(b"cmd '$var1 ${var2} %var3%' \$var1 \${var2} \\", e) 270 >>> shelltocmdexe(b"cmd '$var1 ${var2} %var3%' \$var1 \${var2} \\", e)
271 "cmd '$var1 ${var2} %var3%' $var1 ${var2} \\" 271 'cmd "$var1 ${var2} %var3%" $var1 ${var2} \\'
272 >>> # $$ is not special. %% is not special either, but can be the end and 272 >>> # $$ is not special. %% is not special either, but can be the end and
273 >>> # start of consecutive variables 273 >>> # start of consecutive variables
274 >>> shelltocmdexe(b"cmd $$ %% %var1%%var2%", e) 274 >>> shelltocmdexe(b"cmd $$ %% %var1%%var2%", e)
275 'cmd $$ %% %var1%%var2%' 275 'cmd $$ %% %var1%%var2%'
276 >>> # No double substitution 276 >>> # No double substitution
277 >>> shelltocmdexe(b"$var1 %var1%", {b'var1': b'%var2%', b'var2': b'boom'}) 277 >>> shelltocmdexe(b"$var1 %var1%", {b'var1': b'%var2%', b'var2': b'boom'})
278 '%var1% %var1%' 278 '%var1% %var1%'
279 """ 279 """
280 if b'$' not in path: 280 if not any(c in path for c in b"$'"):
281 return path 281 return path
282 282
283 varchars = pycompat.sysbytes(string.ascii_letters + string.digits) + b'_-' 283 varchars = pycompat.sysbytes(string.ascii_letters + string.digits) + b'_-'
284 284
285 res = b'' 285 res = b''
290 if c == b'\'': # no expansion within single quotes 290 if c == b'\'': # no expansion within single quotes
291 path = path[index + 1:] 291 path = path[index + 1:]
292 pathlen = len(path) 292 pathlen = len(path)
293 try: 293 try:
294 index = path.index(b'\'') 294 index = path.index(b'\'')
295 res += b'\'' + path[:index + 1] 295 res += b'"' + path[:index] + b'"'
296 except ValueError: 296 except ValueError:
297 res += c + path 297 res += c + path
298 index = pathlen - 1 298 index = pathlen - 1
299 elif c == b'%': # variable 299 elif c == b'%': # variable
300 path = path[index + 1:] 300 path = path[index + 1:]