Encapsulate Windows shell conditional

This commit is contained in:
Tim Pope 2014-06-21 11:11:17 -04:00
parent 585fcc8a9b
commit 336c716ba0

View File

@ -26,10 +26,14 @@ function! s:gsub(str,pat,rep) abort
return substitute(a:str,'\v\C'.a:pat,a:rep,'g') return substitute(a:str,'\v\C'.a:pat,a:rep,'g')
endfunction endfunction
function! s:winshell() abort
return &shell =~# 'cmd' || &shell =~# 'power'
endfunction
function! s:shellesc(arg) abort function! s:shellesc(arg) abort
if a:arg =~ '^[A-Za-z0-9_/.-]\+$' if a:arg =~ '^[A-Za-z0-9_/.-]\+$'
return a:arg return a:arg
elseif &shell =~# 'cmd' || &shell =~# 'power' elseif s:winshell()
return '"'.s:gsub(s:gsub(a:arg, '"', '""'), '\%', '"%"').'"' return '"'.s:gsub(s:gsub(a:arg, '"', '""'), '\%', '"%"').'"'
else else
return shellescape(a:arg) return shellescape(a:arg)
@ -57,7 +61,7 @@ function! s:warn(str) abort
endfunction endfunction
function! s:shellslash(path) abort function! s:shellslash(path) abort
if exists('+shellslash') && !&shellslash if s:winshell()
return s:gsub(a:path,'\\','/') return s:gsub(a:path,'\\','/')
else else
return a:path return a:path
@ -965,7 +969,7 @@ function! s:Commit(args) abort
try try
try try
execute cd.s:fnameescape(s:repo().tree()) execute cd.s:fnameescape(s:repo().tree())
if &shell =~# 'cmd' || &shell =~# 'power' if s:winshell()
let command = '' let command = ''
let old_editor = $GIT_EDITOR let old_editor = $GIT_EDITOR
let $GIT_EDITOR = 'false' let $GIT_EDITOR = 'false'
@ -2078,14 +2082,14 @@ function! s:ReplaceCmd(cmd,...) abort
let prefix = '' let prefix = ''
try try
if a:0 && a:1 != '' if a:0 && a:1 != ''
if &shell =~# 'cmd' || &shell =~# 'power' if s:winshell()
let old_index = $GIT_INDEX_FILE let old_index = $GIT_INDEX_FILE
let $GIT_INDEX_FILE = a:1 let $GIT_INDEX_FILE = a:1
else else
let prefix = 'env GIT_INDEX_FILE='.s:shellesc(a:1).' ' let prefix = 'env GIT_INDEX_FILE='.s:shellesc(a:1).' '
endif endif
endif endif
if &shell =~# 'cmd' || &shell =~# 'power' if s:winshell()
let cmd_escape_char = &shellxquote == '(' ? '^' : '^^^' let cmd_escape_char = &shellxquote == '(' ? '^' : '^^^'
call system('cmd /c "'.prefix.s:gsub(a:cmd,'[<>]', cmd_escape_char.'&').' > '.tmp.'"') call system('cmd /c "'.prefix.s:gsub(a:cmd,'[<>]', cmd_escape_char.'&').' > '.tmp.'"')
else else
@ -2239,7 +2243,7 @@ function! s:BufWriteIndexFile() abort
endif endif
let info = old_mode.' '.sha1.' '.stage."\t".path let info = old_mode.' '.sha1.' '.stage."\t".path
call writefile([info],tmp) call writefile([info],tmp)
if &shell =~# 'cmd' || &shell =~# 'power' if s:winshell()
let error = system('type '.s:gsub(tmp,'/','\\').'|'.s:repo().git_command('update-index','--index-info')) let error = system('type '.s:gsub(tmp,'/','\\').'|'.s:repo().git_command('update-index','--index-info'))
else else
let error = system(s:repo().git_command('update-index','--index-info').' < '.tmp) let error = system(s:repo().git_command('update-index','--index-info').' < '.tmp)