Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zsh completion for custom git "bang" alias - Git branch name

I have a Git alias update that I would like to outfit with branch-name completion. The alias is defined like so:

[alias]
        update = "!f() { git push . origin/$1:$1; }; f"

(It updates a local tracking branch with its upstream version, without having to check out the branch. Not really important to the specific question, though.)

I would like the command to tab-complete existing branch names for its $1 argument. I know I can define a function called _git-update to control completion, but I'm missing some pieces to get it to work:

_git-update ()
{
  ***some-function-here*** "$(__git_branch_names)"
}

I am using the completions installed on OS X by brew install zsh-completions, which is the set at https://github.com/zsh-users/zsh-completions .

(This question is directly analogous to https://stackoverflow.com/a/41307951/169947, but for Zsh instead of Bash.)

like image 708
Ken Williams Avatar asked Nov 12 '18 21:11

Ken Williams


1 Answers

May be a bit preemptive, but this is working:

# provides completion options similar to git branch/rebase/log
_complete_like_git_branch() {
  __gitcomp_nl_append "FETCH_HEAD"
  __gitcomp_nl_append "HEAD"
  __gitcomp_nl_append "ORIG_HEAD"
  __gitcomp_nl_append "$(__git_heads)"
  __gitcomp_nl_append "$(__git_remote_heads)"
  __gitcomp_nl_append "$(__git_tags)"
  __gitcomp_nl_append "$(__git_complete_refs)"
}

_git_rebase_chain() { _complete_like_git_branch }

# my git "bang" alias of git log
_git_lgk() { _complete_like_git_branch }

reference: contrib/completion/git-completion.bash

Possible improvements:

  • Is the above canonically correct? i.e. Using global shell functions in ~/.zshrc?
  • Choices are super-similar to git rebase and git log, but are they the same?
like image 103
Kache Avatar answered Oct 11 '22 09:10

Kache