3

I'm trying to create the following git alias:

stat = "!stat() { echo -n Number of revisions:; git log --oneline | wc -l;}; stat"

But the output is wrong:

> git stat
-n Number of revisions:
    5917

The output I want is this:

> git stat
Number of revisions: 5917

Is it normal that the -n option doesn't work ? It does on the command line though.

I'm using git version 2.5.0.

ling
  • 9,545
  • 4
  • 52
  • 49
  • 1
    I guess, your version of `echo` doen't support `-n` flag. What is your OS and `echo` version? – Alexey Ten Sep 16 '15 at 11:39
  • When use echo from command line it's usually a shell's intenal command. But in git script it's a real executable, supposedly `/bin/echo` – Alexey Ten Sep 16 '15 at 11:41

3 Answers3

2

Ok. I found it!

It seems that git alias by default uses sh instead of bash.

On my OS, sh doesn't support -n, but bash does. So the trick was to tell git to use bash instead of sh. (http://blogs.atlassian.com/2014/10/advanced-git-aliases/)

Using the -c option of bash, I could get the desired output. Here is my now working alias:

stat = "!/bin/bash -c 'stat() { echo -n Number of revisions:; git log --oneline | wc -l;}; stat'"
ling
  • 9,545
  • 4
  • 52
  • 49
1

Your shell probably does not have an echo that supports the -n flag.

But you don’t actually need the -n flag, you can just echo one line and insert the result inline there:

stat = "!echo Number of revisions: $(git log --oneline | wc -l)"

As stated in this question, you can also count the number of revisions using git rev-list HEAD --count. So the alias would be this:

stat = "!echo Number of revisions: $(git rev-list HEAD --count)"
Community
  • 1
  • 1
poke
  • 369,085
  • 72
  • 557
  • 602
0

Faster would be:

 stat = "!echo Number of revisions: $(git rev-list --count HEAD)"

This avoids log having to pull out information about each commit getting git to count the commits, and avoids having to to the word-count. rev-list also has a neat version range feature:

Another special notation is "…" which is useful for merges. The resulting set of commits is the symmetric difference between the two operands. The following two commands are equivalent:

    $ git rev-list A B --not $(git merge-base --all A B)
    $ git rev-list A...B

rev-list is a very essential Git command, since it provides the ability to build and traverse commit ancestry graphs.

This can be used to update your alias so that it will give you the revisions on a particular branch since it was forked or merged.

andygavin
  • 2,784
  • 22
  • 32