GitXA while back I wrote about having the output of “git describe” as your version-string in your Mac-OS app, only to discover that this doesn’t work that well since the string in the Info.plist is supposed to be an constantly increasing number of the form x.y.z.zzzz, which doesn’t really work with the SHAs git gives you. But since I really wanted that feature I kept searching and found a solution which seems to work pretty well and doesn’t interfere with the version-string.

Introducing a new key to Info.plist

First of you’ll need an additional key in your Info.plist where your custom version-string will be stored. I used “CFBundleGitVersion” and set its content to “GIT_VERSION”. You can simply do that in XCode.

The build-script

You’ll still need a build-script, like I described before. A really simple one would be:

VERSION=$(cd $PROJECT_DIR; git describe)<br /> echo -n "#define GIT_VERSION $VERSION" > $PROJECT_TEMP_DIR/revision<br /> touch Info.plistIf that doesn’t work you might need to include some PATH exports above, but I wanted to keep this example to the relevant parts.

Info.plist preprocessing

Now you can preprocess the Info.plist when building your app, using the revision-file as a header. This means that your GIT_VERSION string will be replaced. Edit your Build-Settings like this:Info.plist preprocessing using git describe

Using the string in the About-window

So, now your Info.plist contains a meaningful string. You want that to be displayed to the user when he opens the “About” window, but you don’t want to write your own window. First you have to write an IBAction in some controller that’s in your MainMenu.xib. Then you’ll have to reconnect the About-button to that action from your controller, which should look like this:

- (IBAction)showAboutPanel:(id)sender
	NSString *gitversion = [[[NSBundle mainBundle] infoDictionary] 
	NSMutableDictionary *dict = nil;
	if (gitversion)
		dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
			gitversion, @"Version", nil];

	[NSApp orderFrontStandardAboutPanelWithOptions:dict];

So, if gitversion is set then the default Version-string is overwritten with it when calling the about-panel, while for Mac-OS the extra-key in your Info.plist is completely meaningless and to differentiate between version you can still use a simple integer. If that was to hard to follow just have a look at the commit in the GitX-repo.