1

I've tried to do almost the same as described here, with same problems:

Creating a 'git log' alias with formatting

Solutions from this topic, of course, works.

But let say I want to group my git functions in git namespace, that is, to run git gl "some time ago". But when I try to define function directly in .gitconfig:

gl = "!f() { git log --all --pretty=format:'%h %cd %s (%an)' --since="$1"; }; f",

I have same error as before - fatal: ambiguous argument ...lalala....

Same happens even when function is defined in .bash_profile and called from git alias.

It's not so big problem to use "global" functions, but I want to know, why this happens? And is it possible at all to define such function in .gitconfig?

Community
  • 1
  • 1
jeron-diovis
  • 788
  • 1
  • 8
  • 19

1 Answers1

3

It's a quoting issue.

I have not seen anything that properly describes how quoting works in git aliases, but this works:

gl = "!f() { git log --all --pretty=format:'%h %cd %s (%an)' --since=\"$1\"; }; f"

That is, use backslash-double-quote to get the double quotes to go through to the shell alias, so that $1 is expanded.

Note that you must invoke it as:

$ git gl "one year ago"

If, instead of $1, you use $*, you can invoke it as:

$ git gl one year ago

which is kind of convenient.

torek
  • 448,244
  • 59
  • 642
  • 775
  • +1. I am wondering how did you figure that out? trial and error? – Anshul Goyal Apr 11 '14 at 19:57
  • 1
    Experimentation. Helpful items include setting GIT_TRACE (to nonzero) in the environment, and setting -x in the shell function, so you can see what shows up where. (I did the experimentation a while ago, and left some commented out aliases in my .gitconfig as a reminder to myself.) – torek Apr 11 '14 at 20:06