简体   繁体   English

如何使git别名的bash完成工作?

[英]How to make bash completion work for git alias?

I create git alias ~/.gitconfig : 我创建了git别名~/.gitconfig

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

But when I type git xxx TAB TAB in shell I get list of files in current directory instead of list of branches available as it for git rebase TAB TAB 但是,当我在外壳中键入git xxx TAB TAB时 ,会得到当前目录中的文件列表,而不是可用的分支列表,因为它可用于git rebase TAB TAB

Is there a way to make bash auto completion for git xxx ? 有没有办法使git xxx bash自动完成? like for usual alias: 像通常的别名一样:

__git_complete grb _git_rebase

~/.bashrc 在〜/ .bashrc

_git_xxx ()
{
  _git_rebase
}

~/.gitconfig 〜/的.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-completion.bash中找到-该文件的路径取决于您的系统,请使用locatefind或其他方法来查找它):

_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. 复制此函数,例如,重命名为_git_whatever ,根据_git_whatever进行更改,然后将其放入~/.bashrc然后使用__git_complete gwhat _git_whatever为其创建别名。

Completion will work out of the box on git whatever<TAB><TAB> withouth the __git_complete ... step. git whatever<TAB><TAB>完成都可以在git whatever<TAB><TAB>上完成,而__git_complete ...步骤。

To prevent code duplication, you can bind your alias completion to an existing one: 为了防止代码重复,您可以将别名补全绑定到现有的别名补全:

_git_xxx ()
{
  _git_rebase
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM