Skip to content

Commit

Permalink
merge of issue 105
Browse files Browse the repository at this point in the history
  • Loading branch information
Sambojambo101 committed Sep 9, 2013
2 parents f9cac87 + 0e14b62 commit a396dbb
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions plugin/gitgutter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ endfunction
" Utility {{{

function! s:is_active()
return g:gitgutter_enabled && s:exists_file() && s:is_in_a_git_repo() && s:is_tracked_by_git()
return g:gitgutter_enabled && s:exists_file() && s:is_tracked_by_git()
endfunction

function! s:current_file()
Expand All @@ -84,6 +84,13 @@ function! s:directory_of_file()
return shellescape(fnamemodify(s:file(), ':h'))
endfunction

function! s:file_relative_to_repo_root()
" pushd prints directory stack before printing current directory
let dirs = system('pushd ' . s:directory_of_file() . ' && git rev-parse --show-toplevel')
let repo_root_dir = split(dirs)[-1] . '/'
return substitute(s:file, repo_root_dir, '', '')
endfunction

" https://github.com/tpope/vim-dispatch/blob/9cdd05a87f8a47120335be03dfcd8358544221cd/autoload/dispatch/windows.vim#L8-L17
function! s:escape(str)
if &shellxquote ==# '"'
Expand All @@ -107,20 +114,14 @@ function! s:discard_stdout_and_stderr()
return s:discard
endfunction

function! s:command_in_directory_of_file(cmd)
let s:cmd_in_dir = 'pushd ' . s:directory_of_file() . ' && ' . a:cmd
function! s:command_in_directory_of_repo_root(cmd)
let s:cmd_in_dir = 'pushd ' . s:directory_of_file() . ' && pushd $(git rev-parse --show-toplevel) && ' . a:cmd
return substitute(s:cmd_in_dir, "'", '"', 'g')
endfunction

function! s:is_in_a_git_repo()
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 = s:escape('git ls-files --error-unmatch' . s:discard_stdout_and_stderr() . ' ' . shellescape(s:file()))
call system(s:command_in_directory_of_file(cmd))
let cmd = s:escape('git ls-files --error-unmatch' . s:discard_stdout_and_stderr() . ' ' . shellescape(s:file_relative_to_repo_root()))
call system(s:command_in_directory_of_repo_root(cmd))
return !v:shell_error
endfunction

Expand Down Expand Up @@ -208,7 +209,7 @@ endfunction

function! s:run_diff(realtime)
if a:realtime
let blob_name = ':' . fnamemodify(s:file(),':t')
let blob_name = ':' . shellescape(s:file_relative_to_repo_root())
let cmd = 'diff -U0 ' . g:gitgutter_diff_args . ' <(git show '. blob_name .') - '
else
let cmd = 'git diff --no-ext-diff --no-color -U0 ' . g:gitgutter_diff_args . ' ' . shellescape(s:file())
Expand All @@ -219,9 +220,9 @@ function! s:run_diff(realtime)
let cmd = s:escape(cmd)
if a:realtime
let buffer_contents = join(getline(1, '$'), "\n") . "\n"
let diff = system(s:command_in_directory_of_file(cmd), buffer_contents)
let diff = system(s:command_in_directory_of_repo_root(cmd), buffer_contents)
else
let diff = system(s:command_in_directory_of_file(cmd))
let diff = system(s:command_in_directory_of_repo_root(cmd))
endif
return diff
endfunction
Expand Down

0 comments on commit a396dbb

Please sign in to comment.