Skip to content

Commit

Permalink
Fix cmd.exe shellescape. Add plug#shellescape
Browse files Browse the repository at this point in the history
  • Loading branch information
janlazo committed Aug 10, 2019
1 parent b8d4952 commit f0eebef
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
29 changes: 15 additions & 14 deletions plug.vim
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ if s:is_win
function! s:batchfile(cmd)
let batchfile = tempname().'.bat'
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$'
let cmd = '& ' . cmd
endif
Expand Down Expand Up @@ -1107,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 @@ -1164,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 @@ -1359,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, {'script': 0}),
\ s:shellesc(s:trim(spec.dir), {'script': 0})), { '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 @@ -1988,16 +1988,17 @@ EOF
endfunction

function! s:shellesc_cmd(arg, script)
let escaped = substitute(a:arg, '%', (a:script ? '%' : '^') . '&', 'g')
return substitute('"'.escaped.'"', '[&|<>()@^!"]', '^&', 'g')
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, ...)
function! plug#shellescape(arg, ...)
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 script = get(opts, 'script', 1)
if shell =~# 'cmd\.exe$'
Expand Down Expand Up @@ -2039,7 +2040,7 @@ endfunction

function! s:with_cd(cmd, dir, ...)
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

function! s:system(cmd, ...)
Expand Down Expand Up @@ -2125,7 +2126,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 @@ -2234,7 +2235,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 @@ -2377,7 +2378,7 @@ 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, cmd] = s:batchfile(cmd)
endif
Expand Down Expand Up @@ -2428,9 +2429,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
30 changes: 30 additions & 0 deletions test/functional.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Execute (plug#shellescape() works without optional arguments):
if has('unix')
AssertEqual plug#shellescape(""), "''"
AssertEqual plug#shellescape("foo'"), "'foo'\\'''"
endif

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

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

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

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

0 comments on commit f0eebef

Please sign in to comment.