Skip to content

Commit

Permalink
Extract function to reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
airblade committed Jun 5, 2023
1 parent 3531f4e commit 6ebc0fd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
12 changes: 9 additions & 3 deletions autoload/gitgutter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ endfunction

" }}}


function! gitgutter#git()
return g:gitgutter_git_executable.' '.g:gitgutter_git_args
endfunction


function! gitgutter#setup_maps()
if !g:gitgutter_map_keys
return
Expand Down Expand Up @@ -195,14 +201,14 @@ endfunction
" - it ignores unsaved changes in buffers
" - it does not change to the repo root
function! gitgutter#quickfix(current_file)
let cmd = g:gitgutter_git_executable.' '.g:gitgutter_git_args.' rev-parse --show-cdup'
let cmd = gitgutter#git().' rev-parse --show-cdup'
let path_to_repo = get(systemlist(cmd), 0, '')
if !empty(path_to_repo) && path_to_repo[-1:] != '/'
let path_to_repo .= '/'
endif

let locations = []
let cmd = g:gitgutter_git_executable.' '.g:gitgutter_git_args.' --no-pager'.
let cmd = gitgutter#git().' --no-pager'.
\ ' diff --no-ext-diff --no-color -U0'.
\ ' --src-prefix=a/'.path_to_repo.' --dst-prefix=b/'.path_to_repo.' '.
\ g:gitgutter_diff_args. ' '. g:gitgutter_diff_base
Expand Down Expand Up @@ -251,7 +257,7 @@ function! gitgutter#difforig()
if g:gitgutter_diff_relative_to ==# 'index'
let index_name = gitgutter#utility#get_diff_base(bufnr).':'.path
let cmd = gitgutter#utility#cd_cmd(bufnr,
\ g:gitgutter_git_executable.' '.g:gitgutter_git_args.' --no-pager show '.index_name
\ gitgutter#git().' --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.
Expand Down
8 changes: 4 additions & 4 deletions autoload/gitgutter/diff.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let s:hunk_re = '^@@ -\(\d\+\),\?\(\d*\) +\(\d\+\),\?\(\d*\) @@'

" True for git v1.7.2+.
function! s:git_supports_command_line_config_override() abort
call gitgutter#utility#system(g:gitgutter_git_executable.' '.g:gitgutter_git_args.' -c foo.bar=baz --version')
call gitgutter#utility#system(gitgutter#git().' -c foo.bar=baz --version')
return !v:shell_error
endfunction

Expand Down Expand Up @@ -87,7 +87,7 @@ function! gitgutter#diff#run_diff(bufnr, from, preserve_full_diff) abort
" doesn't work for this case.
if !empty(g:gitgutter_diff_base)
let index_name = gitgutter#utility#get_diff_base(a:bufnr).':'.gitgutter#utility#repo_path(a:bufnr, 1)
let cmd = g:gitgutter_git_executable.' '.g:gitgutter_git_args.' --no-pager show '.index_name
let cmd = gitgutter#git().' --no-pager show '.index_name
let cmd = gitgutter#utility#cd_cmd(a:bufnr, cmd)
call gitgutter#utility#system(cmd)
if v:shell_error
Expand Down Expand Up @@ -138,14 +138,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#repo_path(a:bufnr, 1)
let cmd .= g:gitgutter_git_executable.' '.g:gitgutter_git_args.' --no-pager show --textconv '.index_name.' > '.from_file.' && '
let cmd .= gitgutter#git().' --no-pager show --textconv '.index_name.' > '.from_file.' && '

elseif a:from ==# 'working_tree'
let from_file = gitgutter#utility#repo_path(a:bufnr, 1)
endif

" Call git-diff.
let cmd .= g:gitgutter_git_executable.' '.g:gitgutter_git_args.' --no-pager'
let cmd .= gitgutter#git().' --no-pager'
if s:c_flag
let cmd .= ' -c "diff.autorefreshindex=0"'
let cmd .= ' -c "diff.noprefix=false"'
Expand Down
5 changes: 2 additions & 3 deletions autoload/gitgutter/hunk.vim
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,7 @@ function! s:stage(hunk_diff)
let path = gitgutter#utility#repo_path(bufnr, 1)
" Add file to index.
let cmd = gitgutter#utility#cd_cmd(bufnr,
\ g:gitgutter_git_executable.' '.g:gitgutter_git_args.
\ ' add '.
\ gitgutter#git().' add '.
\ gitgutter#utility#shellescape(gitgutter#utility#filename(bufnr)))
call gitgutter#utility#system(cmd)
else
Expand All @@ -314,7 +313,7 @@ function! s:stage(hunk_diff)
let diff = s:adjust_header(bufnr, a:hunk_diff)
" Apply patch to index.
call gitgutter#utility#system(
\ gitgutter#utility#cd_cmd(bufnr, g:gitgutter_git_executable.' '.g:gitgutter_git_args.' apply --cached --unidiff-zero - '),
\ gitgutter#utility#cd_cmd(bufnr, gitgutter#git().' apply --cached --unidiff-zero - '),
\ diff)
endif

Expand Down
6 changes: 2 additions & 4 deletions autoload/gitgutter/utility.vim
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ function! gitgutter#utility#set_repo_path(bufnr, continuation) abort

call gitgutter#utility#setbufvar(a:bufnr, 'path', -1)
let cmd = gitgutter#utility#cd_cmd(a:bufnr,
\ g:gitgutter_git_executable.' '.g:gitgutter_git_args.
\ ' 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)))

if g:gitgutter_async && gitgutter#async#available() && !has('vim_starting')
Expand Down Expand Up @@ -182,8 +181,7 @@ 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,
\ g:gitgutter_git_executable.' '.g:gitgutter_git_args.
\ ' check-attr filter -- '.
\ gitgutter#git().' check-attr filter -- '.
\ gitgutter#utility#shellescape(gitgutter#utility#filename(a:bufnr)))
let out = gitgutter#utility#system(cmd)
let filtered = out !~ 'unspecified'
Expand Down

0 comments on commit 6ebc0fd

Please sign in to comment.