Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab completion freezes for Git commands only

I have some strange behavior regarding my setup that I can't seem to narrow down.

I am using tab completion in my shell without any issues (my shell is zsh). The issue I'm having is regarding tab completion after issuing a git command.

Example 1 (works fine):

I make a new directory, change into it and git init. I then touch hello.rb. If I do git add <tab> it will change it to git add hello.rb.

Example 2 (doesn't work):

I'm in a rails app that really isn't very big, and if I try to run git add G<tab> with the intent that it will pull up my Gemfile, it just hangs and hangs until I kill it with ctrl-c which outputs:

Killed by signal in __git_complete_index_file after 159s

In zsh I'm using:

# completion
autoload -U compinit
compinit

Has anyone else had this issue? I can work around it but I have to be doing something wrong and I'm unsure where else to look.

Versions of things:

git version 2.1.2
zsh 5.0.7
iTerm2 Build 2.0.0.20141103

Update:

Git v 2.2.0 has fixed this issue so just upgrade if you're running into this issue.

like image 497
Anthony Avatar asked Nov 04 '14 16:11

Anthony


People also ask

Does bash have tab completion?

Bash completion is a functionality through which Bash helps users type their commands more quickly and easily. It does this by presenting possible options when users press the Tab key while typing a command.

Why is git auto completion useful?

It can really save you a lot of typing. If there is more than one possibility, then it will let you know that it needs more information and you can type a little bit more of the word and try again. Git auto-completion is a separate script, and it's included inside the Git source code repository on GitHub.

How do you completion a tab in Linux?

Command-line completion allows the user to type the first few characters of a command, program, or filename, and press a completion key (normally Tab ↹ ) to fill in the rest of the item. The user then presses Return or ↵ Enter to run the command or open the file.


1 Answers

I'm assuming you are using RVM or some tool like that.

There is a bug in the git-completion.bash shipped with the current git (2.1.3) and older versions, causing an endless loop when listing file completions in directories where RVM is used.

The reason for this endless loop is a change of chpwd_functions, made by RVM and some other tools.

I've found a patch for the git-comletion.bash affecting only the __git_ls_files_helper method which is used for listing files. The patch ignores the chpwd_functions and hence, these endless loops are omitted.

In short: The __git_ls_files_helper function needs to be changed from:

__git_ls_files_helper ()
{
  (
    test -n "${CDPATH+set}" && unset CDPATH
    cd "$1"
    if [ "$2" == "--committable" ]; then
      git diff-index --name-only --relative HEAD
    else
      # NOTE: $2 is not quoted in order to support multiple options
      git ls-files --exclude-standard $2
    fi
   ) 2>/dev/null
}

to:

__git_ls_files_helper ()
{
  (
    test -n "${CDPATH+set}" && unset CDPATH
    (( ${+functions[chpwd]} )) && unfunction chpwd
    (( ${#chpwd_functions} )) && chpwd_functions=()
    setopt chaselinks
    builtin cd "$1" 2>/dev/null
    if [ "$2" == "--committable" ]; then
      git diff-index --name-only --relative HEAD
    else
      # NOTE: $2 is not quoted in order to support multiple options
      git ls-files --exclude-standard $2
    fi
  ) 2>/dev/null
}

Further information can be found in the RVM issue discussion on Github. The location of your git-completion.bash depends on how you have installed git. When using Homebrew, the location is something like

/usr/local/Cellar/git/<git version>/etc/bash_completion.d/

on other systems, or when using other package managers, it usually should be something like

/opt/local/etc/bash_completion.d

For further information about the git-completion.bash, take a look at the Git Tips and Tricks, chapter 2.7 in the git-scm.com book.

Update:

Git v 2.2.0 has fixed this issue so just upgrade if you're running into this issue.

like image 147
TobiasMende Avatar answered Oct 25 '22 07:10

TobiasMende