Use git -C instead of cd

This commit is contained in:
Andy Stewart
2024-07-13 06:20:56 +01:00
parent e801371917
commit bed580ab8b
4 changed files with 23 additions and 37 deletions

View File

@@ -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) if empty(g:gitgutter_git_args)
return g:gitgutter_git_executable return git
else else
return g:gitgutter_git_executable.' '.g:gitgutter_git_args return git.' '.g:gitgutter_git_args
endif endif
endfunction endfunction
@@ -258,9 +263,7 @@ function! gitgutter#difforig()
if g:gitgutter_diff_relative_to ==# 'index' if g:gitgutter_diff_relative_to ==# 'index'
let index_name = gitgutter#utility#get_diff_base(bufnr).':'.gitgutter#utility#base_path(bufnr) let index_name = gitgutter#utility#get_diff_base(bufnr).':'.gitgutter#utility#base_path(bufnr)
let cmd = gitgutter#utility#cd_cmd(bufnr, let cmd = gitgutter#git(bufnr).' --no-pager show '.index_name
\ gitgutter#git().' --no-pager show '.index_name
\ )
" NOTE: this uses &shell to execute cmd. Perhaps we should use instead " NOTE: this uses &shell to execute cmd. Perhaps we should use instead
" gitgutter#utility's use_known_shell() / restore_shell() functions. " gitgutter#utility's use_known_shell() / restore_shell() functions.
silent! execute "read ++edit !" cmd silent! execute "read ++edit !" cmd

View File

@@ -116,14 +116,14 @@ function! gitgutter#diff#run_diff(bufnr, from, preserve_full_diff) abort
" Write file from index to temporary file. " Write file from index to temporary file.
let index_name = gitgutter#utility#get_diff_base(a:bufnr).':'.gitgutter#utility#base_path(a:bufnr) 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' elseif a:from ==# 'working_tree'
let from_file = gitgutter#utility#repo_path(a:bufnr, 1) let from_file = gitgutter#utility#repo_path(a:bufnr, 1)
endif endif
" Call git-diff. " 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() if gitgutter#utility#git_supports_command_line_config_override()
let cmd .= ' -c "diff.autorefreshindex=0"' let cmd .= ' -c "diff.autorefreshindex=0"'
let cmd .= ' -c "diff.noprefix=false"' 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 .= ')'
let cmd = gitgutter#utility#cd_cmd(a:bufnr, cmd)
if g:gitgutter_async && gitgutter#async#available() if g:gitgutter_async && gitgutter#async#available()
call gitgutter#async#execute(cmd, a:bufnr, { call gitgutter#async#execute(cmd, a:bufnr, {
\ 'out': function('gitgutter#diff#handler'), \ 'out': function('gitgutter#diff#handler'),

View File

@@ -308,9 +308,8 @@ function! s:stage(hunk_diff)
write write
let path = gitgutter#utility#repo_path(bufnr, 1) let path = gitgutter#utility#repo_path(bufnr, 1)
" Add file to index. " Add file to index.
let cmd = gitgutter#utility#cd_cmd(bufnr, let cmd = gitgutter#git(bufnr).' add '.
\ gitgutter#git().' add '. \ gitgutter#utility#shellescape(gitgutter#utility#filename(bufnr))
\ gitgutter#utility#shellescape(gitgutter#utility#filename(bufnr)))
let [_, error_code] = gitgutter#utility#system(cmd) let [_, error_code] = gitgutter#utility#system(cmd)
else else
return return
@@ -320,7 +319,7 @@ function! s:stage(hunk_diff)
let diff = s:adjust_header(bufnr, a:hunk_diff) let diff = s:adjust_header(bufnr, a:hunk_diff)
" Apply patch to index. " Apply patch to index.
let [_, error_code] = gitgutter#utility#system( 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) \ diff)
endif endif

View File

@@ -66,7 +66,7 @@ function! gitgutter#utility#is_active(bufnr) abort
endfunction endfunction
function! s:not_git_dir(bufnr) abort function! s:not_git_dir(bufnr) abort
return s:dir(a:bufnr) !~ '[/\\]\.git\($\|[/\\]\)' return gitgutter#utility#dir(a:bufnr) !~ '[/\\]\.git\($\|[/\\]\)'
endfunction endfunction
function! s:is_file_buffer(bufnr) abort function! s:is_file_buffer(bufnr) abort
@@ -162,9 +162,8 @@ function! gitgutter#utility#set_repo_path(bufnr, continuation) abort
" * -3 - assume unchanged " * -3 - assume unchanged
call gitgutter#utility#setbufvar(a:bufnr, 'path', -1) call gitgutter#utility#setbufvar(a:bufnr, 'path', -1)
let cmd = gitgutter#utility#cd_cmd(a:bufnr, let cmd = gitgutter#git(a:bufnr).' ls-files -v --error-unmatch --full-name -z -- '.
\ gitgutter#git().' ls-files -v --error-unmatch --full-name -z -- '. \ gitgutter#utility#shellescape(gitgutter#utility#filename(a:bufnr))
\ gitgutter#utility#shellescape(gitgutter#utility#filename(a:bufnr)))
if g:gitgutter_async && gitgutter#async#available() && !has('vim_starting') if g:gitgutter_async && gitgutter#async#available() && !has('vim_starting')
let handler = copy(s:set_path_handler) let handler = copy(s:set_path_handler)
@@ -193,9 +192,8 @@ endfunction
function! gitgutter#utility#clean_smudge_filter_applies(bufnr) function! gitgutter#utility#clean_smudge_filter_applies(bufnr)
let filtered = gitgutter#utility#getbufvar(a:bufnr, 'filter', -1) let filtered = gitgutter#utility#getbufvar(a:bufnr, 'filter', -1)
if filtered == -1 if filtered == -1
let cmd = gitgutter#utility#cd_cmd(a:bufnr, let cmd = gitgutter#git(a:bufnr).' check-attr filter -- '.
\ gitgutter#git().' check-attr filter -- '. \ gitgutter#utility#shellescape(gitgutter#utility#filename(a:bufnr))
\ gitgutter#utility#shellescape(gitgutter#utility#filename(a:bufnr)))
let [out, _] = gitgutter#utility#system(cmd) let [out, _] = gitgutter#utility#system(cmd)
let filtered = out !~ 'unspecified' let filtered = out !~ 'unspecified'
call gitgutter#utility#setbufvar(a:bufnr, 'filter', filtered) call gitgutter#utility#setbufvar(a:bufnr, 'filter', filtered)
@@ -204,19 +202,6 @@ function! gitgutter#utility#clean_smudge_filter_applies(bufnr)
endfunction 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 function! s:use_known_shell() abort
if has('unix') && &shell !=# 'sh' if has('unix') && &shell !=# 'sh'
let [s:shell, s:shellcmdflag, s:shellredir, s:shellpipe, s:shellquote, s:shellxquote] = [&shell, &shellcmdflag, &shellredir, &shellpipe, &shellquote, &shellxquote] 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. " Returns a dict of current path to original path at the given base.
function! s:obtain_file_renames(bufnr, base) function! s:obtain_file_renames(bufnr, base)
let renames = {} let renames = {}
let cmd = gitgutter#git() let cmd = gitgutter#git(a:bufnr)
if gitgutter#utility#git_supports_command_line_config_override() if gitgutter#utility#git_supports_command_line_config_override()
let cmd .= ' -c "core.safecrlf=false"' let cmd .= ' -c "core.safecrlf=false"'
endif endif
let cmd .= ' diff --diff-filter=R --name-status '.a:base 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 if error_code
" Assume the problem is the diff base. " Assume the problem is the diff base.
call gitgutter#utility#warn('g:gitgutter_diff_base ('.a:base.') is invalid') 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 return a:shellesc ? gitgutter#utility#shellescape(p) : p
endfunction 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')) return gitgutter#utility#shellescape(fnamemodify(s:abs_path(a:bufnr, 0), ':h'))
endfunction endfunction