git-open icon indicating copy to clipboard operation
git-open copied to clipboard

Implement branch autocomplete

Open gerep opened this issue 3 years ago • 3 comments

I was trying to find a way of adding an autocomplete script to a subcommand but I couldn't find a way.

Reading the git-completion.bash I noticed that it has a pattern for the function names. For instance _git_stash().

So it came to me and I created the function _git_open() and it works perfectly!

This is a temporary proposal: (this is inside my .profile)

function _git_open()
{
	local word=${COMP_WORDS[COMP_CWORD]}
	COMPREPLY=($(compgen -W "$(git branch -r)" -- "${word}"))

}

complete -F _git_open git-open

The only downside so far is that it lists only remote branches.

When testing local branches with COMPREPLY=($(compgen -W "$(git branch | sed)" -- "${word}")), for some reason, it was also listing the files and directories.

gerep avatar Apr 07 '21 17:04 gerep

A working example =)

function _git_open()
{
	local word=${COMP_WORDS[COMP_CWORD]}
	local local_branches=$(__git_heads)
	local remote_branches=$(git branch -r | sed 's/[->]//g')

	COMPREPLY=($(compgen -W "$local_branches $remote_branches" -- "${word}"))

}

complete -F _git_open git-open

gerep avatar May 17 '21 02:05 gerep

@eelucaslima this script will work if using bash but no sh.

It also depends on git autocomplete script, for both shells.

gerep avatar May 17 '21 13:05 gerep

@gerep, shouldn't the script check the user's shell?

eelucaslima avatar May 18 '21 13:05 eelucaslima