Use a temporary file for buffer for realtime diffs

`git diff` doesn't perform EOL conversion on stdin, causing it to
mistakenly flag every line as having changed when the working tree uses
a different EOL than the blobs. Writing the buffer to a temporary file
and diffing against that avoids this issue.

Fixes #232.
This commit is contained in:
Eli Young
2015-03-03 14:18:37 -08:00
committed by Andy Stewart
parent 06240f3af3
commit 0cb1e41b23
2 changed files with 15 additions and 4 deletions

View File

@@ -16,12 +16,19 @@ function! gitgutter#diff#run_diff(realtime, use_external_grep, lines_of_context)
if a:realtime
let blob_name = ':'.gitgutter#utility#shellescape(gitgutter#utility#file_relative_to_repo_root())
let blob_file = tempname()
let buff_file = tempname()
let extension = gitgutter#utility#extension()
if !empty(extension)
let blob_file .= '.'.extension
let buff_file .= '.'.extension
endif
let cmd .= 'git show '.blob_name.' > '.blob_file.' && '
execute('silent write '.buff_file)
endif
let cmd .= 'git diff --no-ext-diff --no-color -U'.a:lines_of_context.' '.g:gitgutter_diff_args.' -- '
if a:realtime
let cmd .= blob_file.' - '
let cmd .= blob_file.' '.buff_file
else
let cmd .= gitgutter#utility#shellescape(gitgutter#utility#filename())
endif
@@ -44,11 +51,11 @@ function! gitgutter#diff#run_diff(realtime, use_external_grep, lines_of_context)
let cmd .= ')'
endif
let diff = gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(cmd))
if a:realtime
let diff = gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(cmd), gitgutter#utility#buffer_contents())
call delete(blob_file)
else
let diff = gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(cmd))
call delete(buff_file)
endif
if gitgutter#utility#shell_error()

View File

@@ -47,6 +47,10 @@ function! gitgutter#utility#filename()
return fnamemodify(s:file, ':t')
endfunction
function! gitgutter#utility#extension()
return fnamemodify(s:file, ':e')
endfunction
function! gitgutter#utility#directory_of_file()
return fnamemodify(s:file, ':h')
endfunction