Skip to content

Instantly share code, notes, and snippets.

@josephhainline
Forked from karlvr/xcode-git-version.sh
Last active August 29, 2015 14:10
Show Gist options
  • Save josephhainline/b9a2a367e346deb6c0da to your computer and use it in GitHub Desktop.
Save josephhainline/b9a2a367e346deb6c0da to your computer and use it in GitHub Desktop.
#!/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, add the script's path to a "Run Script" build
# phase for your application target.
set -o errexit
set -o nounset
# First, check for git in $PATH
hash git 2>/dev/null || { echo >&2 "Git required, not installed. Aborting build number update script."; exit 0; }
# Run Script build phases that operate on product files of the target that defines them should use the value of this build setting [TARGET_BUILD_DIR]. But Run Script build phases that operate on product files of other targets should use “BUILT_PRODUCTS_DIR” instead.
#INFO_PLIST="${TARGET_BUILD_DIR}/SecureAssets-Info.plist"
#INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/Info"
#INFO_PLIST="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
INFO_PLIST="$INFOPLIST_FILE"
# Build version (closest-tag-or-branch "-" commits-since-tag "-" short-hash dirty-flag)
BUILD_VERSION=$(git describe --tags --always --dirty=+)
# Use the latest tag for short version (expected tag format "vn[.n[.n]]")
# or if there are no tags, we make up version 0.0.<commit count>
LATEST_TAG=$(git describe --tags --match 'v*' --abbrev=0 2>/dev/null) || LATEST_TAG="HEAD"
LATEST_SHA=$(git rev-parse --short HEAD 2>/dev/null)
if [ $LATEST_TAG = "HEAD" ]
then COMMIT_COUNT=$(git rev-list --count HEAD)
LATEST_TAG="0.0.$COMMIT_COUNT"
COMMIT_COUNT_SINCE_TAG=0
else
COMMIT_COUNT_SINCE_TAG=$(git rev-list --count ${LATEST_TAG}..)
LATEST_TAG=${LATEST_TAG##v} # Remove the "v" from the front of the tag
fi
if [ $COMMIT_COUNT_SINCE_TAG = 0 ]; then
SHORT_VERSION="$LATEST_TAG"
else
# append "c" + commit-count-since-tag
# e.g. commit after 1.0 is 1.0c1, commit after 1.0.0 is 1.0.0c1
# this is the bit that requires /bin/bash
OLD_IFS=$IFS
IFS="."
VERSION_PARTS=($LATEST_TAG)
LAST_PART=$((${#VERSION_PARTS[@]}-1))
VERSION_PARTS[$LAST_PART]=$((${VERSION_PARTS[${LAST_PART}]}))
SHORT_VERSION="${VERSION_PARTS[*]}c${COMMIT_COUNT_SINCE_TAG}"
IFS=$OLD_IFS
fi
# For debugging:
echo "BUILD VERSION: $LATEST_SHA"
echo "LATEST_TAG: $LATEST_TAG"
echo "COMMIT_COUNT_SINCE_TAG: $COMMIT_COUNT_SINCE_TAG"
echo "SHORT VERSION: $SHORT_VERSION"
echo "BUNDLE_VERSION: $LATEST_SHA"
/usr/libexec/PlistBuddy -c "Add :CFBundleBuildVersion string $BUILD_VERSION" "$INFO_PLIST" 2>/dev/null || /usr/libexec/PlistBuddy -c "Set :CFBundleBuildVersion $LATEST_SHA" "$INFO_PLIST"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $SHORT_VERSION" "$INFO_PLIST"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $LATEST_SHA" "$INFO_PLIST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment