Update GitNext vim command to also load working copy

This happens when already at the latest revision, and makes sense
considering GitPrev now fetches the latest version of a file from the
repository.
This commit is contained in:
Talia 2020-02-07 14:25:38 +01:00
parent 807679eb0b
commit 525dce4c53

56
git.vim
View file

@ -37,27 +37,9 @@ function! s:cd_git_root(path)
end end
endf endf
" Returns an array containing chronologically sorted commits
function! s:git_history()
if exists("b:git_history")
if b:git_history[0]+10 > localtime()
return b:git_history[1]
end
end
if exists("b:git_original_file") " Is this already a file@revision buffer?
let l:fname = b:git_original_file
else
let l:fname = substitute(expand("%"), "\\\\", "/", "g")
end
let l:commits = system('git log --format="%h" '.l:fname)
let l:hist = split(l:commits, "\n")
let b:git_history = [localtime(), l:hist]
return l:hist
endfun
function! s:previous_commit() function! s:previous_commit()
" TODO: Refactor this block into s:git_init_buffer() and set buffer
" variables instead.
if exists("b:git_original_file") " Is this already a file@revision buffer? if exists("b:git_original_file") " Is this already a file@revision buffer?
let l:fname = b:git_original_file let l:fname = b:git_original_file
let l:revision = b:git_revision_hash let l:revision = b:git_revision_hash
@ -71,6 +53,7 @@ function! s:previous_commit()
endfun endfun
function! s:next_commit() function! s:next_commit()
" TODO: See previous_commit()
if exists("b:git_original_file") " Is this already a file@revision buffer? if exists("b:git_original_file") " Is this already a file@revision buffer?
let l:fname = b:git_original_file let l:fname = b:git_original_file
let l:revision = b:git_revision_hash let l:revision = b:git_revision_hash
@ -84,6 +67,8 @@ function! s:next_commit()
endfun endfun
function! s:git_first() function! s:git_first()
" FIXME: Broken after removing git_history;
" see TODO in previous_commit()
if &modified if &modified
throw "File has unsaved modifications!" throw "File has unsaved modifications!"
end end
@ -91,6 +76,8 @@ function! s:git_first()
endfun endfun
function! s:git_last() function! s:git_last()
" FIXME: Broken after removing git_history;
" see TODO in previous_commit()
if &modified if &modified
throw "File has unsaved modifications!" throw "File has unsaved modifications!"
end end
@ -108,7 +95,8 @@ endfun
function! s:git_next() function! s:git_next()
let l:next = s:next_commit() let l:next = s:next_commit()
if l:next == "" if l:next == ""
echom "No newer versions available! 😱" exec 'e! '.b:git_original_file
echom "No newer versions available! (Loading working copy) 😱"
else else
call s:file_at_revision(l:next) call s:file_at_revision(l:next)
end end
@ -151,27 +139,6 @@ function! s:file_at_revision(rev)
call setpos('.', l:pos) call setpos('.', l:pos)
endfun endfun
function! s:git_diff(...)
if a:0
let l:wd = getcwd()
call s:cd_git_root('%')
split
call s:file_at_revision(a:1)
diffthis
au BufUnload <buffer> diffoff!
exec "normal \<C-w>\<C-p>"
diffthis
exec "normal \<C-w>\<C-p>"
exec "cd ".l:wd
else
if exists("b:git_revision_hash")
call s:git_diff(get(s:git_history(), index(s:git_history(), b:git_revision_hash)+1, "NIL"))
else
call s:git_diff(get(s:git_history(), 0, "HEAD"))
end
end
endfun
function! s:git_blame(first, last) function! s:git_blame(first, last)
let l:input = system('git blame '.expand('%').' --line-porcelain -L '.a:first.','.a:last) let l:input = system('git blame '.expand('%').' --line-porcelain -L '.a:first.','.a:last)
let l:data = map(split(l:input, '\ze\x\{40} \d\+ \d\+'), {idx, elem -> split(elem, '\n')}) let l:data = map(split(l:input, '\ze\x\{40} \d\+ \d\+'), {idx, elem -> split(elem, '\n')})
@ -198,11 +165,6 @@ command! -nargs=? GitCompare try
\| catch \| catch
\| echo 'Not a git repo!' \| echo 'Not a git repo!'
\| endtry \| endtry
command! Uncommited try
\| call <sid>git_diff()
\| catch
\| echo 'Not a git repo!'
\| endtry
command! GitRoot call <SID>cd_git_root('%') command! GitRoot call <SID>cd_git_root('%')
command! GitOrig exec 'e '.b:git_original_file command! GitOrig exec 'e '.b:git_original_file
command! ShowGitRoot try command! ShowGitRoot try