Daniel Kaplan
Daniel Kaplan

Reputation: 67440

How does git-completion call the function related to the command you're typing?

I'm trying to look at the code of git-completion.bash. If you run this file, you can auto complete git command arguments. I want to write a very similar tool, but for another command (ie: not git). I'm trying to figure out how this works so I can copy / modify it. I have a decent understanding:

There are functions like _git_rebase that get called when you type git rebase <something><TAB>. The thing I can't figure out is how does _git_rebase get called? I can't find that function being used anywhere in the code. I think it may have something to do with this function, but I'm not sure.

Can anyone more familiar with bash explain to me what's going on here and how, for example, _git_rebase gets called? For convenience, here's the source code: https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

Upvotes: 1

Views: 99

Answers (1)

pynexj
pynexj

Reputation: 20748

These functions are automatically called by bash depending on what command is current inputted in the command line.

You may have a look at the bash's documentation:

Simple Example:

$ cat compspec.foo
function _foo
{
    local cmd=$1 cur=$2 pre=$3

    if [[ $pre == "$cmd" ]]; then
        COMPREPLY=( $(compgen -W 'hello world' -- "$cur") )
    fi
}

complete -F _foo foo
$ source compspec.foo
$ foo <TAB><TAB>
hello  world
$ foo h<TAB>
$ foo hello

Upvotes: 2

Related Questions