« »

Using git commit count as an incremental build numbering system

I’ve pretty much made a switch away from svn and towards git. It wasn’t easy. I loved svn and still know how to use it better than git. However, git just seems to work better for me as a single developer content management system. I’m still using svn for some of my projects (and whenever I’m ask to use it by a contractor) so it still has a place in my toolbox and in my heart. For nearly all of my new projects, however, I use git repositories.

Unfortunately, most of my other versioning tools are based on svn. I’ve been redoing them as necessary for a while but finally came to one that I must have now.

A while ago, I wrote on how to do better application versioning with svn. Basically, using the commit number of a subversion repository made it easier for me as a developer to figure out meaningful differences between versions of my application. If users reported a change between versions of an application, I would have some clue of what changed because I could compare revisions. Today, I decided that I absolutely needed this for git, too.

The problem is that git works with hashes instead of numbers. This isn’t a problem with git, it’s a problem with my system. However, it’s one that is easily remedied. git provides a lot of “porcelain” that makes it easy to build tools on top of tools. That’s essentially all I’ve done.

The first thing I do is set up a new “Run Shell Script” build phase in Xcode. Enter the following code as the script to run:

GIT="/opt/local/bin/git"
VERSION=`$GIT rev-list --all | wc -l | sed "s/[ \t]//g"`
defaults write $PROJECT_DIR/Info CFBundleVersion $VERSION
touch $PROJECT_DIR/Info.plist

Basically, we’re just getting a list of all of the revisions, getting the line count (using wc), and then stripping out all of the blank space (with sed). Then we use defaults to write the value to the Info.plist file directly. You may have to change the location of git depending on where you’ve installed it by changing the first line. I also like to touch any files that I’ve modified just to be sure that the files I’ve modified are clearly marked as changed.

Now, we’ve got the number of commits in our CFBundleVersion. The next problem is how to convert that into a meaningful git commit hash in case we need to compare commits. Well, I wrote a small Python script to handle it. That script gets all of the commits and prints the line that corresponds to the version number. Simple. You’ll have to install it in an executable location and also chmod +x it.

So now I have a basic incremental build numbering system to use in my applications that’s based on git. If you have any ideas on how I can improve this, please let me know.

5 comments on “Using git commit count as an incremental build numbering system”

Jason:

October 24th, 2008 at 11:22 pm

This seems like a very fragile system considering how easy it is to rewrite git commit history. You really might want to consider using the git revision hash directly instead of a count of revisions. I’ve got a ruby script that does just that here: http://github.com/jsallis/xcode-git-versioner/tree/master

Grayson:

October 25th, 2008 at 8:29 am

I understand the fragility but I’m the only commiter on the repositories that I use this for. If I do rewrite the history, I’m simply back to where I was before I had the script.

I avoid the hash because it’s ugly and more cumbersome for the user. Given that this system is merely for convenience, it doesn’t have to be bullet proof and I prefer incremental versioning to hashes.

Jason:

October 25th, 2008 at 4:20 pm

Fair enough!

Kevin:

March 5th, 2009 at 8:59 pm

I independently came to the same conclusions, although I use a different git command…

git log —pretty=format:”%H”

Grayson:

March 8th, 2009 at 3:58 pm

I like —pretty=format. However, using %h will shorten the hash. This may be important if/when I reach some file/memory/performance limit of the shell/scripting languages. Thanks for letting me know. I’m learning git as I use it and still don’t know a lot of the niceties.

Leave a Reply