public
Last active

This Xcode 4 build phase script automatically sets the version and short version string of an application bundle based on information from the containing Git repository.

  • Download Gist
xcode-git-version.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/bin/bash
 
# This script automatically sets the version and short version string of
# an Xcode project from the Git repository containing the project.
#
# To use this script in Xcode 4, add the contents to a "Run Script" build
# phase for your application target.
 
set -o errexit
set -o nounset
 
INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/Info"
 
# Use the latest version tag for CFBundleShortVersionString. I tag releases
# in Git using the format v0.0.0; this assumes you're doing the same.
SHORT_VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" describe --dirty | sed -e 's/^v//' -e 's/g//')
 
# I'd like to use the Git commit hash for CFBundleVersion.
# VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" rev-parse --short HEAD)
 
# But Apple wants this value to be a monotonically increasing integer, so
# instead use the number of commits on the master branch. If you like to
# play fast and loose with your Git history, this may cause you problems.
# Thanks to @amrox for pointing out the issue and fix.
VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" rev-list master | wc -l)
 
defaults write $INFO_PLIST CFBundleShortVersionString $SHORT_VERSION
defaults write $INFO_PLIST CFBundleVersion $VERSION

you might want to incorporate the small change I did at: https://gist.github.com/4003642, apparently apple wants short version to be at most 3 integers separated by dots. so I have introduced a new variable FullVersion to store hash info

Daft question - but what should WRAPPER_NAME be ? Its not a default var in Xcode 4.5

I had to add describe --tags inside git commands, but overall, it getting the right information.
BUT, looks like the defaults write $INFO_PLIST section doesn't work at all.

Should it? In Xcode 5?

I feel I have significantly improved this script and urge people to take a look: https://gist.github.com/nickshanks/9048830

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.