Set diff base when viewing file versions with Fugitive

When you look at previous versions of a file, e.g. with Fugitive's
:0Gclog, gitgutter will set the diff base to the current version's
parent so that diff markers appear as expected.

Thannks to @rcreasi for the idea and initial implementation.
This commit is contained in:
Andy Stewart
2020-03-12 10:38:58 +00:00
parent c337eef1b7
commit 0e509fb0ac
4 changed files with 19 additions and 0 deletions

View File

@@ -399,6 +399,8 @@ By default buffers are diffed against the index. However you can diff against a
let g:gitgutter_diff_base = '<commit SHA>' let g:gitgutter_diff_base = '<commit SHA>'
``` ```
If you are looking at a previous version of a file with Fugitive (e.g. via `:0Gclog`), gitgutter sets the diff base to the parent of the current revision.
This setting is ignored when the diffs are relative to the working tree. This setting is ignored when the diffs are relative to the working tree.

View File

@@ -33,6 +33,8 @@ function! gitgutter#process_buffer(bufnr, force) abort
return return
endif endif
call gitgutter#utility#set_diff_base_if_fugitive(a:bufnr)
if a:force || s:has_fresh_changes(a:bufnr) if a:force || s:has_fresh_changes(a:bufnr)
let diff = 'NOT SET' let diff = 'NOT SET'

View File

@@ -190,8 +190,20 @@ function! s:restore_shell() abort
endif endif
endfunction endfunction
function! gitgutter#utility#set_diff_base_if_fugitive(bufnr)
let p = resolve(expand('#'.a:bufnr.':p'))
let ml = matchlist(p, '\v^fugitive:/.*/(\x{40,})/')
if !empty(ml) && !empty(ml[1])
let g:gitgutter_diff_base = ml[1].'^'
endif
endfunction
function! s:abs_path(bufnr, shellesc) function! s:abs_path(bufnr, shellesc)
let p = resolve(expand('#'.a:bufnr.':p')) let p = resolve(expand('#'.a:bufnr.':p'))
" Remove extra parts from fugitive's filepaths
let p = substitute(substitute(p, '^fugitive:', '', ''), '\v\.git/\x{40,}/', '', '')
return a:shellesc ? gitgutter#utility#shellescape(p) : p return a:shellesc ? gitgutter#utility#shellescape(p) : p
endfunction endfunction

View File

@@ -366,6 +366,9 @@ a revision instead. For example:
let g:gitgutter_diff_base = '<some commit SHA>' let g:gitgutter_diff_base = '<some commit SHA>'
< <
If you are looking at a previous version of a file with Fugitive (e.g.
via :0Gclog), gitgutter sets the diff base to the parent of the current revision.
This setting is ignore when the diff is relative to the working tree This setting is ignore when the diff is relative to the working tree
(|g:gitgutter_diff_relative_to|). (|g:gitgutter_diff_relative_to|).