Add plug#shellescape

- fix cmd.exe shellescape for '%'
- support pwsh (powershell on Linux)
This commit is contained in:
Jan Edmund Lazo 2019-08-10 02:18:54 -04:00
parent b8d4952305
commit ebc1690f4d
3 changed files with 54 additions and 15 deletions

View File

@ -361,7 +361,7 @@ if s:is_win
function! s:batchfile(cmd) function! s:batchfile(cmd)
let batchfile = tempname().'.bat' let batchfile = tempname().'.bat'
call writefile(s:wrap_cmds(a:cmd), batchfile) call writefile(s:wrap_cmds(a:cmd), batchfile)
let cmd = s:shellesc(batchfile, {'shell': &shell, 'script': 1}) let cmd = plug#shellescape(batchfile, {'shell': &shell, 'script': 1})
if &shell =~# 'powershell\.exe$' if &shell =~# 'powershell\.exe$'
let cmd = '& ' . cmd let cmd = '& ' . cmd
endif endif
@ -1107,7 +1107,7 @@ function! s:update_finish()
elseif has_key(spec, 'tag') elseif has_key(spec, 'tag')
let tag = spec.tag let tag = spec.tag
if 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) if !v:shell_error && !empty(tags)
let tag = tags[0] let tag = tags[0]
call s:log4(name, printf('Latest tag for %s -> %s', spec.tag, tag)) call s:log4(name, printf('Latest tag for %s -> %s', spec.tag, tag))
@ -1164,7 +1164,7 @@ function! s:job_abort()
silent! call job_stop(j.jobid) silent! call job_stop(j.jobid)
endif endif
if j.new 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 endif
endfor endfor
let s:jobs = {} let s:jobs = {}
@ -1359,8 +1359,8 @@ while 1 " Without TCO, Vim stack is bound to explode
\ printf('git clone %s %s %s %s 2>&1', \ printf('git clone %s %s %s %s 2>&1',
\ has_tag ? '' : s:clone_opt, \ has_tag ? '' : s:clone_opt,
\ prog, \ prog,
\ s:shellesc(spec.uri, {'script': 0}), \ plug#shellescape(spec.uri, {'script': 0}),
\ s:shellesc(s:trim(spec.dir), {'script': 0})), { 'new': 1 }) \ plug#shellescape(s:trim(spec.dir), {'script': 0})), { 'new': 1 })
endif endif
if !s:jobs[name].running if !s:jobs[name].running
@ -1988,21 +1988,22 @@ EOF
endfunction endfunction
function! s:shellesc_cmd(arg, script) function! s:shellesc_cmd(arg, script)
let escaped = substitute(a:arg, '%', (a:script ? '%' : '^') . '&', 'g') let escaped = substitute('"'.a:arg.'"', '[&|<>()@^!"]', '^&', 'g')
return substitute('"'.escaped.'"', '[&|<>()@^!"]', '^&', 'g') return substitute(escaped, '%', (a:script ? '%' : '^') . '&', 'g')
endfunction endfunction
function! s:shellesc_ps1(arg) function! s:shellesc_ps1(arg)
return "'".substitute(escape(a:arg, '\"'), "'", "''", 'g')."'" return "'".substitute(escape(a:arg, '\"'), "'", "''", 'g')."'"
endfunction endfunction
function! s:shellesc(arg, ...) function! plug#shellescape(arg, ...)
let opts = get(a:000, 0, {}) let opts = get(a:000, 0, {})
let opts = type(opts) == s:TYPE.dict ? opts : {}
let shell = get(opts, 'shell', s:is_win ? 'cmd.exe' : 'sh') let shell = get(opts, 'shell', s:is_win ? 'cmd.exe' : 'sh')
let script = get(opts, 'script', 1) let script = get(opts, 'script', 1)
if shell =~# 'cmd\.exe$' if shell =~# 'cmd\.exe$'
return s:shellesc_cmd(a:arg, script) return s:shellesc_cmd(a:arg, script)
elseif shell =~# 'powershell\.exe$' elseif shell =~# 'powershell\.exe$' || shell =~# 'pwsh$'
return s:shellesc_ps1(a:arg) return s:shellesc_ps1(a:arg)
endif endif
return shellescape(a:arg) return shellescape(a:arg)
@ -2039,7 +2040,7 @@ endfunction
function! s:with_cd(cmd, dir, ...) function! s:with_cd(cmd, dir, ...)
let script = get(a:000, 0, 1) let script = get(a:000, 0, 1)
return printf('cd%s %s && %s', s:is_win ? ' /d' : '', s:shellesc(a:dir, {'script': script}), a:cmd) return printf('cd%s %s && %s', s:is_win ? ' /d' : '', plug#shellescape(a:dir, {'script': script}), a:cmd)
endfunction endfunction
function! s:system(cmd, ...) function! s:system(cmd, ...)
@ -2125,7 +2126,7 @@ endfunction
function! s:rm_rf(dir) function! s:rm_rf(dir)
if isdirectory(a: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 endif
endfunction endfunction
@ -2234,7 +2235,7 @@ function! s:upgrade()
let new = tmp . '/plug.vim' let new = tmp . '/plug.vim'
try 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 if v:shell_error
return s:err('Error upgrading vim-plug: '. out) return s:err('Error upgrading vim-plug: '. out)
endif endif
@ -2377,7 +2378,7 @@ function! s:preview_commit()
setlocal previewwindow filetype=git buftype=nofile nobuflisted modifiable setlocal previewwindow filetype=git buftype=nofile nobuflisted modifiable
try try
let [sh, shellcmdflag, shrd] = s:chsh(1) 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 if s:is_win
let [batchfile, cmd] = s:batchfile(cmd) let [batchfile, cmd] = s:batchfile(cmd)
endif endif
@ -2428,9 +2429,9 @@ function! s:diff()
call s:append_ul(2, origin ? 'Pending updates:' : 'Last update:') call s:append_ul(2, origin ? 'Pending updates:' : 'Last update:')
for [k, v] in plugs for [k, v] in plugs
let range = origin ? '..origin/'.v.branch : 'HEAD@{1}..' 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') if has_key(v, 'rtp')
let cmd .= ' -- '.s:shellesc(v.rtp) let cmd .= ' -- '.plug#shellescape(v.rtp)
endif endif
let diff = s:system_chomp(cmd, v.dir) let diff = s:system_chomp(cmd, v.dir)
if !empty(diff) if !empty(diff)

37
test/functional.vader Normal file
View File

@ -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^%", {
\ 'shell': 'cmd.exe',
\ 'script': 0,
\ }),
Execute (plug#shellescape() supports non-trivial powershell.exe escaping):
AssertEqual '''\"Foo\\''''\\Bar\"''', plug#shellescape('"Foo\''\Bar"', {
\ 'shell': 'powershell.exe',
\ }),

View File

@ -90,6 +90,7 @@ Execute (Print Interpreter Version):
Include: workflow.vader Include: workflow.vader
Include: regressions.vader Include: regressions.vader
Include: functional.vader
Execute (Cleanup): Execute (Cleanup):
silent! call RmRf(g:temp_plugged) silent! call RmRf(g:temp_plugged)