Skip to content

Instantly share code, notes, and snippets.

@CraigsOverItAll
Last active October 11, 2018 04:49
Show Gist options
  • Save CraigsOverItAll/f9a219d81ac0ecc8f38406f71c974eea to your computer and use it in GitHub Desktop.
Save CraigsOverItAll/f9a219d81ac0ecc8f38406f71c974eea to your computer and use it in GitHub Desktop.
Bash script to generate version, iOS build numbers and macOS build number from a projects Git repository.
#!/bin/sh
# Generates a version and build numbers from repository tags starting with a 'v' e.g. v1.0.3
# and commit counts
# Colors for output
p="\033[38;5;208m"
g="\033[38;5;7m"
a="\033[38;5;10m"
# Colour end
e="\033[0;00m"
# Get our v9.9 format tags (note the period in the 2nd+ sequences allows for
# primary version numbers > 9, ie. 10.1 will match )
vTags=$(git tag -l --sort=-v:refname "v[0123456789][.0123456789][.0123456789]*")
IFS="\n" read -ra vTagsArray <<< $vTags
rawVersionTag=${vTagsArray[0]}
version=${rawVersionTag:1}
# Nicely --count will return a big fat zero if we are on the tag YAY!!
iosBuildNumber=$(git rev-list $rawVersionTag..HEAD --count)
let iosBuildNumber+=1
macBuildNumber=$(git rev-list --count HEAD)
# The short form of the current hash
gitHash=$(git show -s --format=%h)
# Is this repo "dirty"
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working tree clean" ]] && echo "-dirty"
}
# Maybe store '-dirty' in our dirty var
dirty=$(parse_git_dirty)
# Finally lets do some output
if [ -z $1 ] || [ $1 == "-v" ]
then
output=$version
elif [ $1 == "-b" ]
then
output=$iosBuildNumber
elif [ $1 == "-mb" ]
then
output=$macBuildNumber
elif [ $1 == "-h" ]
then
output=$gitHash
else
echo $g"Usage:"$e" XcodeVersion "$a"[-b] [-mb] [-h] [-v] [--dirty]"$e
echo "\tReturns the last git version tag (format v99.99.99) stripped of the"
echo "\tleading 'v', e.g. if the tag is 'v1.2.3' it will return '1.2.3'"
echo
echo "Options:"
echo "\t-b\tReturns a pseudo build number (based on the number of commits"
echo "\t\tsince the last version tag + 1)"
echo
echo "\t-mb\tReturns a pseudo Mac build number (based on the total number of"
echo "\t\tcommits in the repository)"
echo
echo "\t-h\tReturns the short hash of the current commit"
echo
echo "\t-v\tReturns the version number. Only required if '-dirty' should be"
echo "\t\tappended to the version string."
echo
echo "\t--dirty\tReturn value will have '-dirty' appended if the repository has"
echo "\t\tuncommitted changes."
echo
echo "\t--help\tor any unknown option shows this usage output."
echo
echo "Examples:"
echo $g"\tXcodeVersion"$e
echo "\t1.2.3 "$g"\t\t=> returns the last version tag found stripped of the"
echo "\t\t\t leading 'v', in this example the tag would be: "$p"v1.2.3"$e
echo
echo $g"\tXcodeVersion"$a" -b"$e"\n\t13 "$g"\t\t=> returns number of commits after the version tag + 1"
echo
echo $g"\tXcodeVersion"$a" -mb"$e"\n\t146"$g"\t\t=> returns 'approximately' total number of commits in"
echo "\t\t\t the repository since inception. Due to branches etc,"
echo "\t\t\t the number is only approximate, but it should only"
echo "\t\t\t increase unless a rollback of the repository occurs."
echo
echo "\t\t\t=> Used for macOS build numbers which must monotonically"
echo "\t\t\t increase, see Apple Technical Note TN2420"
echo
echo "\tXcodeVersion"$a" -h"$e
echo "\td8452af "$g"\t=> returns the short hash form of the last commit"$e
echo ""
fi
# Append the dirty if requested
if [[ -n $2 && $2 == "--dirty" ]]
then
output+=$dirty
fi
echo $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment