From d282c337897b845b5cf30f837f5b7dc46e483e19 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Fri, 22 Apr 2016 15:04:32 +0100 Subject: [PATCH] Fix new-line signs for untracked repo files with square brackets. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before the plugin tries to diff a file, it checks whether git is tracking the file. If git isn't tracking the file, it stops there and doesn't display any signs. If git is tracking the file, the plugin remembers so next time it can skip the check. When I introduced asynchronous diffing for NeoVim (18b78361), I made a refactoring mistake which caused the plugin on second and subsequent runs [to always think git is tracking a file][1]. The non-realtime diffs – the ones you get when you save a buffer – basically run `git diff FILE`. With an untracked file git returns nothing and exits successfully. So although the plugin erroneously thinks git is tracking the file, it gets an error-free, empty diff back and so removes any and all signs. Which means that the bug doesn't make any difference. However the realtime diffs write the buffer's contents to a temporary file, and write the file as staged in the index to a temporary file, then run `git diff FILE1 FILE2`. To write the staged version of the file we use `git show :FILE > TMPFILE`. When `FILE` isn't known to git, `git show :FILE` exits with an error. Unless, that is, [the filename contains square brackets and you're using git v2.5.0+][2], in which case git exits successfully with empty output. So if you're using git v2.5.0+, and you're editing an untracked file in a repository, and the filename contains square brackets, the plugin will think: git is tracking the file; the realtime diff is successful; the file in the index is empty; so every line in the the working copy must be an addition; hence a `+` sign on every line. [1]: https://github.com/airblade/vim-gitgutter/blob/18b7836168ae9956abebaa6046980b16687dfb7f/autoload/gitgutter/diff.vim#L119-L121 [2]: http://comments.gmane.org/gmane.comp.version-control.git/285686 Closes #325. --- autoload/gitgutter.vim | 2 ++ autoload/gitgutter/diff.vim | 4 ---- test/testUntrackedFileSquareBracketsWithinRepo.vim | 10 ++++++++++ test/testUntrackedFileWithinRepo.vim | 2 ++ test/untrackedFileSquareBracketsWithinRepo.expected | 2 ++ 5 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 test/testUntrackedFileSquareBracketsWithinRepo.vim create mode 100644 test/untrackedFileSquareBracketsWithinRepo.expected diff --git a/autoload/gitgutter.vim b/autoload/gitgutter.vim index 82d8b67..e9dcc15 100644 --- a/autoload/gitgutter.vim +++ b/autoload/gitgutter.vim @@ -56,6 +56,8 @@ endfunction function! gitgutter#handle_diff(diff) + call setbufvar(gitgutter#utility#bufnr(), 'gitgutter_tracked', 1) + call gitgutter#hunk#set_hunks(gitgutter#diff#parse_diff(a:diff)) let modified_lines = gitgutter#diff#process_hunks(gitgutter#hunk#hunks()) diff --git a/autoload/gitgutter/diff.vim b/autoload/gitgutter/diff.vim index c5b875c..ef9af83 100644 --- a/autoload/gitgutter/diff.vim +++ b/autoload/gitgutter/diff.vim @@ -123,10 +123,6 @@ function! gitgutter#diff#run_diff(realtime, preserve_full_diff) endif end - if !tracked - call setbufvar(bufnr, 'gitgutter_tracked', 1) - endif - if g:gitgutter_async && has('nvim') && !a:preserve_full_diff let cmd = gitgutter#utility#command_in_directory_of_file(cmd) " Note that when `cmd` doesn't produce any output, i.e. the diff is empty, diff --git a/test/testUntrackedFileSquareBracketsWithinRepo.vim b/test/testUntrackedFileSquareBracketsWithinRepo.vim new file mode 100644 index 0000000..b1fd48a --- /dev/null +++ b/test/testUntrackedFileSquareBracketsWithinRepo.vim @@ -0,0 +1,10 @@ +source helper.vim + +let tmpfile = '[un]trackedFileWithinRepo.tmp' +call system('touch '.tmpfile) +execute 'edit '.tmpfile +normal ggo* +doautocmd CursorHold +call DumpSigns('untrackedFileSquareBracketsWithinRepo') + +call system('rm '.tmpfile) diff --git a/test/testUntrackedFileWithinRepo.vim b/test/testUntrackedFileWithinRepo.vim index cbca9e7..4fdd48e 100644 --- a/test/testUntrackedFileWithinRepo.vim +++ b/test/testUntrackedFileWithinRepo.vim @@ -3,6 +3,8 @@ source helper.vim let tmpfile = 'untrackedFileWithinRepo.tmp' call system('touch '.tmpfile) execute 'edit '.tmpfile +normal ggo* +doautocmd CursorHold call DumpSigns('untrackedFileWithinRepo') call system('rm '.tmpfile) diff --git a/test/untrackedFileSquareBracketsWithinRepo.expected b/test/untrackedFileSquareBracketsWithinRepo.expected new file mode 100644 index 0000000..aa8cd86 --- /dev/null +++ b/test/untrackedFileSquareBracketsWithinRepo.expected @@ -0,0 +1,2 @@ + +--- Signs ---