views:

4205

answers:

8

I'd like to get the number of commits of my git repository, a bit like SVN revision numbers. The goal is to use it as a unique, incrementing build number.

I currently do like that, on Unix/Cygwin/msysGit:

git log --pretty=format:'' | wc -l

But I feel it's a bit of a hack.

Is there a better way to do that? It would be cool if I actually didn't need wc or even git, so it could work on a bare Windows. Just read a file or a directory structure ...

+9  A: 
VonC
I've updated my script to work with Xcode 3. You can pick up an up to date version from http://gist.github.com/208825.
Abizern
@Abizern Thank you for this feedback.
VonC
No, thank you for using the code. It's good to know that it's useful to others.
Abizern
+2  A: 

Generate a number during the build and write it to a file. Whenever you make a release, commit that file with the comment "Build 147" (or whatever the build number currently is). Don't commit the file during normal development. This way, you can easily map between build numbers and versions in Git.

Aaron Digulla
+19  A: 

If you’re looking for a unique and still quite readable identifier for commits, git describe might be just the thing for you.

Bombe
That could work and would be more easy to use than a custom-made algo. +1
VonC
I didn't know git describe. This little number between the tag name and the sha1 is just what I was looking for. Thank you.
Splo
Take a look at GIT-VERSION-GEN script and how it is used in git repository, and similar script in Linux kernel sources (and how they are used in Makefile).
Jakub Narębski
+5  A: 

There's a nice helper script that the Git folks use to help generate a useful version number based on Git describe. I show the script and explain it in my answer to How would you include the current commit id in a git project’s files? Hope that helps.

Pat Notz
Very interesting, thank you. I ended up doing something like that.
Splo
A: 

git rev-parse --short HEAD

makuchaku
+10  A: 

git shortlog is one way.

Rayne
A: 

Tito might be more than you need, but here it goes. It's a tool for managing RPM based projects using git for their source code repository and it offers the following features:

  • Tag new releases with incremented RPM version or release.
  • Auto-generate spec file changelog based on git history since last tag.
  • Build packages off an "upstream" git repository, where modifications in the "downstream" git repository will be applied as a patch in the source rpm.
Cristian Ciupitu
A: 

Adding to Rayne's answer, to strip out the blank lines and usernames from the output, and get just the commit count, run it through grep:

git shortlog | grep -E '^[ ]+\w+' | wc

The lines that have commit messages begin with some spaces.

Ben Atkin