Skip to content

Commit

Permalink
Revert to non-binary writefile() for writing buffer.
Browse files Browse the repository at this point in the history
The previous commit switched use of writefile() to binary mode so that
we could prevent a newline being added to a completely empty buffer.
Evidently, however, binary mode has side effects (see #567) so this
commit returns to non-binary mode - with a simpler fix for completely
empty files.

Unfortunately this implementation does not work for noeol files - see
the failing test - because writefile() does not take account of
'nofixeol' (unlike :write).  This is suboptimal but acceptable because
noeol files are not be encountered often.

See #567.
  • Loading branch information
airblade committed Dec 15, 2018
1 parent 5c636b1 commit 1d422b9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
12 changes: 4 additions & 8 deletions autoload/gitgutter/diff.vim
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,10 @@ function! s:write_buffer(bufnr, file)
let bufcontents = getbufline(a:bufnr, 1, '$')

if bufcontents == [''] && line2byte(1) == -1
" Special case: empty buffer; do not write an empty line in this case.
" Special case: completely empty buffer.
" 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')))
call add(bufcontents, '')
endif
call writefile([], a:file)
return
endif

if getbufvar(a:bufnr, '&fileformat') ==# 'dos'
Expand All @@ -398,7 +394,7 @@ function! s:write_buffer(bufnr, file)
let bufcontents[0]=''.bufcontents[0]
endif

call writefile(bufcontents, a:file, 'b')
call writefile(bufcontents, a:file)
endfunction


Expand Down
2 changes: 1 addition & 1 deletion test/test_gitgutter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ function Test_empty_file()

" 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')
call system('echo -n a > oneline.txt && git add oneline.txt')
set noeol nofixeol
edit! oneline.txt

Expand Down

4 comments on commit 1d422b9

@blueyed
Copy link
Contributor

@blueyed blueyed commented on 1d422b9 Jan 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@airblade
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blueyed Thanks! I looked at that when I wrote my binary-writefile code. I think my version did the same as neomake's while being a little simpler. I could be wrong though ;)

I reverted it because of a mysterious, still-unidentified side-effect it had (see #567). I would love to find and fix the problem there but I can't reproduce the problem myself.

@blueyed
Copy link
Contributor

@blueyed blueyed commented on 1d422b9 Jan 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@airblade
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had tests too but I still can't reproduce the problem from #567....

Please sign in to comment.