Created
February 8, 2012 16:37
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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