tle*_*man 5 bash git autocomplete
我正在尝试git commit在点击时添加自动完成功能TabTab。
我正在开发的自动完成功能基于分支命名约定。约定是将 PivotalTracker Id 编号附加到分支名称的末尾,因此典型的分支看起来像foo-bar-baz-1449242.
我们可以通过添加[#1449242]到提交消息的开头来将提交与 PivotalTracker 卡相关联。如果git commit输入并且用户点击,我希望它自动插入TabTab。
我已经在这里做了这个:https : //github.com/tlehman/dotfiles/blob/master/ptid_git_complete
(为了方便起见,这里是源代码):
function _ptid_git_complete_()
{
local line="${COMP_LINE}" # the entire line that is being completed
# check that the commit option was passed to git
if [[ "$line" == "git commit" ]]; then
# get the PivotalTracker Id from the branch name
ptid=`git branch | grep -e "^\*" | sed 's/^\* //g' | sed 's/\-/ /g' | awk '{ print $(NF) }'`
nodigits=$(echo $ptid | sed 's/[[:digit:]]//g')
if [ ! -z $nodigits ]; then
: # do nothing
else
COMPREPLY=("commit -m \"[#$ptid]")
fi
else
reply=()
fi
}
complete -F _ptid_git_complete_ git
Run Code Online (Sandbox Code Playgroud)
问题是这破坏了git-autocompletion.bash 中定义的 git 自动完成功能
如何使此功能与 git-autocompletion.bash 兼容?
小智 1
您可以使用__git_complete(在 中定义git-autocompletion.bash)来安装您自己的函数,并使您的函数回退到原始函数。可能是这样的:
function _ptid_git_complete_()
{
local line="${COMP_LINE}" # the entire line that is being completed
# check that the commit option was passed to git
if [[ "$line" == "git commit " ]]; then
# get the PivotalTracker Id from the branch name
ptid=`git branch | grep -e "^\*" | sed 's/^\* //g' | sed 's/\-/ /g' | awk '{ print $(NF) }'`
nodigits=$(echo $ptid | sed 's/[[:digit:]]//g')
if [ ! -z $nodigits ]; then
: # do nothing
else
COMPREPLY=("-m \"[#$ptid]")
fi
else
__git_main
fi
}
__git_complete git _ptid_git_complete_
Run Code Online (Sandbox Code Playgroud)