Skip to content

Commit

Permalink
update get_version_from_git.sh script
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanotorresi committed Dec 20, 2024
1 parent 151db10 commit 5f7b6cf
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions scripts/get_version_from_git.sh
Original file line number Diff line number Diff line change
@@ -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}"

0 comments on commit 5f7b6cf

Please sign in to comment.