Skip to content

Instantly share code, notes, and snippets.

@thirtified
Forked from jpwatts/xcode-git-version.sh
Created July 16, 2012 09:14
Show Gist options
  • Save thirtified/3121716 to your computer and use it in GitHub Desktop.
Save thirtified/3121716 to your computer and use it in GitHub Desktop.
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.
#!/bin/bash
# This script automatically sets the version and short version string of
# an Xcode project from the Git repository containing the project.
#
# - Uses version from latest git tag for CFBundleShortVersionString
# (e.g. 1.2.3 from "v1.2.3")
# - Uses combined string <commit number>.<short commit hash> for CFBundleVersion
# (e.g. 18.9d75e30)
#
# To use this script in Xcode 4, add the contents to a "Run Script" build
# phase for your application target.
#
# Based on https://gist.github.com/966838
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}" tag | tail -n 1 | sed -e 's/^v//')
# I'd like to use the Git commit hash for CFBundleVersion.
COMMIT_HASH=$(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 + "." + the commit hash.
NUMBER_OF_COMMITS=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" rev-list master | wc -l)
VERSION="${NUMBER_OF_COMMITS}.${COMMIT_HASH}"
echo "CFBundleShortVersionString: $SHORT_VERSION"
echo "CFBundleVersion: $VERSION"
echo "Writing Version info to $INFO_PLIST"
defaults write $INFO_PLIST CFBundleShortVersionString $SHORT_VERSION
defaults write $INFO_PLIST CFBundleVersion $VERSION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment