Skip to content

Instantly share code, notes, and snippets.

@simonwhitaker
Created February 8, 2012 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonwhitaker/1770898 to your computer and use it in GitHub Desktop.
Save simonwhitaker/1770898 to your computer and use it in GitHub Desktop.
A shell script for setting the CFBundleVersion of an Xcode build based on the current git revision
# A script for generating pseudo-version numbers for a git repository
# and updating the BundleVersion of an Xcode app build with this version
alias git=$(which git)
git_version()
{
# The cornerstone of this hack is "git describe", which
# relies on there being at least one "real" tag (i.e. not
# a lightweight tag) somewhere in the current code's commit
# parentage. So I normally start by tagging my initial commit
# as "0.1". Then, N commits down the line git describe returns
# 0.1-[N]-[commit-sha], e.g. 0.1-24-g0bf6847
#
# The cut -d- -f1-2 splits the input on hyphens and returns
# the first two tokens, joined back together with tokens. So
# now I've got 0.1-24
#
# The tr - . replaces hyphens with dots, so now I've got 0.1.24
GITVERSION=`git describe | cut -d- -f1-2 | tr - .`
# If there are modifications in the repository, append "-M" to
# the "revision number"
GITMODIFIED=`(git status | grep "modified:\|added:\|deleted:" -q) && echo "-M"`
echo $GITVERSION$GITMODIFIED
}
# I use the git-flow framework and build App Store builds from master, Ad Hoc
# builds from develop. This if clause ensures that I don't mess with the
# version number of an App Store build.
if [[ $(git symbolic-ref -q HEAD) != 'refs/heads/master' ]]; then
# Get location of parsed Info.plist
GS_INFO_PLIST_PATH="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/Info"
# Use git version as version number
export GS_NEW_VERSION=`git_version`
echo Version is $GS_NEW_VERSION
# Write new version number to parsed Info.plist
defaults write "$GS_INFO_PLIST_PATH" CFBundleVersion $GS_NEW_VERSION
defaults write "$GS_INFO_PLIST_PATH" CFBundleShortVersionString $GS_NEW_VERSION
fi
@zman0900
Copy link

zman0900 commented Apr 4, 2012

You might try using git rev-parse. Look at how the Linux kernel does it: http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=scripts/setlocalversion;hb=HEAD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment