From 5c636b128ed40f3ed926d18adb307e01dfc082f8 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Thu, 13 Dec 2018 10:50:02 +0000 Subject: [PATCH] Fix handling of buffer consisting only of a newline. See #510. --- autoload/gitgutter/diff.vim | 6 ++++-- test/test_gitgutter.vim | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/autoload/gitgutter/diff.vim b/autoload/gitgutter/diff.vim index 56e63dd..e1bd5a3 100644 --- a/autoload/gitgutter/diff.vim +++ b/autoload/gitgutter/diff.vim @@ -374,8 +374,10 @@ endfunction function! s:write_buffer(bufnr, file) let bufcontents = getbufline(a:bufnr, 1, '$') - " Special case: empty buffer; do not write an empty line in this case. - if len(bufcontents) > 1 || bufcontents != [''] + if bufcontents == [''] && line2byte(1) == -1 + " Special case: empty buffer; do not write an empty line in this case. + " A nearly empty buffer of only a newline has line2byte(1) == 1. + else if getbufvar(a:bufnr, '&endofline') \ || (!getbufvar(a:bufnr, '&binary') \ && (!exists('+fixendofline') || getbufvar(a:bufnr, '&fixendofline'))) diff --git a/test/test_gitgutter.vim b/test/test_gitgutter.vim index 5f40d38..2f0c27c 100644 --- a/test/test_gitgutter.vim +++ b/test/test_gitgutter.vim @@ -648,3 +648,33 @@ function Test_encoding() call assert_equal([], s:signs('cp932.txt')) endfunction + + +function Test_empty_file() + " 0-byte file + call system('touch empty.txt && git add empty.txt') + edit empty.txt + + call s:trigger_gitgutter() + call assert_equal([], s:signs('empty.txt')) + + + " File consisting only of a newline + call system('echo "" > newline.txt && git add newline.txt') + edit newline.txt + + call s:trigger_gitgutter() + call assert_equal([], s:signs('newline.txt')) + + + " 1 line file without newline + " Vim will force a newline unless we tell it not to. + call system('echo -n "a" > oneline.txt && git add oneline.txt') + set noeol nofixeol + edit! oneline.txt + + call s:trigger_gitgutter() + call assert_equal([], s:signs('oneline.txt')) + + set eol fixeol +endfunction