Bash

git 的自定義 bash 自動完成功能會破壞其他 git 自動完成功能

  • October 21, 2012

我正在嘗試git commit在點擊時添加自動完成功能Tab``Tab

我正在研究的自動完成功能基於分支命名約定。慣例是將 PivotalTracker Id 編號附加到分支名稱的末尾,因此典型的分支看起來像foo-bar-baz-1449242.

我們可以通過將送出與 PivotalTracker 卡關聯到[#1449242]送出消息的開頭。git commit如果輸入並且使用者點擊,我希望它自動插入Tab``Tab

我已經在這裡完成了: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-autocompletion.bash中定義的 git 自動完成功能

如何使此功能與 git-autocompletion.bash 兼容?

您可以使用__git_complete(defined in 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_

引用自:https://unix.stackexchange.com/questions/52134