Redo Blame command for vim

This commit is contained in:
Talia 2019-05-21 08:38:28 +02:00
parent 00b4e918c0
commit c01fecb8b2

64
git.vim
View file

@ -1,10 +1,11 @@
" ┌─────────────────┐ " " vim: set noexpandtab :miv "
" └─┬─┬───┬─┬───┬─┬─┘ " " ┌─────────────────┐ "
" │ │ │ │ │ │ " " └─┬─┬───┬─┬───┬─┬─┘ "
" │ │ │ │ │ │ " " │ │ │ │ │ │ "
" ┌─┴─┴───┴─┴───┴─┴─┐ " " │ │ │ │ │ │ "
" ┌┘ Git Stuff └┐ " " ┌─┴─┴───┴─┴───┴─┴─┐ "
" └───────────────────┘ " " ┌┘ Git Stuff └┐ "
" └───────────────────┘ "
" Find the root of a git repository " Find the root of a git repository
function! s:gitroot() function! s:gitroot()
@ -164,46 +165,31 @@ function! s:git_diff(...)
end end
endfun endfun
function! s:git_blame() function! s:git_blame(first, last)
let l:name = expand('%') let l:input = system('git blame '.expand('%').' --line-porcelain -L '.a:first.','.a:last)
let l:line = getpos('.')[1] let l:data = map(split(l:input, '\ze\x\{40} \d\+ \d\+'), {idx, elem -> split(elem, '\n')})
let l:char = getpos('.')[2]+59 return map(l:data, {idx, ary -> ary[1][match(ary[1], '\s\+\zs'):]})
let l:type = &filetype
enew
set modifiable
let &filetype = l:type
set buftype=nofile
set bufhidden=delete
set nowrap
silent exec "file Blame: ".l:name
keepjumps exec 'r !git blame '.l:name
keepjumps 0,0del "
set nomodifiable
keepjumps call setpos('.', [0, l:line, l:char, 0])
endfun endfun
command! Blame try command! -range Blame echom join(uniq(sort(<sid>git_blame(<line1>, <line2>))), ', ')
\| call s:gitroot() | call <sid>git_blame()
\| catch | echo 'Not a git repo!'
\| endtry
command! GitNext try command! GitNext try
\| call s:gitroot() | call <sid>git_next() | call s:git_info() \| call s:gitroot() | call <sid>git_next() | call s:git_info()
\| catch | echo 'Not a git repo!' \| catch | echo 'Not a git repo!'
\| endtry \| endtry
command! GitPrev call <sid>git_prev() | call s:git_info() command! GitPrev call <sid>git_prev() | call s:git_info()
command! GitFirst call <sid>git_first() | call s:git_info() command! GitFirst call <sid>git_first() | call s:git_info()
command! GitLast call <sid>git_last() | call s:git_info() command! GitLast call <sid>git_last() | call s:git_info()
command! GitInfo call <sid>git_info() command! GitInfo call <sid>git_info()
command! -nargs=1 GitCheckout call <sid>file_at_revision(<f-args>) command! -nargs=1 GitCheckout call <sid>file_at_revision(<f-args>)
command! -nargs=? GitCompare try command! -nargs=? GitCompare try
\| call s:gitroot() | call <sid>git_diff(<f-args>) \| call s:gitroot() | call <sid>git_diff(<f-args>)
\| catch | echo 'Not a git repo!' \| catch | echo 'Not a git repo!'
\| endtry \| endtry
command! Uncommited try command! Uncommited try
\| call s:gitroot() | call <sid>git_diff() \| call s:gitroot() | call <sid>git_diff()
\| catch | echo 'Not a git repo!' \| catch | echo 'Not a git repo!'
\| endtry \| endtry
command! GitRoot try command! GitRoot try
\| echo <sid>gitroot() \| echo <sid>gitroot()
\| catch | echo 'Not a git repository' \| catch | echo 'Not a git repository'
\| endtry \| endtry