Xcode: git describe in your Info.plist

Xcode git describeDebugging programs for yourself (or from bug-reports from other people) requires knowing which version caused the bug. Of course most people think in terms of major releases, but a developer needs to know the exact revision, as this might make the crucial difference. UNIX command-line programs usually offer a –version switch, which returns detailed version and build-option information.

If you want to have something similar with Xcode you can use the “New Run Script Build Phase” to add a simple script to your build-process. In the case of GitX, the code looks like this (the original can be found at xcode-git-build-scripts at GitHub):

#!/usr/bin/env ruby

version = `/usr/bin/env git describe`.chomp

info_file = File.join(ENV['BUILT_PRODUCTS_DIR'], ENV['INFOPLIST_PATH'])
info = File.open(info_file, "r").read

version_re = /([t ]+<key>CFBundleVersion</key>n[t ]+<string>).*?(</string>)/
info =~ version_re
bundle_version_string = $1 + version + $2

info.gsub!(version_re, bundle_version_string)
File.open(info_file, "w") { |file| file.write(info) }
puts "Set version string to '#{version}'"

Needless to say that this script is not very error-resistant, but since ruby is included with OSX and people compiling GitX usually have git installed, it should work in most cases.
Update
I just discovered that the CFBundVersion is supposed to be a monotically increasing integer, separated only by periods. This is required by MacOS and things like Sparkle. The string “git describe” returns doesn’t match these criteria, obviously. So, if you encounter problems, you should fall back to using the format of x.y.z.<commits> where x.y.z is your major release number and <commits> is the number of commits since mentioned tag.

2 Responses to “Xcode: git describe in your Info.plist”

  1. One last time entity encoded?

    #!/bin/sh

    version=$(git describe) || exit
    info_file=”$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH”

    if sed -i.orig -e ”
    /<key>CFBundleVersion<\\/key>/ {
    n
    s/<string>.*<\\/string>/<string>$version<\\/string>/
    }” “$info_file”
    then
    echo Set version string to $version
    rm -f “$info_file.orig”
    else
    mv -f “$info_file.orig” “$info_file”
    fi

  2. [...] 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 [...]

Leave a Reply