diff --git a/scripts/get_version_from_git.sh b/scripts/get_version_from_git.sh index 6fee348..51ab797 100755 --- a/scripts/get_version_from_git.sh +++ b/scripts/get_version_from_git.sh @@ -1,14 +1,19 @@ #!/bin/sh -TAG=$( git tag | grep -E "[0-9]\.[0-9]\.[0-9]" | sort -rn | head -n1 ) - -if [ -n "${TAG}" ]; then - COMMITS_SINCE_TAG=$(git rev-list "${TAG}".. --count) - if [ "${COMMITS_SINCE_TAG}" -gt 0 ]; then - COMMIT_SHA=$(git show -s --format=%ct.%h HEAD) - SUFFIX="+git.dev${COMMITS_SINCE_TAG}.${COMMIT_SHA}" - fi + +# https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string +SEMVER_REGEX="^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$" + +TAG=$(git tag | grep -P "$SEMVER_REGEX" | sed '/-/!{s/$/_/}' | sort -V | sed 's/_$//' | tail -n1) + +if [ -z "${TAG}" ]; then + echo "Could not find any semver tag" 1>&2 + exit 1 else - TAG="0" + COMMITS_SINCE_TAG=$(git rev-list "${TAG}".. --count) + if [ "${COMMITS_SINCE_TAG}" -gt 0 ]; then + COMMIT_INFO=$(git show -s --format=%ct.%h HEAD) + SUFFIX="+git.${COMMITS_SINCE_TAG}.${COMMIT_INFO}" + fi fi echo "${TAG}${SUFFIX}"