Use Xcode+git to automatically version your apps

20 Dec
2011

It’s convenient to use git’s commit hash as a versioning tool. Here’s an easy way to integrate this with Xcode.
Bonus: This one doesn’t actually change the Info.plist file, so after a compile there are no dirty files git would want to check in.

The first method works for regular apps. At the end I show a option if you’re building a framework/static library.

Easiest way: Use the Info.plist preprocessor. Add this as a Run Script phase at the top of your target (after Target Dependencies).

We’re using the output of git describe as the full version string (which can be anything, according to Apple), and the count of the total commits for CFBundleVersion, which should be numeric, else certain functions like Finder’s ability to find the lastest app version might break. Counting git commits is not the best way, but it works.

Then, enable “Preprocess Info.plist File” and add “Info.plist Preprocessor Prefix File” to “InfoPlist.h”.

Almost done! Add GIT_VERSION and GIT_COMMIT_COUNT to your Info.plist file:

Finally, you need to create the InfoPlist.h file once, else Xcode doesn’t compile at all.

That’s it! Note that you probably don’t want to use “1.x.x-00-g34g3343″ as a version for the AppStore. In this case, make a target for publishing and change the git describe to “git describe –abbrev=0 –tags” (this will return the latest tag – you DO tag your code, right?)

If you are doing more tricky things, like compiling your own static framework (I provide one for PSPDFKit), you need stronger weapons. Add this to your build script. No need for custom Info.plist preprocessing in this case. (I manually copy Info.plist into the directory, so the original plist file never gets changed.)

Thanks for everyone on Twitter helping me to get this right! This blog post on CIMGF was also pretty helpful.

Of you like posts like this, you should follow me on Twitter.

Related posts:

  1. XCode opens windows off-screen?
  2. Add a version timestamp with capistrano
  3. Enable Clang in XCode
  4. XCode and three20: Let’s refactor again!
  5. Freezing your Rails version/gems!

3 Responses to Use Xcode+git to automatically version your apps

Avatar

Jeroen

December 27th, 2011 at 4:25 pm

Yep, got this. Thx!

You could do nice things with the branch too if you want to include that:

branch=`git branch | grep \* | sed ‘s/^\* //’`

Avatar

Jeroen

December 27th, 2011 at 4:26 pm

BTW “-abbrev” should have two dashed in front.

Avatar

Rafael

February 10th, 2012 at 1:49 pm

Don’t forget to set -traditional as Info.plist Other Preprocessor Flags if you have URLs in your Plist file or otherwise they will be stripped after the colon, because a double-slash is a comment. The Plist file would get corrupted and you couldn’t build the app.

Comment Form

top

Switch to our mobile site