alamoot
alamoot

Reputation: 2144

Git auto completion behaving strangely

I'm seeing something very weird with git aliases. When using the tab key to get git commands auto completed I get bash errors. For example, if I type git pu to see all the options this happens:

$ git pu-bash: remote: command not found
-bash: -v: command not found
-bash: command substitution: line -327: syntax error near unexpected token `|'
-bash: command substitution: line -327: `| '
-bash: usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]: command not found
-bash: rev-parse: command not found
-bash: usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]: command not found
-bash: usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]: command not found
-bash: brname: command not found
-bash: brname: command not found
-bash: brname-trimmed: command not found
-bash: brname-trimmed: command not found
-bash: ls-files: command not found
-bash: @{u}: command not found

The terminal gets stuck few lines into the above output and after ^Cing a couple of times all error lines are printed.

I'm not sure to which file the line numbers the errors are referring to as my aliass in ~/.gitconfig end on line 174. I commented out any alias that had its name in the error list (ex brname), or used a git command that was listed (ex the builtin rev-parse is in the error). This was done till the error messages went away and and auto completion started working again.

So in the good state now if I type git pu and press tab I see the following:

$ git pu
pull   pull   push   push  

Note that the responses are doubled here, and for any other input I press tab on.

One of the aliases I commented out to get to the good state is this:

brname-trimmed = !git rev-parse --abbrev-ref HEAD | tr -d '\n'

Let's say I'm on tab T1

  1. If I open a new tab T2, uncomment the alias on T1, and save the file, I can use git with auto completion without any problems on T2.
  2. If I uncomment the alias, save the file, and then open a new tab T2 and type git pu on T2 I see:
$ git pu-bash: : command not found
-bash: command substitution: line -166: syntax error near unexpected token `&&'
-bash: command substitution: line -166: `&& '

line 166 of my ~/.gitconfig doesn't have : nor &&, it's even commented out.

  1. If I uncomment the alias, and change it to xbrname-trimmed (just adding an x to the beginning so it's a different word) auto completion works just fine regardless of when T2 is opened.

I'm working on a machine I haven't done dev work in months, so not sure when the issue occurred. The git version is 2.22.0

Upvotes: 2

Views: 1586

Answers (1)

VonC
VonC

Reputation: 1323203

Check first if the issue persists with Git 2.24, considering 2.23 and 2.24 have worked on the completion script.

Check also if your Git bash completion is correctly installed.

I added in the comments:

There a possibility for your .bashrc/.profile to have somehow an error which would manifest when running that completion bash script.
You need to test it with a minimal (almost empty) content for your .bashrc/.profile ("empty", beside defining at least the $PATH)

The OP alamoot confirms:

Great call!
In my ~/.bash_profile I was calling a script which was setting a custom $BASH_COMPLETION and $BASH_COMPLETION_DIR.
This custom script was part of and old "system setup" I don't need no more.
So taking it out I have got git completion working again.

Upvotes: 3

Related Questions