nvim: autoload/ftplugin: git: Add rebase and merge helpers

This commit is contained in:
Sanchayan Maity 2021-06-17 11:14:59 +05:30
parent 108630d19c
commit a16c244fce
3 changed files with 49 additions and 5 deletions

View File

@ -13,3 +13,4 @@ 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>

View File

@ -10,17 +10,17 @@ nnoremap <Leader>gD :Gvdiffsplit!<CR>
nnoremap <Leader>ge :sp<CR>:Gedit HEAD~:%<Left><Left>
nnoremap <Leader>gE :sp<CR>:Gedit :%:p<Left><Left><Left><Left>
nnoremap <Leader>gf :Git fetch --all<CR>
nnoremap <Leader>gF :Git fetch origin<SPACE>
nnoremap <Leader>gF :call git#git_fetch_origin_merge()<CR>
nnoremap <Leader>gl :Git log --stat %<CR>
nnoremap <Leader>gL :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 :Git merge origin/
nnoremap <Leader>gM :call git#git_merge_origin()<CR>
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 :Git rebase origin/master<CR>
nnoremap <Leader>gR :Git rebase origin/main<CR>
nnoremap <Leader>gr :call git#git_rebase_origin()<CR>
nnoremap <Leader>gR :Git rebase --abort<CR>
nnoremap <Leader>g- :Git stash<CR>:e<CR>
nnoremap <Leader>g+ :Git stash pop<CR>:e<CR>
nnoremap <Leader>gs :Git<CR>

View File

@ -136,7 +136,7 @@ endfunction
" This combined with Git difftool -y are helpful for MR reviews.
function! git#git_log_compare() abort
let default = split(trim(system('git symbolic-ref refs/remotes/origin/HEAD')), '/')
let current = trim(system('git rev-parse --abbrev-ref HEAD'))
let current = trim(system('git branch --show-current'))
let are_we_on_default = match(default[3], current)
if (are_we_on_default == 0)
echom "We are already on default branch. Nothing to compare."
@ -144,3 +144,46 @@ function! git#git_log_compare() abort
silent execute "Git log " . default[3] . ".." . current
endif
endfunction
" Merge remote origin:master/main into local:master/main
function! git#git_fetch_origin_merge() abort
let default = split(trim(system('git symbolic-ref refs/remotes/origin/HEAD')), '/')
execute "Git fetch origin " . default[3] . ":" . default[3]
endfunction
" Figures out whether default branch is main or master and runs git merge.
function! git#git_merge_origin() abort
let default = split(trim(system('git symbolic-ref refs/remotes/origin/HEAD')), '/')
let current = trim(system("git branch --show-current"))
let are_we_on_default = match(default[3], current)
if (are_we_on_default == 0)
echom "Merging origin/" . default[3]
execute "Git merge origin/" . default[3]
else
echom "Not on " . default[3]
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
let line = trim(getline('.'))
let branch = split(line, "/")
if len(branch) == 1
" Handles the case for local branch
execute "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
endif
silent execute "bw"
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')), '/')
echom "Rebasing current branch on origin/" . default[3]
execute "Git rebase origin/" . default[3]
endfunction