views:

54

answers:

2

we have a build system that uses the svn ID as an imput to a VM builder appliance that required a five digit number. When I build from git I have been faking this by counting the number of commits in the git repo. This only sort-of-works :-/ I'm tyring to figure out:

  • how can I get a unique 5 digit number from the git repo.
A: 

In git, every commit generates a unique SHA1 hash id. You can see the id for each commit when running git log. If you want a 5 digit number for the most recent commit, you could do something like git log --pretty=oneline --abbrev-commit --abbrev=5 -1. For one of my repos the output looks like this:

$ git log --pretty=oneline --abbrev-commit --abbrev=5 -1    
3b405... fixed css for page title.

You can experiment with the other options to git log to customize the format if needed. Of course, if the repository has enough commits, there's always the possibility that 5 digits will not be enough to guarantee uniqueness, but for small enough projects it may do.

Jason Jenkins
+2  A: 

You're looking for git describe:

The command finds the most recent tag that is reachable from a commit. If the tag points to the commit, then only the tag is shown. Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object and the abbreviated object name of the most recent commit.

$ git describe master
v1.10-5-g4efc7e1
Casey
You'll probably want to use the `--tags` option, unless you're in the good habit of using only annotated tags.
Jefromi