How to make bash completion work for git alias?

Eugen Konkov

I create git alias ~/.gitconfig:

[alias]
xxx = !bash -c 'git rebase ...'

But when I type git xxxTABTAB in shell I get list of files in current directory instead of list of branches available as it for git rebaseTABTAB

Is there a way to make bash auto completion for git xxx? like for usual alias:

__git_complete grb _git_rebase
bimlas

~/.bashrc

_git_xxx ()
{
  _git_rebase
}

~/.gitconfig

[alias]
xxx = !bash -c 'git rebase ...'

For example _git_rebase looks like this (found in git-completion.bash - the path of this file depends on your system, use locate, find, whatever to find it):

_git_rebase ()
{
        __git_find_repo_path
        if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
                __gitcomp "--continue --skip --abort --quit --edit-todo"
                return
        elif [ -d "$__git_repo_path"/rebase-apply ] || \
            [ -d "$__git_repo_path"/rebase-merge ]; then
                __gitcomp "--continue --skip --abort --quit"
                return
        fi
        __git_complete_strategy && return
        case "$cur" in
        --whitespace=*)
                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
                return
                ;;
        --*)
                __gitcomp "
                        --onto --merge --strategy --interactive
                        --preserve-merges --stat --no-stat
                        --committer-date-is-author-date --ignore-date
                        --ignore-whitespace --whitespace=
                        --autosquash --no-autosquash
                        --fork-point --no-fork-point
                        --autostash --no-autostash
                        --verify --no-verify
                        --keep-empty --root --force-rebase --no-ff
                        --exec
                        "

                return
        esac
        __git_complete_refs
}

Copy this function, rename to _git_whatever for example, change it as you like and put it in to your ~/.bashrc then use __git_complete gwhat _git_whatever to make an alias from it.

Completion will work out of the box on git whatever<TAB><TAB> withouth the __git_complete ... step.

To prevent code duplication, you can bind your alias completion to an existing one:

_git_xxx ()
{
  _git_rebase
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related