Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Windows support for Unix shells and powershell #860

Merged
merged 11 commits into from
Aug 16, 2019
88 changes: 49 additions & 39 deletions plug.vim
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,23 @@ if s:is_win
function! s:is_local_plug(repo)
return a:repo =~? '^[a-z]:\|^[%~]'
endfunction

" Copied from fzf
function! s:wrap_cmds(cmds)
return map(['@echo off', 'for /f "tokens=4" %%a in (''chcp'') do set origchcp=%%a', 'chcp 65001 > nul'] +
\ (type(a:cmds) == type([]) ? a:cmds : [a:cmds]) +
\ ['chcp %origchcp% > nul'], 'v:val."\r"')
endfunction

function! s:batchfile(cmd)
let batchfile = tempname().'.bat'
call writefile(s:wrap_cmds(a:cmd), batchfile)
let cmd = plug#shellescape(batchfile, {'shell': &shell, 'script': 1})
if &shell =~# 'powershell\.exe$'
let cmd = '& ' . cmd
endif
return [batchfile, cmd]
endfunction
else
function! s:rtp(spec)
return s:dirpath(a:spec.dir . get(a:spec, 'rtp', ''))
Expand Down Expand Up @@ -807,9 +824,7 @@ function! s:bang(cmd, ...)
" but it won't work on Windows.
let cmd = a:0 ? s:with_cd(a:cmd, a:1) : a:cmd
if s:is_win
let batchfile = tempname().'.bat'
call writefile(["@echo off\r", cmd . "\r"], batchfile)
let cmd = s:shellesc(expand(batchfile))
let [batchfile, cmd] = s:batchfile(cmd)
endif
let g:_plug_bang = (s:is_win && has('gui_running') ? 'silent ' : '').'!'.escape(cmd, '#!%')
execute "normal! :execute g:_plug_bang\<cr>\<cr>"
Expand Down Expand Up @@ -1092,7 +1107,7 @@ function! s:update_finish()
elseif has_key(spec, 'tag')
let tag = spec.tag
if tag =~ '\*'
let tags = s:lines(s:system('git tag --list '.s:shellesc(tag).' --sort -version:refname 2>&1', spec.dir))
let tags = s:lines(s:system('git tag --list '.plug#shellescape(tag).' --sort -version:refname 2>&1', spec.dir))
if !v:shell_error && !empty(tags)
let tag = tags[0]
call s:log4(name, printf('Latest tag for %s -> %s', spec.tag, tag))
Expand Down Expand Up @@ -1149,7 +1164,7 @@ function! s:job_abort()
silent! call job_stop(j.jobid)
endif
if j.new
call s:system('rm -rf ' . s:shellesc(g:plugs[name].dir))
call s:system('rm -rf ' . plug#shellescape(g:plugs[name].dir))
endif
endfor
let s:jobs = {}
Expand Down Expand Up @@ -1202,15 +1217,10 @@ endfunction

function! s:spawn(name, cmd, opts)
let job = { 'name': a:name, 'running': 1, 'error': 0, 'lines': [''],
\ 'batchfile': (s:is_win && (s:nvim || s:vim8)) ? tempname().'.bat' : '',
\ 'new': get(a:opts, 'new', 0) }
let s:jobs[a:name] = job
let cmd = has_key(a:opts, 'dir') ? s:with_cd(a:cmd, a:opts.dir) : a:cmd
if !empty(job.batchfile)
call writefile(["@echo off\r", cmd . "\r"], job.batchfile)
let cmd = s:shellesc(expand(job.batchfile))
endif
let argv = add(s:is_win ? ['cmd', '/c'] : ['sh', '-c'], cmd)
let cmd = has_key(a:opts, 'dir') ? s:with_cd(a:cmd, a:opts.dir, 0) : a:cmd
let argv = s:is_win ? ['cmd', '/s', '/c', '"'.cmd.'"'] : ['sh', '-c', cmd]
janlazo marked this conversation as resolved.
Show resolved Hide resolved

if s:nvim
call extend(job, {
Expand Down Expand Up @@ -1260,9 +1270,6 @@ function! s:reap(name)
call s:log(bullet, a:name, empty(result) ? 'OK' : result)
call s:bar()

if has_key(job, 'batchfile') && !empty(job.batchfile)
call delete(job.batchfile)
endif
call remove(s:jobs, a:name)
endfunction

Expand Down Expand Up @@ -1352,8 +1359,8 @@ while 1 " Without TCO, Vim stack is bound to explode
\ printf('git clone %s %s %s %s 2>&1',
\ has_tag ? '' : s:clone_opt,
\ prog,
\ s:shellesc(spec.uri),
\ s:shellesc(s:trim(spec.dir))), { 'new': 1 })
\ plug#shellescape(spec.uri, {'script': 0}),
\ plug#shellescape(s:trim(spec.dir), {'script': 0})), { 'new': 1 })
endif

if !s:jobs[name].running
Expand Down Expand Up @@ -1980,17 +1987,23 @@ function! s:update_ruby()
EOF
endfunction

function! s:shellesc_cmd(arg)
let escaped = substitute(a:arg, '[&|<>()@^]', '^&', 'g')
let escaped = substitute(escaped, '%', '%%', 'g')
let escaped = substitute(escaped, '"', '\\^&', 'g')
let escaped = substitute(escaped, '\(\\\+\)\(\\^\)', '\1\1\2', 'g')
return '^"'.substitute(escaped, '\(\\\+\)$', '\1\1', '').'^"'
function! s:shellesc_cmd(arg, script)
let escaped = substitute('"'.a:arg.'"', '[&|<>()@^!"]', '^&', 'g')
return substitute(escaped, '%', (a:script ? '%' : '^') . '&', 'g')
endfunction

function! s:shellesc_ps1(arg)
return "'".substitute(escape(a:arg, '\"'), "'", "''", 'g')."'"
endfunction

function! s:shellesc(arg)
if &shell =~# 'cmd.exe$'
return s:shellesc_cmd(a:arg)
function! plug#shellescape(arg, ...)
let opts = a:0 > 0 && type(a:1) == s:TYPE.dict ? a:1 : {}
let shell = get(opts, 'shell', s:is_win ? 'cmd.exe' : 'sh')
let script = get(opts, 'script', 1)
if shell =~# 'cmd\.exe$'
return s:shellesc_cmd(a:arg, script)
elseif shell =~# 'powershell\.exe$' || shell =~# 'pwsh$'
return s:shellesc_ps1(a:arg)
endif
return shellescape(a:arg)
endfunction
Expand Down Expand Up @@ -2024,18 +2037,17 @@ function! s:format_message(bullet, name, message)
endif
endfunction

function! s:with_cd(cmd, dir)
return printf('cd%s %s && %s', s:is_win ? ' /d' : '', s:shellesc(a:dir), a:cmd)
function! s:with_cd(cmd, dir, ...)
let script = a:0 > 0 ? a:1 : 1
return printf('cd%s %s && %s', s:is_win ? ' /d' : '', plug#shellescape(a:dir, {'script': script}), a:cmd)
endfunction

function! s:system(cmd, ...)
try
let [sh, shellcmdflag, shrd] = s:chsh(1)
let cmd = a:0 > 0 ? s:with_cd(a:cmd, a:1) : a:cmd
if s:is_win
let batchfile = tempname().'.bat'
call writefile(["@echo off\r", cmd . "\r"], batchfile)
let cmd = s:shellesc(expand(batchfile))
let [batchfile, cmd] = s:batchfile(cmd)
endif
return system(cmd)
finally
Expand Down Expand Up @@ -2113,7 +2125,7 @@ endfunction

function! s:rm_rf(dir)
if isdirectory(a:dir)
call s:system((s:is_win ? 'rmdir /S /Q ' : 'rm -rf ') . s:shellesc(a:dir))
call s:system((s:is_win ? 'rmdir /S /Q ' : 'rm -rf ') . plug#shellescape(a:dir))
endif
endfunction

Expand Down Expand Up @@ -2222,7 +2234,7 @@ function! s:upgrade()
let new = tmp . '/plug.vim'

try
let out = s:system(printf('git clone --depth 1 %s %s', s:shellesc(s:plug_src), s:shellesc(tmp)))
let out = s:system(printf('git clone --depth 1 %s %s', plug#shellescape(s:plug_src), plug#shellescape(tmp)))
if v:shell_error
return s:err('Error upgrading vim-plug: '. out)
endif
Expand Down Expand Up @@ -2365,11 +2377,9 @@ function! s:preview_commit()
setlocal previewwindow filetype=git buftype=nofile nobuflisted modifiable
try
let [sh, shellcmdflag, shrd] = s:chsh(1)
let cmd = 'cd '.s:shellesc(g:plugs[name].dir).' && git show --no-color --pretty=medium '.sha
let cmd = 'cd '.plug#shellescape(g:plugs[name].dir).' && git show --no-color --pretty=medium '.sha
if s:is_win
let batchfile = tempname().'.bat'
call writefile(["@echo off\r", cmd . "\r"], batchfile)
let cmd = expand(batchfile)
let [batchfile, cmd] = s:batchfile(cmd)
endif
execute 'silent %!' cmd
finally
Expand Down Expand Up @@ -2418,9 +2428,9 @@ function! s:diff()
call s:append_ul(2, origin ? 'Pending updates:' : 'Last update:')
for [k, v] in plugs
let range = origin ? '..origin/'.v.branch : 'HEAD@{1}..'
let cmd = 'git log --graph --color=never '.join(map(['--pretty=format:%x01%h%x01%d%x01%s%x01%cr', range], 's:shellesc(v:val)'))
let cmd = 'git log --graph --color=never '.join(map(['--pretty=format:%x01%h%x01%d%x01%s%x01%cr', range], 'plug#shellescape(v:val)'))
if has_key(v, 'rtp')
let cmd .= ' -- '.s:shellesc(v.rtp)
let cmd .= ' -- '.plug#shellescape(v.rtp)
endif
let diff = s:system_chomp(cmd, v.dir)
if !empty(diff)
Expand Down
37 changes: 37 additions & 0 deletions test/functional.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Execute (plug#shellescape() works without optional arguments):
if has('unix')
AssertEqual "''", plug#shellescape("")
AssertEqual "'foo'\\'''", plug#shellescape("foo'")
endif

Execute (plug#shellescape() ignores invalid optional argument):
if has('unix')
AssertEqual "''", plug#shellescape("", '')
AssertEqual "'foo'\\'''", plug#shellescape("foo'", [])
endif

Execute (plug#shellescape() depends on the shell):
AssertEqual "'foo'\\'''", plug#shellescape("foo'", {'shell': 'sh'})
AssertEqual '^"foo''^"', plug#shellescape("foo'", {'shell': 'cmd.exe'})
AssertEqual "'foo'''", plug#shellescape("foo'", {'shell': 'powershell.exe'})
AssertEqual "'foo'''", plug#shellescape("foo'", {'shell': 'pwsh'})

Execute (plug#shellescape() supports non-trivial cmd.exe escaping):
" batchfile
AssertEqual '^"^^%%PATH^^%%^"', plug#shellescape("^%PATH^%", {
\ 'shell': 'cmd.exe',
\ })
AssertEqual '^"^^%%PATH^^%%^"', plug#shellescape("^%PATH^%", {
\ 'shell': 'cmd.exe',
\ 'script': 1,
\ })
" command prompt
AssertEqual '^"^^^%PATH^^^%^"', plug#shellescape("^%PATH^%", {
junegunn marked this conversation as resolved.
Show resolved Hide resolved
\ 'shell': 'cmd.exe',
\ 'script': 0,
\ }),

Execute (plug#shellescape() supports non-trivial powershell.exe escaping):
AssertEqual '''\"Foo\\''''\\Bar\"''', plug#shellescape('"Foo\''\Bar"', {
\ 'shell': 'powershell.exe',
\ }),
1 change: 1 addition & 0 deletions test/test.vader
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Execute (Print Interpreter Version):

Include: workflow.vader
Include: regressions.vader
Include: functional.vader

Execute (Cleanup):
silent! call RmRf(g:temp_plugged)
Expand Down