13

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_{.

Ilkka
  • 2,644
  • 1
  • 23
  • 27
HaveF
  • 2,995
  • 2
  • 26
  • 36
  • I only see that completion error in the got bash command though, not in a git cmd session, where the completion works well. – VonC Sep 02 '11 at 05:58
  • You means it's a bash error? No related with Git? – HaveF Sep 03 '11 at 11:27
  • Yes, maybe you are right. I test this alias in Git 1.7.4 on Debian, it seems all right. Maybe this bug is taken by the bash which shipped with msysgit. I will report it to msysgit community.Thanks, VonC! – HaveF Sep 03 '11 at 15:32
  • AFAIK Debian use dash rather than bash... – kusma Sep 03 '11 at 16:43
  • The `_git_{` will be somewhere within a script expansion that added the `_` and the `_{` either side of the command 'git' that you typed. Though that doesn't help much... – Philip Oakley Sep 04 '11 at 17:30
  • 1
    @kusma I don't know what's the default shell on Debian, but I use *bash*. – HaveF Sep 05 '11 at 02:14
  • @Philip, thanks for your tip. BTW do you know where is the strange quote mark come from? – HaveF Sep 05 '11 at 02:16
  • @HaveF; Not sure which of the marks you think is the strange one ;-). I'm expecting the message is from the shell itself, so you won't find them in the script, and that the problem is at a `declare` action within the script which included expansion/evaluation of variables. – Philip Oakley Sep 05 '11 at 09:19
  • @Philip, thanks for your patience. I get it. – HaveF Sep 06 '11 at 01:57

3 Answers3

3

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
unguiculus
  • 413
  • 3
  • 8
2

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.

Community
  • 1
  • 1
Daniel Harding
  • 1,186
  • 11
  • 14
  • Note: as of this writing, Git For Windows is at 2.4.2 (soon 2.4.3): https://github.com/git-for-windows/git/releases. Msysgit is phased out (http://stackoverflow.com/a/784743/6309), using msys2 64-bits (http://stackoverflow.com/a/17810334/6309), and resulting in http://git-for-windows.github.io/ instead of the old and now obsolete http://msysgit.github.io/. – VonC Jun 10 '15 at 15:36
  • Looks like things are still catching up with the switch then. git-scm.com still links to version 1.9.5 of msysGit instead of the latest version of Git for Windows. Will update my answer accordingly. – Daniel Harding Jun 11 '15 at 12:11
  • I agree. The switch isn't official yet, but I have been using Git for Windows 2.x for more than a month, without any issue. – VonC Jun 11 '15 at 12:27
2

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"
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • Thanks for your reply. Yes, there is a custom completion function file for git, but it does same as the file of Git 1.7.4 on Debian, so I still think this bug is introduced by bash which shipped with mysysgit. I also send this question to the maillist of mysysgit, but did not get a answer. So, just forget about it...BTW, thanks for your "side note"! – HaveF Sep 24 '11 at 02:45
  • Note to self: msysgit mailing list message on this: http://groups.google.com/group/msysgit/browse_thread/thread/37e89bcac954f5ec/f09a450a140021dc. No answer yet. – VonC Nov 12 '11 at 12:06