ohmybash / oh-my-bash

A delightful community-driven framework for managing your bash configuration, and an auto-update tool so that makes it easy to keep up with the latest updates from the community.
https://ohmybash.github.io
MIT License
5.55k stars 626 forks source link

Completion for custom Git commands #411

Closed RoggeHaj closed 1 year ago

RoggeHaj commented 1 year ago

Hi,

I have a question on whether it's possible to enable completion for home brew bash functions? Currently we have a regular bash function

function gcb () {
    _branch=${1}
    git checkout ${_branch} || return -1
    git submodule status && git submodule update --init

   # Other stuff goes here...

    return 0

}

Code that hides under "Other stuff" makes it difficult to implement as a regular git alias. What I want to achieve is to have completion similar to what I get from

git checkout <tab>

I'm totally happy with a solution to this specific case, but if there's a more general way to set things up it's even better.

akinomyoga commented 1 year ago

That is explained in the code comments of the upstream git-completion file.

git/git - d15644fe contrib/completion/git-completion.bash:32-36

# If you have a command that is not part of git, but you would still
# like completion, you can use __git_complete:
#
#   __git_complete gl git_log
#

So I guess something like the following would work:

__git_complete gcb git_checkout

Note: The above line needs to be put in .bashrc after source /path/to/oh-my-bash.sh. Also, please do not forget to include git in the completions array.

RoggeHaj commented 1 year ago

Ah, the last paragraph did it! I tried running the __git_complete command from my $OSH/custom folder and that did not work.

Many thanks for the quick response.