From da7900a809001c3c6bc730eb8e262be499df8dc4 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Wed, 7 Aug 2013 10:02:18 +0200 Subject: [PATCH 1/3] Escape shell commands on windows. --- plugin/gitgutter.vim | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/plugin/gitgutter.vim b/plugin/gitgutter.vim index 4741eb1..1ad3519 100644 --- a/plugin/gitgutter.vim +++ b/plugin/gitgutter.vim @@ -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 From fa6c08b841d6996105383863c33d9552d44f0828 Mon Sep 17 00:00:00 2001 From: "Wilson Y. Wong" Date: Tue, 13 Aug 2013 21:32:00 -0700 Subject: [PATCH 2/3] Fixed escaping --- plugin/gitgutter.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gitgutter.vim b/plugin/gitgutter.vim index 1ad3519..8260111 100644 --- a/plugin/gitgutter.vim +++ b/plugin/gitgutter.vim @@ -89,7 +89,7 @@ function! s:escape(str) else let esc = exists('+shellxescape') ? &shellxescape : '"&|<>()@^' return &shellquote . - \ substitute(a:str, '['.esc.']', '^&', 'g') . + \ substitute(a:str, '['.esc.']', '&', 'g') . \ get({'(': ')', '"(': ')"'}, &shellquote, &shellquote) endif endfunction From e49faaea8fa5d7080b8f8fe1be4c7ad295a60360 Mon Sep 17 00:00:00 2001 From: "Wilson Y. Wong" Date: Tue, 13 Aug 2013 22:16:02 -0700 Subject: [PATCH 3/3] Now working with shellslash --- plugin/gitgutter.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin/gitgutter.vim b/plugin/gitgutter.vim index 8260111..88fb60c 100644 --- a/plugin/gitgutter.vim +++ b/plugin/gitgutter.vim @@ -106,7 +106,8 @@ function! s:discard_stdout_and_stderr() endfunction function! s:command_in_directory_of_file(cmd) - return 'cd ' . s:directory_of_file() . ' && ' . a:cmd + let s:cmd_in_dir = 'cd ' . s:directory_of_file() . ' && ' . a:cmd + return substitute(s:cmd_in_dir, "'", '"', 'g') endfunction function! s:is_in_a_git_repo()