diff --git a/nvim/.config/nvim/after/ftplugin/git.vim b/nvim/.config/nvim/after/ftplugin/git.vim index 29aa363..1fdd70f 100644 --- a/nvim/.config/nvim/after/ftplugin/git.vim +++ b/nvim/.config/nvim/after/ftplugin/git.vim @@ -13,3 +13,4 @@ nnoremap gl :call git#git_branch_log() nnoremap gL :call git#git_branch_log_pretty() nnoremap gp :call git#git_cherry_pick() xnoremap gP :call git#git_cherry_pick_range() +nnoremap gr :call git#git_rebase_branch() diff --git a/nvim/.config/nvim/after/plugin/fugitive.vim b/nvim/.config/nvim/after/plugin/fugitive.vim index 5306176..af4c8bb 100644 --- a/nvim/.config/nvim/after/plugin/fugitive.vim +++ b/nvim/.config/nvim/after/plugin/fugitive.vim @@ -10,17 +10,17 @@ nnoremap gD :Gvdiffsplit! nnoremap ge :sp:Gedit HEAD~:% nnoremap gE :sp:Gedit :%:p nnoremap gf :Git fetch --all -nnoremap gF :Git fetch origin +nnoremap gF :call git#git_fetch_origin_merge() nnoremap gl :Git log --stat % nnoremap gL :Git log --stat -n 100 xnoremap gl :call git#git_log_range() xnoremap gL :call git#git_log_named_block() -nnoremap gM :Git merge origin/ +nnoremap gM :call git#git_merge_origin() nnoremap go :call git#git_log_compare() nnoremap gp :Git push nnoremap gP :Git push -u -nnoremap gr :Git rebase origin/master -nnoremap gR :Git rebase origin/main +nnoremap gr :call git#git_rebase_origin() +nnoremap gR :Git rebase --abort nnoremap g- :Git stash:e nnoremap g+ :Git stash pop:e nnoremap gs :Git diff --git a/nvim/.config/nvim/autoload/git.vim b/nvim/.config/nvim/autoload/git.vim index 6c7f323..f23e98c 100644 --- a/nvim/.config/nvim/autoload/git.vim +++ b/nvim/.config/nvim/autoload/git.vim @@ -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