2

I would like to write a git alias for:

git log --all --grep='Big boi'

What I have so far is:

[alias]
    search = "!f() { str=${@}; echo $str; git log --all --grep=$str; }; f"

Which echos the string perfectly fine but gives an error, I can't seem to figure out how to pass the string to the grep flag.

$ user in ~/src/repo on master λ git search 'Big boi'
Big boi
fatal: ambiguous argument 'boi': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

I'm using zsh if it makes any difference . . .

joshuatvernon
  • 1,530
  • 2
  • 23
  • 45
  • 1
    Git alias processing aside, your function should be `f() { str="$*"; echo "$str"; git log --all --grep="$str"; }`. There is no call for using `$@` here, because you aren't trying to treat the expansion as a sequence of separate words. – chepner Mar 01 '18 at 17:14

1 Answers1

5

That alias seems to work if you are using double-quotes:

git search "Big boi"

I also made it work with --grep=\"$str\" (and still using double-quotes)

The OP joshuatvernon adds in the comments:

I amended it to

search = "!f() { str="$*"; echo "$str"; git log --all --grep=\"$str\"; }; f"

and it works with single, double or no quotes.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    The alias should be fixed; the user shouldn't have to know what type of quotes to use to satisfy Git's alias rules. – chepner Mar 01 '18 at 17:14
  • 1
    With a mixture this @VonC answer and the comment by @chepner I amended it to `search = "!f() { str="$*"; echo "$str"; git log --all --grep=\"$str\"; }; f"` and it works with single, double or no quotes – joshuatvernon Mar 02 '18 at 03:03
  • 1
    @joshuatvernon Great! I have included your comment in the answer for more visibility. – VonC Mar 02 '18 at 05:53