5

I am trying to add autocompletion to git commit upon hitting TabTab.

The autocomplete feature I am working on is based on a branch-naming convention. The convention is to append the PivotalTracker Id number to the end of the branch name, so a typical branch would look like foo-bar-baz-1449242.

We can associate the commits with the PivotalTracker card by prepending [#1449242] to the beginning of the commit message. I would like this to be auto-inserted if git commit is typed in and the user hits TabTab.

I have already done this here: https://github.com/tlehman/dotfiles/blob/master/ptid_git_complete

(and for convenience, here is the source code):

  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

The problem is that this breaks the git autocompletion features defined in git-autocompletion.bash

How can I make this function compatible with git-autocompletion.bash?

1 Answer 1

1
+50

You could use __git_complete (defined in git-autocompletion.bash) to install your own function, and have your function fall back to the original function. Possibly like this:

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_
2
  • Thank you! I did a little trial and error to get the right function in the else clause, but that did it!
    – tlehman
    Commented Oct 21, 2012 at 1:15
  • Here is the finished script, I forked the original and added the changes you helped me with: github.com/tlehman/git/compare/ptid-autocomplete-42
    – tlehman
    Commented Oct 21, 2012 at 1:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .