Skip to content

Instantly share code, notes, and snippets.

@ericbmerritt
Created November 4, 2012 20:53
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ericbmerritt/4013729 to your computer and use it in GitHub Desktop.
Save ericbmerritt/4013729 to your computer and use it in GitHub Desktop.
A script to generate a valid semver version in any git repo
#!/bin/bash
#
# If run inside a git repository will return a valid semver based on
# the semver formatted tags. For example if the current HEAD is tagged
# at 0.0.1, then the version echoed will simply be 0.0.1. However, if
# the tag is say, 3 patches behind, the tag will be in the form
# `0.0.1+build.3.0ace960`. This is basically, the current tag a
# monotonically increasing commit (the number of commits since the
# tag, and then a git short ref to identify the commit.
#
# You may also pass this script the a `release` argument. If that is
# the case it will exit with a non-zero value if the current head is
# not tagged.
#
set -e
commit_count=
version_candidate=
version_tag=
vsn=
get-version-candidate()
{
local ver_regex='tag: (v([^,\)]+)|([0-9]+(\.[0-9]+)*))'
local tag_lines=`git log --oneline --decorate | fgrep "tag: "`
if [[ $tag_lines =~ $ver_regex ]]; then
if [[ ${BASH_REMATCH[1]:0:1} = "v" ]]; then
version_tag=${BASH_REMATCH[1]}
version_candidate=${BASH_REMATCH[2]}
else
version_tag=${BASH_REMATCH[3]}
version_candidate=${BASH_REMATCH[3]}
fi
else
version_tag=""
version_candidate="0.0.0"
fi
}
get-commit-count()
{
if [[ $version_tag = "" ]]; then
commit_count=`git rev-list HEAD | wc -l`
else
commit_count=`git rev-list ${version_tag}..HEAD | wc -l`
fi
commit_count=`echo $commit_count | tr -d ' 't`
}
build-version()
{
if [[ $commit_count = 0 ]]; then
vsn=$version_candidate
else
local ref=`git log -n 1 --pretty=format:'%h'`
vsn="${version_candidate}+build.${commit_count}.${ref}"
fi
}
check-tag()
{
if [[ "$1" = "release" ]]; then
if [[ $commit_count != 0 ]]; then
echo "The current head *must* be tagged"
exit 127
fi
fi
}
get-version-candidate
get-commit-count
build-version
check-tag "$1"
echo $vsn
@atrauzzi
Copy link

How would you go about adding pre-release identifier support to this script?

https://semver.org/#spec-item-9

Great stuff by the way!

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