From 2ee95686c5944f99b42dd04fec005b30497006de Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Thu, 27 Apr 2023 16:16:54 +0100 Subject: [PATCH] Handle file which does not exist in diff base If g:gitgutter_diff_base has been set and the file being processed does not exist in that branch/commit, ensure that every line is marked as added - this is how git-diff behaves. Fixes #855. --- autoload/gitgutter.vim | 2 ++ autoload/gitgutter/diff.vim | 20 ++++++++++++++++++++ test/test_gitgutter.vim | 15 +++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/autoload/gitgutter.vim b/autoload/gitgutter.vim index 6978979..f77045d 100644 --- a/autoload/gitgutter.vim +++ b/autoload/gitgutter.vim @@ -46,6 +46,8 @@ function! gitgutter#process_buffer(bufnr, force) abort call gitgutter#debug#log('Not tracked: '.gitgutter#utility#file(a:bufnr)) catch /gitgutter assume unchanged/ call gitgutter#debug#log('Assume unchanged: '.gitgutter#utility#file(a:bufnr)) + catch /gitgutter file unknown in base/ + let diff = gitgutter#diff#hunk_header_showing_every_line_added(a:bufnr) catch /gitgutter diff failed/ call gitgutter#debug#log('Diff failed: '.gitgutter#utility#file(a:bufnr)) call gitgutter#hunk#reset(a:bufnr) diff --git a/autoload/gitgutter/diff.vim b/autoload/gitgutter/diff.vim index 794cd14..eb41312 100644 --- a/autoload/gitgutter/diff.vim +++ b/autoload/gitgutter/diff.vim @@ -81,6 +81,20 @@ function! gitgutter#diff#run_diff(bufnr, from, preserve_full_diff) abort throw 'gitgutter assume unchanged' endif + " If we are diffing against a specific branch/commit, handle the case + " where a file exists on the current branch but not in/at the diff base. + " We have to handle it here because the approach below (using git-show) + " doesn't work for this case. + if !empty(g:gitgutter_diff_base) + let index_name = gitgutter#utility#get_diff_base(a:bufnr).':'.gitgutter#utility#repo_path(a:bufnr, 1) + let cmd = g:gitgutter_git_executable.' '.g:gitgutter_git_args.' --no-pager show '.index_name + let cmd = gitgutter#utility#cd_cmd(a:bufnr, cmd) + call gitgutter#utility#system(cmd) + if v:shell_error + throw 'gitgutter file unknown in base' + endif + endif + " Wrap compound commands in parentheses to make Windows happy. " bash doesn't mind the parentheses. let cmd = '(' @@ -376,6 +390,12 @@ function! gitgutter#diff#hunk_diff(bufnr, full_diff, ...) endfunction +function! gitgutter#diff#hunk_header_showing_every_line_added(bufnr) + let buf_line_count = getbufinfo(a:bufnr)[0].linecount + return '@@ -0,0 +1,'.buf_line_count.' @@' +endfunction + + function! s:write_buffer(bufnr, file) let bufcontents = getbufline(a:bufnr, 1, '$') diff --git a/test/test_gitgutter.vim b/test/test_gitgutter.vim index df527b8..a3d5a0d 100644 --- a/test/test_gitgutter.vim +++ b/test/test_gitgutter.vim @@ -348,6 +348,21 @@ function Test_untracked_file_square_brackets_within_repo() endfunction +function Test_file_unknown_in_base() + let starting_branch = system('git branch --show-current') + let starting_branch = 'main' + call system('git checkout -b some-feature') + let tmp = 'file-on-this-branch-only.tmp' + call system('echo "hi" > '.tmp.' && git add '.tmp) + execute 'edit '.tmp + let g:gitgutter_diff_base = starting_branch + GitGutter + let expected = [{'lnum': 1, 'name': 'GitGutterLineAdded', 'group': 'gitgutter', 'priority': 10}] + call s:assert_signs(expected, tmp) + let g:gitgutter_diff_base = '' +endfunction + + function Test_hunk_outside_noop() 5 GitGutterStageHunk