nvim: fugitive: Execute commands while keeping current alt file

Keep current alternate file where possible for git commands executed
via fugitive.
This commit is contained in:
Sanchayan Maity 2022-05-14 19:56:49 +05:30
parent 69b3327598
commit d057fa0385
2 changed files with 34 additions and 34 deletions

View file

@ -1,8 +1,8 @@
nnoremap <Leader>ga :Git add %<CR>
nnoremap <Leader>gA :Git reset HEAD %<CR>
xnoremap <Leader>gb :Git blame<CR>
nnoremap <Leader>gb :Git branch -a<CR>
nnoremap <Leader>gB :Git branch<SPACE>
nnoremap <Leader>gb :keepalt Git branch -a<CR>
nnoremap <Leader>gB :keepalt Git branch<SPACE>
nnoremap <Leader>gc :Git checkout -b<SPACE>
nnoremap <Leader>gC :Git checkout<SPACE>
nnoremap <Leader>gd :Gtabedit<Bar>Git diff %<Bar>only<CR>
@ -12,27 +12,27 @@ nnoremap <Leader>gE :sp<CR>:Gedit :%:p<Left><Left><Left><Left>
nnoremap <Leader>gf :Git fetch --all --tags -f<CR>
nnoremap <Leader>gF :call git#git_fetch_origin_merge()<CR>
nnoremap <Leader>gg :Git<CR>
nnoremap <Leader>gh :Git log --grep=
nnoremap <Leader>gH :<C-U>Git log -G<C-r><C-w>
nnoremap <Leader>gh :keepalt Git log --grep=
nnoremap <Leader>gH :<C-U>keepalt Git log -G<C-r><C-w>
nnoremap <Leader>gk :call git#git_review()<CR>
nnoremap <Leader>gK :call git#git_review_fileview()<CR>
nnoremap <Leader>gl :Git log --stat %<CR>
nnoremap <Leader>gL :Git log --stat -n 100<CR>
nnoremap <Leader>gl :keepalt Git log --stat %<CR>
nnoremap <Leader>gL :keepalt Git log --stat -n 100<CR>
xnoremap <Leader>gl :<C-U>call git#git_log_range()<CR>
xnoremap <Leader>gL :call git#git_log_named_block()<CR>
nnoremap <Leader>gm :0,3Git blame<CR><C-w>j<CR>
nnoremap <Leader>gM :call git#git_merge_origin()<CR>
nnoremap <Leader>gn :Git branch -m<SPACE>
nnoremap <Leader>go :call git#git_log_compare()<CR>
nnoremap <Leader>gr :Ggrep! -q<SPACE>
nnoremap <Leader>gr :keepalt Ggrep! -q<SPACE>
nnoremap <Leader>gR :call git#git_rebase_origin()<CR>
nnoremap <Leader>g- :call git#git_stash()<CR>:e<CR>
nnoremap <Leader>g+ :Git stash pop stash@
nnoremap <Leader>gs :Git stash list<CR>
nnoremap <Leader>gS :Git stash -- %<CR>
nnoremap <Leader>gt :Git reflog<CR>
nnoremap <Leader>gt :keepalt Git reflog<CR>
nnoremap <Leader>gw :Gwrite<CR>
nnoremap <Leader>G :Git<SPACE>
nnoremap <Leader>G :keepalt Git<SPACE>
nnoremap <Leader>gp :call git#git_push_to_upstream()<CR>
nnoremap <expr> <Leader>gP git#git_push()

View file

@ -9,15 +9,15 @@ function! git#git_branch_delete() abort
let branch = split(line, "/")
if len(branch) == 1
" Handles the case for local branch
execute "Git branch -D " . branch[0]
execute "keepalt Git branch -D " . branch[0]
else
" Handles the case for remote remotes/remote_name/branch_name
let remote = branch[1]
let branch = branch[2]
execute "Git push -d " . remote . " " . branch
execute "Git branch -D " . branch
execute "keepalt Git push -d " . remote . " " . branch
execute "keepalt Git branch -D " . branch
endif
silent execute "bw"
silent execute "keepalt bw"
endfunction
" Checkout the branch on the current line in the output of Git branch -a.
@ -26,14 +26,14 @@ function! git#git_branch_checkout() abort
let branch = split(line, "/")
if len(branch) == 1
" Handles the case for local branch
silent execute "Git checkout " . branch[0]
silent execute "keepalt Git checkout " . branch[0]
else
" Handles the case for remote remotes/remote_name/branch_name
let remote = join(branch[1:2], "/")
silent execute "Git checkout " . remote
silent execute "Git checkout -b " . branch[2]
silent execute "keepalt Git checkout " . remote
silent execute "keepalt Git checkout -b " . branch[2]
endif
silent execute "bw"
silent execute "keepalt bw"
endfunction
" Populate the command line with git command to rename branch on current line.
@ -51,11 +51,11 @@ function! git#git_branch_log() abort
silent execute "bw"
if len(branch) == 1
" Handles the case for local branch
execute "Git log " . branch[0]
execute "keepalt Git log " . branch[0]
else
" Handles the case for remote remotes/remote_name/branch_name
let remote = join(branch[1:2], "/")
execute "Git log " . remote
execute "keepalt Git log " . remote
endif
endfunction
@ -69,11 +69,11 @@ function! git#git_branch_log_pretty() abort
silent execute "bw"
if len(branch) == 1
" Handles the case for local branch
execute "Git log --pretty=oneline --no-merges " . branch[0]
execute "keepalt Git log --pretty=oneline --no-merges " . branch[0]
else
" Handles the case for remote remotes/remote_name/branch_name
let remote = join(branch[1:2], "/")
execute "Git log --pretty=oneline --no-merges " . remote
execute "keepalt Git log --pretty=oneline --no-merges " . remote
endif
endfunction
@ -83,7 +83,7 @@ function! git#git_cherry_pick() abort
" A line in Git log is of the form commit <SHA>
let line = trim(getline('.'))
let commit = split(line, " ")
silent execute "Git cherry-pick " . commit[1]
silent execute "keepalt Git cherry-pick " . commit[1]
endfunction
" Git cherry-pick a range of commits. To be run on output of Git log
@ -97,8 +97,8 @@ function! git#git_cherry_pick_range() abort
endif
let oldCommit = split(lines[0], " ")[:0][0]
let newCommit = split(lines[-1], " ")[:0][0]
execute "Git cherry-pick " . oldCommit . "^.." . newCommit
silent execute "bw"
execute "keepalt Git cherry-pick " . oldCommit . "^.." . newCommit
silent execute "keepalt bw"
endfunction
" Run Git diff on the commit in the line which we are at. To be used after
@ -124,7 +124,7 @@ function! git#git_format_patch() abort
" A line in Git log is of the form commit <SHA>
let line = trim(getline('.'))
let commit = split(line, " ")
execute "Git format-patch " . commit[1] . "~1"
execute "keepalt Git format-patch " . commit[1] . "~1"
endfunction
" Git format-patch a range of commits. To be run on output of Git log
@ -138,8 +138,8 @@ function! git#git_format_patch_range() abort
endif
let oldCommit = split(lines[0], " ")[:0][0]
let newCommit = split(lines[-1], " ")[:0][0]
execute "Git format-patch " . oldCommit . "~1.." . newCommit
silent execute "bw"
execute "keepalt Git format-patch " . oldCommit . "~1.." . newCommit
silent execute "keepalt bw"
endfunction
" The next two functions allow scoping diffs by line range of a file and named
@ -152,14 +152,14 @@ endfunction
function! git#git_log_range() abort
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
silent execute "Git log --no-patch -L " . lnum1 . "," . lnum2 . ":%"
silent execute "keepalt Git log --no-patch -L " . lnum1 . "," . lnum2 . ":%"
endfunction
" Taken from https://learnvimscriptthehardway.stevelosh.com/chapters/33.html.
" Modified and stripped to do what I needed.
function! git#git_log_named_block() abort
normal! `<v`>y
silent execute "Git log --no-patch -L :" . trim(shellescape(@@)) . ":%"
silent execute "keepalt Git log --no-patch -L :" . trim(shellescape(@@)) . ":%"
endfunction
" Compares current branch against master. For eg. git log master..current.
@ -171,7 +171,7 @@ function! git#git_log_compare() abort
if (are_we_on_default == 0)
echom "We are already on default branch. Nothing to compare."
else
silent execute "Git log " . default[3] . ".." . current
silent execute "keepalt Git log " . default[3] . ".." . current
endif
endfunction
@ -236,14 +236,14 @@ function! git#git_rebase_branch() abort
let branch = split(line, "/")
if len(branch) == 1
" Handles the case for local branch
execute "Git rebase " . branch[0]
execute "keepalt Git rebase " . branch[0]
else
" Handles the case for remote remotes/remote_name/branch_name
let remote = branch[1]
let branch = branch[2]
execute "Git rebase " . remote . "/" . branch
execute "keepalt Git rebase " . remote . "/" . branch
endif
silent execute "bw"
silent execute "keepalt bw"
endfunction
" Git reflog should be run first before invoking this
@ -261,7 +261,7 @@ function! git#git_reflog_restore() abort
call inputrestore()
let to_restore = match(restore, 'y')
if (to_restore == 0)
execute "Git reset --hard " . sl2[1]
execute "keepalt Git reset --hard " . sl2[1]
else
echom "Not restoring to " . line
endif