From ad7e4817e17310104d99f583183b77ff92473d63 Mon Sep 17 00:00:00 2001 From: Evan Boyle Date: Sun, 29 Mar 2020 16:53:23 -0700 Subject: [PATCH] update get-version to understand submodule tags (#4229) --- scripts/get-version | 40 +++++++++++++++++++++++++++++----------- scripts/get-version.ps1 | 14 ++++++++++++-- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/scripts/get-version b/scripts/get-version index d9889f9d0..00a9550f0 100755 --- a/scripts/get-version +++ b/scripts/get-version @@ -8,6 +8,7 @@ fi COMMITISH=$1 DIRTY_TAG="" +EXACT=0 # Figure out if the worktree is dirty, we run update-index first # as we've seen cases in Travis where not doing so causes git to @@ -19,7 +20,34 @@ fi # If we have an exact tag, just use it. if git describe --tags --exact-match "${COMMITISH}" >/dev/null 2>&1; then - echo -n "$(git describe --tags --exact-match "${COMMITISH}")" + EXACT=1 + TAG=$(git describe --tags --exact-match "${COMMITISH}") +# Otherwise, increment the minor version version (if the package is 1.X or later) or the +# patch version (if the package is pre 1.0), add the -alpha tag and some +# commit metadata. If there's no existing tag, pretend a v0.0.0 was +# there so we'll produce v0.0.1-dev builds. +elif git describe --tags --abbrev=0 "${COMMITISH}" > /dev/null 2>&1; then + TAG=$(git describe --tags --abbrev=0 "${COMMITISH}") +else + TAG="v0.0.0" +fi + +# Check to see if the latest tag is for a submodule and transform to semver tolerant +# e.g: submodule/submodule/.../v.X.X.X +# sdk/v1.1.1 -> v1.1.1 +IFS='/' +read -a tagsplit <<< "${TAG}" + +if echo "${#tagsplit[@]}" > /dev/null 2>&1 -gt 1; +then + TAG=${tagsplit[${#tagsplit[@]}-1]} +fi + +# if we found an exact match, return it at this point +# after we have removed any submodule prefixes. +if [ $EXACT -eq 1 ] +then + echo -n "${TAG}" if [ ! -z "${DIRTY_TAG}" ]; then echo -n "+${DIRTY_TAG}" fi @@ -28,16 +56,6 @@ if git describe --tags --exact-match "${COMMITISH}" >/dev/null 2>&1; then exit 0 fi -# Otherwise, increment the minor version version (if the package is 1.X or later) or the -# patch version (if the package is pre 1.0), add the -alpha tag and some -# commit metadata. If there's no existing tag, pretend a v0.0.0 was -# there so we'll produce v0.0.1-dev builds. -if git describe --tags --abbrev=0 "${COMMITISH}" > /dev/null 2>&1; then - TAG=$(git describe --tags --abbrev=0 "${COMMITISH}") -else - TAG="v0.0.0" -fi - # Strip off any pre-release tag we might have (e.g. from doing a -rc build) TAG=${TAG%%-*} diff --git a/scripts/get-version.ps1 b/scripts/get-version.ps1 index 46ba51d43..a9eadddb2 100644 --- a/scripts/get-version.ps1 +++ b/scripts/get-version.ps1 @@ -9,10 +9,15 @@ $dirty=($LASTEXITCODE -ne 0) try { git describe --tags --exact-match >$null 2>$null # If we get here the above did not throw, so we can use the exact tag + $tag = "$(git describe --tags --exact-match)" + # Remove any sub-module prefixes + if ($tag.LastIndexOf("/") -ne -1) { + $tag=$tag.Substring($tag.LastIndexOf("/")+1) + } if ($dirty) { - Write-Output "$(git describe --tags --exact-match)+dirty" + Write-Output "$tag+dirty" } else { - Write-Output "$(git describe --tags --exact-match)" + Write-Output "$tag" } } catch { # Otherwise, take the existing tag, increment the patch version and append the timestamp of the commit and hash @@ -25,6 +30,11 @@ try { $tag="v0.0.0" } + # Remove any sub-module prefixes + if ($tag.LastIndexOf("/") -ne -1) { + $tag=$tag.Substring($tag.LastIndexOf("/")+1) + } + # Remove any pre-release tag if ($tag.LastIndexOf("-") -ne -1) { $tag=$tag.Substring(0, $tag.LastIndexOf("-"))