diff --git a/autoload/gitgutter.vim b/autoload/gitgutter.vim index 01bacdc..3fdac4a 100644 --- a/autoload/gitgutter.vim +++ b/autoload/gitgutter.vim @@ -118,11 +118,16 @@ endfunction " }}} -function! gitgutter#git() +" Optional argument is buffer number +function! gitgutter#git(...) + let git = g:gitgutter_git_executable + if a:0 + let git .= ' -C '.gitgutter#utility#dir(a:1) + endif if empty(g:gitgutter_git_args) - return g:gitgutter_git_executable + return git else - return g:gitgutter_git_executable.' '.g:gitgutter_git_args + return git.' '.g:gitgutter_git_args endif endfunction @@ -258,9 +263,7 @@ function! gitgutter#difforig() if g:gitgutter_diff_relative_to ==# 'index' let index_name = gitgutter#utility#get_diff_base(bufnr).':'.gitgutter#utility#base_path(bufnr) - let cmd = gitgutter#utility#cd_cmd(bufnr, - \ gitgutter#git().' --no-pager show '.index_name - \ ) + let cmd = gitgutter#git(bufnr).' --no-pager show '.index_name " NOTE: this uses &shell to execute cmd. Perhaps we should use instead " gitgutter#utility's use_known_shell() / restore_shell() functions. silent! execute "read ++edit !" cmd diff --git a/autoload/gitgutter/diff.vim b/autoload/gitgutter/diff.vim index 6325ca3..484b89d 100644 --- a/autoload/gitgutter/diff.vim +++ b/autoload/gitgutter/diff.vim @@ -116,14 +116,14 @@ function! gitgutter#diff#run_diff(bufnr, from, preserve_full_diff) abort " Write file from index to temporary file. let index_name = gitgutter#utility#get_diff_base(a:bufnr).':'.gitgutter#utility#base_path(a:bufnr) - let cmd .= gitgutter#git().' --no-pager show --textconv '.index_name.' > '.from_file.' || exit 0) && (' + let cmd .= gitgutter#git(a:bufnr).' --no-pager show --textconv '.index_name.' > '.from_file.' || exit 0) && (' elseif a:from ==# 'working_tree' let from_file = gitgutter#utility#repo_path(a:bufnr, 1) endif " Call git-diff. - let cmd .= gitgutter#git().' --no-pager' + let cmd .= gitgutter#git(a:bufnr).' --no-pager' if gitgutter#utility#git_supports_command_line_config_override() let cmd .= ' -c "diff.autorefreshindex=0"' let cmd .= ' -c "diff.noprefix=false"' @@ -144,8 +144,6 @@ function! gitgutter#diff#run_diff(bufnr, from, preserve_full_diff) abort let cmd .= ')' - let cmd = gitgutter#utility#cd_cmd(a:bufnr, cmd) - if g:gitgutter_async && gitgutter#async#available() call gitgutter#async#execute(cmd, a:bufnr, { \ 'out': function('gitgutter#diff#handler'), diff --git a/autoload/gitgutter/hunk.vim b/autoload/gitgutter/hunk.vim index c6c5387..7eda8ac 100644 --- a/autoload/gitgutter/hunk.vim +++ b/autoload/gitgutter/hunk.vim @@ -308,9 +308,8 @@ function! s:stage(hunk_diff) write let path = gitgutter#utility#repo_path(bufnr, 1) " Add file to index. - let cmd = gitgutter#utility#cd_cmd(bufnr, - \ gitgutter#git().' add '. - \ gitgutter#utility#shellescape(gitgutter#utility#filename(bufnr))) + let cmd = gitgutter#git(bufnr).' add '. + \ gitgutter#utility#shellescape(gitgutter#utility#filename(bufnr)) let [_, error_code] = gitgutter#utility#system(cmd) else return @@ -320,7 +319,7 @@ function! s:stage(hunk_diff) let diff = s:adjust_header(bufnr, a:hunk_diff) " Apply patch to index. let [_, error_code] = gitgutter#utility#system( - \ gitgutter#utility#cd_cmd(bufnr, gitgutter#git().' apply --cached --unidiff-zero - '), + \ gitgutter#git(bufnr).' apply --cached --unidiff-zero - ', \ diff) endif diff --git a/autoload/gitgutter/utility.vim b/autoload/gitgutter/utility.vim index 9c2b9c3..d657938 100644 --- a/autoload/gitgutter/utility.vim +++ b/autoload/gitgutter/utility.vim @@ -66,7 +66,7 @@ function! gitgutter#utility#is_active(bufnr) abort endfunction function! s:not_git_dir(bufnr) abort - return s:dir(a:bufnr) !~ '[/\\]\.git\($\|[/\\]\)' + return gitgutter#utility#dir(a:bufnr) !~ '[/\\]\.git\($\|[/\\]\)' endfunction function! s:is_file_buffer(bufnr) abort @@ -162,9 +162,8 @@ function! gitgutter#utility#set_repo_path(bufnr, continuation) abort " * -3 - assume unchanged call gitgutter#utility#setbufvar(a:bufnr, 'path', -1) - let cmd = gitgutter#utility#cd_cmd(a:bufnr, - \ gitgutter#git().' ls-files -v --error-unmatch --full-name -z -- '. - \ gitgutter#utility#shellescape(gitgutter#utility#filename(a:bufnr))) + let cmd = gitgutter#git(a:bufnr).' ls-files -v --error-unmatch --full-name -z -- '. + \ gitgutter#utility#shellescape(gitgutter#utility#filename(a:bufnr)) if g:gitgutter_async && gitgutter#async#available() && !has('vim_starting') let handler = copy(s:set_path_handler) @@ -193,9 +192,8 @@ endfunction function! gitgutter#utility#clean_smudge_filter_applies(bufnr) let filtered = gitgutter#utility#getbufvar(a:bufnr, 'filter', -1) if filtered == -1 - let cmd = gitgutter#utility#cd_cmd(a:bufnr, - \ gitgutter#git().' check-attr filter -- '. - \ gitgutter#utility#shellescape(gitgutter#utility#filename(a:bufnr))) + let cmd = gitgutter#git(a:bufnr).' check-attr filter -- '. + \ gitgutter#utility#shellescape(gitgutter#utility#filename(a:bufnr)) let [out, _] = gitgutter#utility#system(cmd) let filtered = out !~ 'unspecified' call gitgutter#utility#setbufvar(a:bufnr, 'filter', filtered) @@ -204,19 +202,6 @@ function! gitgutter#utility#clean_smudge_filter_applies(bufnr) endfunction -function! gitgutter#utility#cd_cmd(bufnr, cmd) abort - let cd = s:unc_path(a:bufnr) ? 'pushd' : (gitgutter#utility#windows() && s:dos_shell() ? 'cd /d' : 'cd') - return cd.' '.s:dir(a:bufnr).' && '.a:cmd -endfunction - -function! s:unc_path(bufnr) - return s:abs_path(a:bufnr, 0) =~ '^\\\\' -endfunction - -function! s:dos_shell() - return &shell == 'cmd.exe' || &shell == 'command.com' -endfunction - function! s:use_known_shell() abort if has('unix') && &shell !=# 'sh' let [s:shell, s:shellcmdflag, s:shellredir, s:shellpipe, s:shellquote, s:shellxquote] = [&shell, &shellcmdflag, &shellredir, &shellpipe, &shellquote, &shellxquote] @@ -303,12 +288,12 @@ endfunction " Returns a dict of current path to original path at the given base. function! s:obtain_file_renames(bufnr, base) let renames = {} - let cmd = gitgutter#git() + let cmd = gitgutter#git(a:bufnr) if gitgutter#utility#git_supports_command_line_config_override() let cmd .= ' -c "core.safecrlf=false"' endif let cmd .= ' diff --diff-filter=R --name-status '.a:base - let [out, error_code] = gitgutter#utility#system(gitgutter#utility#cd_cmd(a:bufnr, cmd)) + let [out, error_code] = gitgutter#utility#system(cmd) if error_code " Assume the problem is the diff base. call gitgutter#utility#warn('g:gitgutter_diff_base ('.a:base.') is invalid') @@ -335,7 +320,8 @@ function! s:abs_path(bufnr, shellesc) return a:shellesc ? gitgutter#utility#shellescape(p) : p endfunction -function! s:dir(bufnr) abort +" Shellescaped +function! gitgutter#utility#dir(bufnr) abort return gitgutter#utility#shellescape(fnamemodify(s:abs_path(a:bufnr, 0), ':h')) endfunction