Git 自定义 bash 函数的自动完成
Git autocomplete for custom bash functions
在我的 .bash_profile
中,我有很多 git 的功能快捷键。例如:
function gitpull() {
branch=""
if [[ -z $branch ]]; then
current_branch=`git symbolic-ref -q --short HEAD`
git pull origin $current_branch;
elif [[ -n $branch && $branch == "m" ]]; then
git pull origin master;
else
git pull origin $branch;
fi;
}
但是,当我在终端中键入此内容时,我希望它自动完成 git 分支。我该怎么做呢? (我已经在使用 .git-completion.bash
)
手工制作bash完成就这么简单:
# our handler that returns choices by populating Bash array COMPREPLY
# (filtered by the currently entered word () via compgen builtin)
_gitpull_complete() {
branches=$(git branch -l | cut -c3-)
COMPREPLY=($(compgen -W "$branches" -- ""))
}
# we now register our handler to provide completion hints for the "gitpull" command
complete -F _gitpull_complete gitpull
获取上述命令后:
$ gitpull <TAB>
asd master qwe zxc
$ gitpull m<TAB>
$ gitpull master
关于 bash 完成的最终参考(当然)是关于 Programmable Completion in the bash manual, but a nice introduction is given on "Debian Administration" page (part 1 and a more important part 2 的部分)。
推荐的方法是使用__git_complete()
:
__git_complete gitpull _git_pull
在 Git 2.31(2021 年第一季度)中,bash 完成(在 contrib/
中)已更新,使最终用户更容易为他们的自定义添加完成“git
" 子命令。
感谢FelipeC (who wrote a )
参见 commit 5a067ba, commit 0e02bdc, commit 810df0e, commit 7f94b78 (30 Dec 2020) by Felipe Contreras (felipec
)。
(由 Junio C Hamano -- gitster
-- in commit f9fb906 合并,2021 年 1 月 15 日)
completion
: add proper public __git_complete
Signed-off-by: Felipe Contreras
When __git_complete
was introduced, it was meant to be temporarily, while a proper guideline for public shell functions was established (tentatively _GIT_complete
), but since that never happened, people in the wild started to use __git_complete
, even though it was marked as not public.
Eight years is more than enough wait, let's mark this function as public, and make it a bit more user-friendly.
So that instead of doing:
__git_complete gk __gitk_main
The user can do:
__git_complete gk gitk
And instead of:
__git_complete gf _git_fetch
Do:
__git_complete gf git_fetch
Backwards compatibility is maintained.
在我的 .bash_profile
中,我有很多 git 的功能快捷键。例如:
function gitpull() {
branch=""
if [[ -z $branch ]]; then
current_branch=`git symbolic-ref -q --short HEAD`
git pull origin $current_branch;
elif [[ -n $branch && $branch == "m" ]]; then
git pull origin master;
else
git pull origin $branch;
fi;
}
但是,当我在终端中键入此内容时,我希望它自动完成 git 分支。我该怎么做呢? (我已经在使用 .git-completion.bash
)
手工制作bash完成就这么简单:
# our handler that returns choices by populating Bash array COMPREPLY
# (filtered by the currently entered word () via compgen builtin)
_gitpull_complete() {
branches=$(git branch -l | cut -c3-)
COMPREPLY=($(compgen -W "$branches" -- ""))
}
# we now register our handler to provide completion hints for the "gitpull" command
complete -F _gitpull_complete gitpull
获取上述命令后:
$ gitpull <TAB>
asd master qwe zxc
$ gitpull m<TAB>
$ gitpull master
关于 bash 完成的最终参考(当然)是关于 Programmable Completion in the bash manual, but a nice introduction is given on "Debian Administration" page (part 1 and a more important part 2 的部分)。
推荐的方法是使用__git_complete()
:
__git_complete gitpull _git_pull
在 Git 2.31(2021 年第一季度)中,bash 完成(在 contrib/
中)已更新,使最终用户更容易为他们的自定义添加完成“git
" 子命令。
感谢FelipeC (who wrote a
参见 commit 5a067ba, commit 0e02bdc, commit 810df0e, commit 7f94b78 (30 Dec 2020) by Felipe Contreras (felipec
)。
(由 Junio C Hamano -- gitster
-- in commit f9fb906 合并,2021 年 1 月 15 日)
completion
: add proper public __git_completeSigned-off-by: Felipe Contreras
When
__git_complete
was introduced, it was meant to be temporarily, while a proper guideline for public shell functions was established (tentatively_GIT_complete
), but since that never happened, people in the wild started to use__git_complete
, even though it was marked as not public.Eight years is more than enough wait, let's mark this function as public, and make it a bit more user-friendly.
So that instead of doing:
__git_complete gk __gitk_main
The user can do:
__git_complete gk gitk
And instead of:
__git_complete gf _git_fetch
Do:
__git_complete gf git_fetch
Backwards compatibility is maintained.