Escape shell commands on windows.

This commit is contained in:
Andy Stewart
2013-08-07 10:02:18 +02:00
parent ff031d022f
commit da7900a809

View File

@@ -82,6 +82,18 @@ function! s:directory_of_file()
return shellescape(fnamemodify(s:file(), ':h'))
endfunction
" https://github.com/tpope/vim-dispatch/blob/9cdd05a87f8a47120335be03dfcd8358544221cd/autoload/dispatch/windows.vim#L8-L17
function! s:escape(str)
if &shellxquote ==# '"'
return '"' . substitute(a:str, '"', '""', 'g') . '"'
else
let esc = exists('+shellxescape') ? &shellxescape : '"&|<>()@^'
return &shellquote .
\ substitute(a:str, '['.esc.']', '^&', 'g') .
\ get({'(': ')', '"(': ')"'}, &shellquote, &shellquote)
endif
endfunction
function! s:discard_stdout_and_stderr()
if !exists('s:discard')
if &shellredir ==? '>%s 2>&1'
@@ -98,13 +110,13 @@ function! s:command_in_directory_of_file(cmd)
endfunction
function! s:is_in_a_git_repo()
let cmd = 'git rev-parse' . s:discard_stdout_and_stderr()
let cmd = s:escape('git rev-parse' . s:discard_stdout_and_stderr())
call system(s:command_in_directory_of_file(cmd))
return !v:shell_error
endfunction
function! s:is_tracked_by_git()
let cmd = 'git ls-files --error-unmatch' . s:discard_stdout_and_stderr() . ' ' . shellescape(s:file())
let cmd = s:escape('git ls-files --error-unmatch' . s:discard_stdout_and_stderr() . ' ' . shellescape(s:file()))
call system(s:command_in_directory_of_file(cmd))
return !v:shell_error
endfunction
@@ -196,6 +208,7 @@ function! s:run_diff()
if s:grep_available
let cmd .= s:grep_command
endif
let cmd = s:escape(cmd)
let diff = system(s:command_in_directory_of_file(cmd))
return diff
endfunction