From 4afc15f5cdc3498c9340eed826804757dbfdcecd Mon Sep 17 00:00:00 2001 From: David Fluck Date: Mon, 21 Oct 2024 16:21:55 -0400 Subject: [PATCH] build: INFENG-926: Fix version.sh version string output (#10085) This fixes a bug in version.sh that would cause an invalid local version string to be output under certain circumstances; specifically, if a tag were made on a commit that was not on the current branch (i.e. reachable with git-describe). This took a code path in version.sh that did not properly remove any existing tag +metadata with grep, leading to version strings like 0.751.0+dryrun+27a014b44. Note the extra plus sign (+). This causes downstream tools, like helm, to fail. Now, we remove any existing version metadata from the final for all code paths. This also changes all instances of \d character classes to [0-9]. This fixes a cross-platform issue where BSD grep's -E includes \d classes, while GNU grep's -E does not. This means the script worked fine on macOS, but broke subtly in CI (and I suppose on anyone's work machine that uses Linux). We avoid this entirely by falling back to standard character sets. --- version.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/version.sh b/version.sh index 0472da9025a..8de3496d7eb 100755 --- a/version.sh +++ b/version.sh @@ -68,7 +68,7 @@ fi if [[ -z ${VERSION} ]]; then # Check if this branch has any tags (typically, only release branches will # have tags). - MAYBE_TAG=$(git describe --tags --abbrev=0 2>/dev/null | grep -Eo 'v?\d+\.\d+\.\d+') + MAYBE_TAG=$(git describe --tags --abbrev=0 2>/dev/null) SHA=$(git rev-parse --short HEAD) # No tag on current branch. @@ -92,6 +92,16 @@ if [[ -z ${VERSION} ]]; then ) fi + # Filter out additional +metadata from the tag, should it + # exist. This prevents version strings like + # 0.751.0+dryrun+27a014b44. + # Note: we use [0-9] instead of \d here, because while BSD grep's + # -E includes the \d character class, GNU grep's -E does not. This + # means using \d on macOS will work fine, but it will break on + # Linux (and thus, CI). To avoid this, we fall back to standard + # character sets. + MAYBE_TAG=$(grep -Eo 'v?[0-9]+\.[0-9]+\.[0-9]+' <(printf "%s" "$MAYBE_TAG")) + # Munge the tag into the form we want. Note: we always append a SHA hash, # even if we're on the commit with the tag. This is partially because I feel # like it will be more consistent and result in fewer surprises, but also it