nvim: fugitive/git: Add git helper to push upstream

Now that we set push.default to nothing in our git configuration, add
a git helper to figure out the upstream for the current branch and push
it.
This commit is contained in:
Sanchayan Maity 2021-09-23 14:31:01 +05:30
parent 2fb77b64bc
commit 350a6edd2c
3 changed files with 41 additions and 4 deletions

View file

@ -5,13 +5,14 @@ nnoremap <buffer> <silent>q :bwipeout!<CR>
nnoremap <buffer> <Leader>gb :call git#git_branch_checkout()<CR>
nnoremap <buffer> <Leader>gB :call git#git_branch_delete()<CR>
nnoremap <buffer> <Leader>gc :call git#git_cherry_pick()<CR>
xnoremap <buffer> <Leader>gC :<C-U>call git#git_cherry_pick_range()<CR>
nnoremap <buffer> <Leader>gd :call git#git_diffview_commit()<CR>
nnoremap <buffer> <Leader>gD :call git#git_diff_commit()<CR>
nnoremap <buffer> <Leader>gl :call git#git_branch_log()<CR>
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>
nnoremap <buffer> <expr> <Leader>gn git#git_branch_rename()
nnoremap <buffer> <expr> <Leader>gp git#git_push_commit()

View file

@ -19,8 +19,6 @@ xnoremap <Leader>gL :call git#git_log_named_block()<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>gp :Git push<CR>
nnoremap <Leader>gP :Git push -u<SPACE>
nnoremap <Leader>gr :call git#git_rebase_origin()<CR>
nnoremap <Leader>gR :Git rebase --abort<CR>
nnoremap <Leader>g- :Git stash<CR>:e<CR>
@ -31,6 +29,9 @@ nnoremap <Leader>gt :Git reflog<CR>
nnoremap <Leader>gw :Gwrite<CR>
nnoremap <Leader>G :Git<SPACE>
nnoremap <Leader>gp :call git#git_push_to_upstream()<CR>
nnoremap <expr> <Leader>gP git#git_push()
augroup custom_fugitive
autocmd!
autocmd FileType fugitive nnoremap <buffer><silent> q :bwipeout!<CR>

View file

@ -171,6 +171,41 @@ function! git#git_merge_origin() abort
endif
endfunction
function! git#git_push_to_upstream() abort
let current = trim(system("git branch --show-current"))
let upstream = split(trim(system('git rev-parse --abbrev-ref ' . current . '@{upstream}')), '/')
if len(upstream) != 2
echom "Upstream not set for branch " . current
else
execute "Git push -u " . upstream[0] . " " . upstream[1]
endif
endfunction
function! git#git_push() abort
let current = trim(system("git branch --show-current"))
let upstream = split(trim(system('git rev-parse --abbrev-ref ' . current . '@{upstream}')), '/')
if len(upstream) != 2
echom "Upstream not set for branch " . current
return ":Git push -u " . " " . current
else
return ":Git push --force-with-lease -u " . upstream[0] . " " . upstream[1]
endif
endfunction
" To be used after calling Git log and cursor on the commit line.
function! git#git_push_commit() abort
let line = trim(getline('.'))
let commit = split(line, " ")[1]
let current = trim(system("git branch --show-current"))
let upstream = split(trim(system('git rev-parse --abbrev-ref ' . current . '@{upstream}')), '/')
if len(upstream) != 2
echom "Upstream not set for branch " . current
return ":Git push -u " . " " . current
else
return ":Git push -u " . upstream[0] . " " . commit . ":" . upstream[1]
endif
endfunction
" Rebase the current checked out branch to the branch on the current line.
" Git branch -a should be run before this.
function! git#git_rebase_branch() abort