bash completion for git custom subcommands?

Viewed 212

Say I have a git-cc executable in PATH. If git-cc supports --help, then it is easy to provide adequate completion with

complete -F _longopt git-cc

This makes $ git-cc --<TAB> complete (as per help output). But git cc --<TAB> won't complete (even though it runs fine). More importantly if I create a git alias to the custom subcommand, e.g. cc-sensible-defaults = cc --opt1 ..., that also won't work, and in this case simply deleting the space (git-cc instead of git cc) isn't an option.

What to do? I've tried messing around with __git_complete [git-]cc _longopt, but none of the various combinations do anything good. It seems to be for completing bash aliases (like gl = git-log), not sub-commands. The intro in git/git-completion.bash is, as expected, not very helpful, containing the confusing

# 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
#
# Or if it's a main command (i.e. git or gitk):
#
#   __git_complete gk gitk

(WTH is _git_log? Did they mean _git_log, which is indeed a function? Is it some convention?)

1 Answers

What to do?

Just define a function that does the compiletion with leading _git_ prefix.

# you should rather use COMPREPLY+=(..) or call `__gitcomp` to append
$ _git_cc() { COMPREPLY=(-a -b); }
$ git cc <tab>
-a  -b  
$ git cc -

See __git_complete_command:

__git_complete_command () {
    local command="$1"
    local completion_func="_git_${command//-/_}"
    ...
    if __git_have_func $completion_func
        then
            $completion_func
            return 0

I've tried messing around with __git_complete

As far as I understand the __git_complete it's the other way round - you want a normal command complete like git subcommand. For example:

$ _git_cc() { COMPREPLY=(-a -b); }
$ alias mycommand='git cc'
$ __git_complete mycommand git_cc                  # or __git_complete mycommand _git_cc
$ mycommand <tab>
-a  -b  
$ mycommand -

WTH is _git_log?

_git_log is a function the generates completion for git log.

Did they mean _git_log, which is indeed a function?

Yes. See __git_complete tests for existence of a function with _main suffix or _ prefix or without any prefix/suffix.

Is it some convention?)

Yes.

Related