我正在尝试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
问题是这破坏了 git 自动完成功能中定义的git-自动完成.bash
如何使该功能与 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_