Skip to content

Instantly share code, notes, and snippets.

@d108
Last active August 29, 2015 14:12
Show Gist options
  • Save d108/384144ca37af615e9378 to your computer and use it in GitHub Desktop.
Save d108/384144ca37af615e9378 to your computer and use it in GitHub Desktop.
Xcode 6 run script designed to set CFBundleShortVersionString and CFBundleVersion in the corresponding info plist for the project according to the last git tag as a version number in the form vx.x.x. This assumes git tags are used for version numbers and may not work with other tags present.
#!/bin/bash
# This script automatically sets the version and short version string of
# an Xcode 6 project from the Git repository containing the project.
#
# CFBundleVersion is set from the commit count of the current branch.
# CFBundleShortVersionString is set from the last git tag where git tags correspond to version numbers in the form vx.x.x.
#
# To use this script in Xcode 6, add the contents to a "Run Script" build
# phase for your application target.
#
# @author Daniel Zhang (張道博)
# @see Adapted from: https://gist.githubusercontent.com/jpwatts/966838/raw/9160cd000c0a844835a1e208e99cdbf8195e03f0/xcode-git-version.sh
set -o errexit
set -o nounset
INFO_PLIST="${INFOPLIST_FILE}"
# 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 --tags `git rev-list --tags --max-count=1` | sed -e 's/^v//' -e 's/g//')
# Use the number of commits on the master branch for CFBundleVersion. 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 --count HEAD)
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $SHORT_VERSION" "${INFOPLIST_FILE}"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $VERSION" "${INFOPLIST_FILE}"
# Alternative method:
#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