Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash git alias tab completion error

I know Git aliases can be used with arguments Git Faq section "Git Aliases with argument".

But today I encountered an error.

Take an easy example which is not suitable for real use. If I make an alias like this:

[alias]
    lo = "!sh -c 'git log $1' -"

then I can use

git lo file_a

to see the log of file_a.

But when I used "tab" to auto-complete the path, the following error occurs.

git lo [tab]

error msg:

sh: declare: `_git_{': not a valid identifier

It seems a bug in git-completion.bash. But I can't find where the `_git_{' is!

Also I find that in the error msg the quote mark around _git_{ seems strange.

BTW, my msysgit version is 1.7.6-preview20110708

ADD:

The other strange thing is, I searched all files under the dir of Git, find there is no file contains the string _git_{.

like image 462
HaveF Avatar asked Sep 02 '11 02:09

HaveF


3 Answers

I'm guessing there's a custom completion function set up for git, and the error is in that setup. Try removing the custom completion first and see if the error disappears:

complete -r git

Side note: for shell commands with reusable arguments in a git alias, the modern idiom is to define a shell function, which lets you use standard shell argument processing and has one fewer levels of argument quoting to deal with when compared to 'sh -c':

[alias]
  plush = "!f() { git pull \"$@\" && git push \"$@\" }; f"
like image 58
Mark Reed Avatar answered Nov 20 '22 03:11

Mark Reed


I had the exact same problem. For example, I had an alias for deleting a local branch and its remote counterpart in one go:

[alias]
db = "!f() { git branch -d $1 && git push origin :$1; }; f"

In order to fix the problem, I removed the alias and added a file named git-db to my Git scripts directory. It can be any directory in the PATH. Here's the contents of the file.

#!/bin/sh

git branch -d $1 && git push origin :$1

Note that the file must not have an extension. It can be used just like the alias:

git db mybranch
like image 41
unguiculus Avatar answered Nov 20 '22 05:11

unguiculus


This error is due to a shortcoming in the bash completion script that shipped with older versions of Git. It was not designed to handle shell aliases, which caused this error. This was fixed in commit 56f24e80f0, but this change was not included until Git 2.1.0. However, msysGit is as of this writing still on Git 1.9.5 and thus does not include the fix.

The preferred solution is to switch to Git for Windows, the successor to msysGit, which tracks current Git releases.

However, if you are stuck with an old version of Git, you can still work around the problem by replacing the alias with a custom script, as described in the answer by @Reinhard Nägele.

like image 2
Daniel Harding Avatar answered Nov 20 '22 03:11

Daniel Harding