From 0327cd4f212e7f647ccde586120a144a269fb3db Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Mon, 17 Aug 2020 14:34:10 +0200 Subject: [PATCH] Add vim GitGoBlame command --- vim/plugin/git.vim | 48 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/vim/plugin/git.vim b/vim/plugin/git.vim index 8868463..343a7ac 100644 --- a/vim/plugin/git.vim +++ b/vim/plugin/git.vim @@ -39,6 +39,13 @@ function! s:cd_git_root(path) end endf +function! s:init_file() + if !exists("b:git_original_file") + let l:git_original_file = substitute(expand("%"), "\\\\", "/", "g") + let l:git_revision_hash = system("git") + end +endfun + function! s:previous_commit() " TODO: Refactor this block into s:git_init_buffer() and set buffer " variables instead. @@ -113,6 +120,19 @@ function! s:git_prev() end endfun +function! s:go_blame() + if exists("b:blame") + let l:next = b:blame[min([getcurpos()[1], len(b:blame)])-1]["commit"] + if exists("b:git_revision_hash") && l:next == b:git_revision_hash + echom "No older versions available! 😱" + else + call s:file_at_revision(l:next) + end + else + throw "Not a git buffer!" + end +endfun + function! s:file_at_revision(rev) let l:pos = getpos(".") if exists("b:git_original_file") " Is this already a file@revision buffer? @@ -138,6 +158,12 @@ function! s:file_at_revision(rev) let b:git_original_file = l:fname let b:git_revision_hash = a:rev + try + let b:blame=s:git_blame("","") + catch + unlet! b:blame + endtry + call setpos('.', l:pos) endfun @@ -156,7 +182,18 @@ endfun let s:split_blame_entry_ref = funcref("s:split_blame_entry") function! s:git_blame(first, last) - let l:input = system('git blame '.expand('%').' --line-porcelain -L '.a:first.','.a:last) + if exists("b:git_revision_hash") + let l:revision = b:git_revision_hash + let l:name = b:git_original_file + else + let l:revision = "HEAD" + let l:name = expand("%") + end + if a:first.a:last == "" + let l:input = system('git blame '.l:revision.' --line-porcelain -- '.l:name) + else + let l:input = system('git blame '.l:revision.' --line-porcelain -L '.a:first.','.a:last.' -- '.l:name) + end if v:shell_error throw v:shell_error else @@ -194,15 +231,12 @@ au BufWritePost * try | let b:blame=git_blame("","") | catch | unlet! b:bla command! -range -nargs=? Blame call blame_command(, , ) command! -range DBlame !git blame % -L , -command! GitNext try - \| call gitroot() - \| call git_next() - \| catch - \| echo 'Not a git repo!' - \| endtry +command! GitNext call git_next() \| GitInfo command! GitPrev call git_prev() \| GitInfo +command! GitGoBlame call go_blame() + \| GitInfo command! GitFirst call git_first() | call s:git_info() command! GitLast call git_last() | call s:git_info() command! GitInfo call git_info()