Skip to content

Instantly share code, notes, and snippets.

@tgray
Last active August 12, 2017 20:41
Show Gist options
  • Save tgray/96f1100a39f5a9d5b35c to your computer and use it in GitHub Desktop.
Save tgray/96f1100a39f5a9d5b35c to your computer and use it in GitHub Desktop.
A set of scripts to manage version numbers with Xcode and git. Primarily aimed at command line tools.

Updating program version numbers

Note - The following could hold true for any Xcode project, but I only really have one, contacts, so it is pretty specific to that project.

Instructions

  1. Change the number in the file VERSION.
  2. Tag the final commit with the version number:
    git tag -am '0.1' 0.1
  3. Push the tag to github:
    git push --tags

See the build script (version-num.sh) for more information. This file is attached in the Xcode project as a Run Script in the Build Phase of a second target, the Version Number Scripts target. I added this target as a dependency to the main binary. This build script will use the largest version number found as either a git tag or in the VERSION file.

Incrementing the version number

I've also writting a script (version-inc.sh) that will auto increment the major or minor version number in the file, or tag the git commit with the number found in the version file.

Incrementing the VERSION should be done before the git commit. Then when you are ready for tagging your final release, use version-inc.sh tag to tag the git commit (post commit).


https://github.com/tgray

#!/bin/sh -
# https://github.com/tgray
_major() {
perl -pi -e 's/(version:\s)(\d+)\.(\d+)/($1) . ($2 + 1) . q{.0} /e' VERSION
}
_minor() {
perl -pi -e 's/(version:\s)(\d+)\.(\d+)/($1) . ($2) . q{.} . (1 + $3) /e' VERSION
}
_tag() {
vernum=`grep 'version' VERSION | grep -Eo '[.0-9]+'`
git tag -am "$vernum" $vernum
}
_help() {
progname=`basename $0`
cat << EOF
Usage: ${progname} <action>
actions:
help - print this help
major - increments the major version number by one and sets the minor to zero.
minor - increments the minor version number by one.
tag - tags the git commit with the version from the VERSIONS file. Only do
this after everything is finalized.
The 'major' and 'minor' actions should be run pre git commit, while the 'tag'
action should be run post commit as the final step before pushing to github.
EOF
}
action=$1; shift
case "${action}" in
major) _major "$@";;
minor) _minor "$@";;
tag) _tag "$@";;
*) _help "$@";;
esac
exit 0
#!/bin/sh -
# build data file that is included in the source
# so we can automatically report Git repo information
# in the application
if [[ ! -d ".git" ]]; then
versionNumber=`cat VERSION | grep version | awk '{print $2}'`
lastCommitHash=`cat VERSION | grep commit | awk '{print $2}'`
else
echo "Building file"
# cd ${PROJECT_DIR}/${PROJECT_NAME}
gitDataFile="${CONFIGURATION_TEMP_DIR}/gitDataAutoGenerated.h"
echo "Get Information from system"
# Date and time that we are running this build
buildDate=`date "+%F %H:%M:%S"`
# Current branch in use
currentBranchTemp=`git rev-parse --abbrev-ref HEAD`
if [ -n "$currentBranchTemp" ]
then
currentBranch=$currentBranchTemp
else
currentBranch=""
fi
# Last hash from the current branch
lastCommitHashTemp=`git rev-parse --short HEAD`
if [ -n "$lastCommitHashTemp" ]
then
lastCommitHash=$lastCommitHashTemp
else
lastCommitHash=""
fi
# Date and time of the last commit on this branch
lastCommitDateTemp=`git log --pretty=format:"%ad" --date=short -1`
if [ -n "$" ]
then
lastCommitDate=$lastCommitDateTemp
else
lastCommitDate=""
fi
# Comment from the last commit on this branch
lastCommitCommentTemp=`git log --pretty=format:"%s" -1`
if [ -n "$" ]
then
lastCommitComment=$lastCommitCommentTemp
else
lastCommitComment=""
fi
# Last tag applied to this branch
lastRepoTagTemp=`git describe --abbrev=0 --tags`
if [ -n "$lastRepoTagTemp" ]
then
lastRepoTag=$lastRepoTagTemp
else
lastRepoTag="0.0.0"
fi
# Version number from VERSIONS file
versionFileVersionTemp=`cat VERSION | grep version | awk '{print $2}'`
if [ -n "$versionFileVersionTemp" ]
then
versionFileVersion=$versionFileVersionTemp
else
versionFileVersion="0.0.0"
fi
# Test version numbers
vertest=`perl -e '($a,$b)=@ARGV; for ($a,$b) {s/(\d+)/sprintf "%5d", $1/ge}; print $a cmp $b;' $lastRepoTag $versionFileVersionTemp`
if [[ "$vertest" -eq -1 ]]; then
versionNumber=$versionFileVersionTemp
else
versionNumber=$lastRepoTag
fi
# Build the file with all the information in it
echo "Create header file"
echo "//-----------------------------------------" > $gitDataFile
echo "// Auto generated file" >> $gitDataFile
echo "// Created $buildDate" >> $gitDataFile
echo "//-----------------------------------------" >> $gitDataFile
echo "" >> $gitDataFile
echo "#define BUILD_DATE @ \"$buildDate\"" >> $gitDataFile
echo "#define GIT_CURRENT_BRANCH @ \"$currentBranch\"" >> $gitDataFile
echo "#define GIT_LAST_COMMIT_HASH @ \"$lastCommitHash\"" >> $gitDataFile
echo "#define GIT_LAST_COMMIT_DATE @ \"$lastCommitDate\"" >> $gitDataFile
echo "#define GIT_LAST_COMMIT_COMMENT @ \"$lastCommitComment\"" >> $gitDataFile
echo "#define GIT_LAST_REPO_TAG @ \"$lastRepoTag\"" >> $gitDataFile
echo "#define VERSION_FILE_VERSION @ \"$versionFileVersion\"" >> $gitDataFile
echo "#define AUTOVERSION $versionNumber" >> $gitDataFile
fi
echo "Create temporary Info.plist"
# Make my own Info.plist file
PLIST="${PROJECT_DIR}/${PROJECT_NAME}/Info"
cp "${PROJECT_DIR}/${PROJECT_NAME}/Info-template.plist" ${PLIST}.plist
# defaults write "${PLIST}" CFBundleShortVersionString ${lastRepoTag}
# defaults write "${PLIST}" CFBundleVersion ${lastCommitHash}
/usr/libexec/PlistBuddy -c "Set CFBundleShortVersionString ${versionNumber}" "${PLIST}.plist"
/usr/libexec/PlistBuddy -c "Set CFBundleVersion ${lastCommitHash}" "${PLIST}.plist"
defaults read "${PLIST}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment