Home > Software design >  Retrieve git_main_branch in Neovim
Retrieve git_main_branch in Neovim

Time:11-20

So I have been getting very acquainted with Neovim/Spacevim lately, and it's freakin awesome!

I am in the process of setting up some personal keybindings and commands and what have you, and am currently working on Git stuff.

Coming from using VSCode and iTerm2, when wanting to switch from any branch to master or main or whatever the main branch is, I have an alias as follows:

alias gcom=git checkout $(git_main_branch)

Where I can just type gcom into the terminal, and it will automatically switch to whatever the main branch is. Unfortunately in Neovim, git_main_branch is not a thing, so I am trying to figure out an equivalent, where I can do something like type :Gcom into the Neovim command prompt, and it switches to the main branch.

I tried to set up a function like this in my init.vim file (I have coc and all corresponding Git plugins installed, including fugitive):

function! GitCheckoutMain()
  let gitMainBranch = system('git_main_branch')

  execute "normal! :Git checkout" . gitMainBranch
endfunction

and then setting up a command like

command! Gcom :call GitCheckoutMain()

But that does not do the trick. Does anyone know how I might accomplish this with Neovim? Thanks!

CodePudding user response:

Ok so I think I actually may have figured out how to solve this. I changed my GitCheckoutMain function from my initial post to be as follows:

function GitCheckoutMain()
  let mainGitBranch = system('git branch | grep -o -m1 "\b\(master\|main\)\b"')

  echo mainGitBranch
endfunction

and I bound that to my Gcom command from above, and when I run :Gcom from the Neovim terminal, it prints out master in the case of a repo where master is the main branch. Will also test this in a repo where the main branch is main, but I think this will do what I want...

EDIT: This does in fact work! Here is my full code, for anyone interested.

function GitCheckoutMain()
  let mainGitBranch = system('git branch | grep -o -m1 "\b\(master\|main\)\b"')

  execute "normal! :Git checkout " . mainGitBranch
endfunction

...

command! Gcom :call GitCheckoutMain()

and whichever branch I am on, if I type in :Gcom to the Neovim terminal, it will change branches to master or main or whatever main branch is set up in that particular repository. Woohoo!

I want to credit the answer posted by @dwelle in this thread - How to get the default for the master branch in Git? - for getting me to the end result!

CodePudding user response:

That's a nifty solution! Just wanted to add my own 2 cents about this. If you use GitHub as your repository, and you have the GitHub CLI installed, you could get the main target branch by typing

gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name'

That will open up another mode which will list the name of the main branch. I know you already got to where you needed, but this might be another cleaner method you could implement.

  • Related