nvim: git: Add functionality to restore using git reflog

This commit is contained in:
Sanchayan Maity 2021-07-07 19:52:31 +05:30
parent a85a76c3eb
commit d3b8cec40a
3 changed files with 23 additions and 0 deletions

View File

@ -12,3 +12,4 @@ nnoremap <buffer> <Leader>gL :call git#git_branch_log_pretty()<CR>
nnoremap <buffer> <Leader>gp :call git#git_cherry_pick()<CR>
xnoremap <buffer> <Leader>gP :<C-U>call git#git_cherry_pick_range()<CR>
nnoremap <buffer> <Leader>gr :call git#git_rebase_branch()<CR>
nnoremap <buffer> <Leader>gR :call git#git_reflog_restore()<CR>

View File

@ -25,6 +25,7 @@ nnoremap <Leader>g- :Git stash<CR>:e<CR>
nnoremap <Leader>g+ :Git stash pop<CR>:e<CR>
nnoremap <Leader>gs :Git<CR>
nnoremap <Leader>gS :Git stash -- %<CR>
nnoremap <Leader>gt :Git reflog<CR>
nnoremap <Leader>gw :Gwrite<CR>
nnoremap <Leader>G :Git<SPACE>

View File

@ -181,6 +181,27 @@ function! git#git_rebase_branch() abort
silent execute "bw"
endfunction
" Git reflog should be run first before invoking this
function! git#git_reflog_restore() abort
let line = trim(getline('.'))
" A line in git reflog is of the form below
" f6a8c7ed4 HEAD@{73}: rebase (pick): gst-rtmp: Add mux queues to stats gathering
let sl1 = split(line, ":")
" sl1[0] will give f6a8c7ed4 HEAD@{73} for above example
let sl2 = split(sl1[0], " ")
" sl2[0] will have f6a8c7ed4 and sl2[1] will have HEAD@{73}
echom "Command executed will be: Git reset --hard " . sl2[1]
call inputsave()
let restore = input('Restore to: ' . line . '. Enter y to proceed: ')
call inputrestore()
let to_restore = match(restore, 'y')
if (to_restore == 0)
execute "Git reset --hard " . sl2[1]
else
echom "Not restoring to " . line
endif
endfunction
" Figures out whether default branch is main or master and runs git rebase.
function! git#git_rebase_origin() abort
let default = split(trim(system('git symbolic-ref refs/remotes/origin/HEAD')), '/')