Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted
+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_
share|improve this answer
    
Thank you! I did a little trial and error to get the right function in the else clause, but that did it! –  Tobi Lehman Oct 21 '12 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 –  Tobi Lehman Oct 21 '12 at 1:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.