Provide :GBrowse behavior in blame buffers

If a line number is given, browse to the commit on that line.
Otherwise, browse to the file, same as if called from the original
buffer.

It would probably make more sense to open the corresponding blame page
on providers that support it, but that will have to wait on an API
change.

References https://github.com/tpope/vim-fugitive/issues/1214
This commit is contained in:
Tim Pope
2021-04-06 23:38:18 -04:00
parent 3a319cd5b8
commit 8ede0aaf57

View File

@@ -5576,7 +5576,10 @@ endfunction
function! s:BlameCommitFileLnum(...) abort function! s:BlameCommitFileLnum(...) abort
let line = a:0 ? a:1 : getline('.') let line = a:0 ? a:1 : getline('.')
let state = a:0 ? a:2 : s:TempState() let state = a:0 > 1 ? a:2 : s:TempState()
if get(state, 'filetype', '') !=# 'fugitiveblame'
return ['', '', 0]
endif
let commit = matchstr(line, '^\^\=[?*]*\zs\x\+') let commit = matchstr(line, '^\^\=[?*]*\zs\x\+')
if commit =~# '^0\+$' if commit =~# '^0\+$'
let commit = '' let commit = ''
@@ -6126,9 +6129,6 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
else else
let rev = arg let rev = arg
endif endif
if rev ==# ''
let rev = s:DirRev(@%)[1]
endif
if rev =~? '^\a\a\+:[\/][\/]' && rev !~? '^fugitive:' if rev =~? '^\a\a\+:[\/][\/]' && rev !~? '^fugitive:'
let rev = substitute(rev, '\\\@<![#!]\|\\\@<!%\ze\w', '\\&', 'g') let rev = substitute(rev, '\\\@<![#!]\|\\\@<!%\ze\w', '\\&', 'g')
elseif rev ==# ':' elseif rev ==# ':'
@@ -6140,7 +6140,17 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
endif endif
exe s:DirCheck(dir) exe s:DirCheck(dir)
if empty(expanded) if empty(expanded)
let expanded = s:Relative(':(top)', dir) let bufname = s:BufName('%')
let expanded = s:DirRev(bufname)[1]
if empty(expanded)
let expanded = fugitive#Path(bufname, ':(top)', dir)
endif
if a:count > 0 && bufname !=# bufname('')
let blame = s:BlameCommitFileLnum(getline(a:count))
if len(blame[0])
let expanded = blame[0]
endif
endif
endif endif
let cdir = FugitiveVimPath(fugitive#CommonDir(dir)) let cdir = FugitiveVimPath(fugitive#CommonDir(dir))
for subdir in ['tags/', 'heads/', 'remotes/'] for subdir in ['tags/', 'heads/', 'remotes/']