1

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

Braiam
  • 1
  • 11
  • 47
  • 78
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 1
    Have you looked at documentation or tutorials for bash completion? I have no experience either but it doesn't take long to find that the function names are created in line 2615 of your linked file. You should probably start by looking at a simpler completion file than git's. – musiKk Apr 29 '15 at 07:25
  • @musiKk Thanks a lot! Actually I looked for at least 30 minutes and didn't notice that line. – Daniel Kaplan Apr 30 '15 at 04:38

1 Answers1

2

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
pynexj
  • 19,215
  • 5
  • 38
  • 56