From 2b582cf98ddc04266e9172e193cca134bf40e53a Mon Sep 17 00:00:00 2001 From: Friedrich Wilken Date: Fri, 12 Jan 2024 14:08:46 +0100 Subject: [PATCH 1/4] add script to get version number from a git branch --- hack/scripts/get-version-from-branch.sh | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 hack/scripts/get-version-from-branch.sh diff --git a/hack/scripts/get-version-from-branch.sh b/hack/scripts/get-version-from-branch.sh new file mode 100644 index 0000000..0fd4e17 --- /dev/null +++ b/hack/scripts/get-version-from-branch.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +# This script will generate the version. +# +# First it verifies, that the current branch name is 'release-x.y', +# where x and y are multi-digit integers. +# It further looks into the existing tags, looking for ones that start with x.y. +# If there is none, it will return x.y.0. Otherwise it will return x.y.z where z +# is the highest existing value increase by one. + +# Get the current branch name. +current_branch=$(git rev-parse --abbrev-ref HEAD) + +# Check if the current branch is a release branch. +if [[ $current_branch =~ ^release-([0-9]+)\.([0-9]+)$ ]]; then + # Extract x and y from the branch name. BASH_REMATCH is an array variable + # automatically generated by pattern matching ([[ ... ]]). + x=${BASH_REMATCH[1]} + y=${BASH_REMATCH[2]} + + # Find the highest z value for the matching tags. + highest_z=$(git tag -l "$x.$y.*" | cut -d '.' -f 3 | sort -n | tail -n 1) + + # Increment the highest z value by 1 or set to 0 if no matching tags are found. + if [ -z "$highest_z" ]; then + next_z=0 + else + next_z=$((highest_z + 1)) + fi + + # Return the new version. + new_version="${x}.${y}.${next_z}" + echo "${new_version}" + export NEW_VERSION=$new_version + exit 0 +else + echo "Not on a release branch." + exit 1 +fi From efaad69725cb9d5b5913f552cb6a1135bed18221 Mon Sep 17 00:00:00 2001 From: Friedrich Wilken Date: Fri, 12 Jan 2024 14:54:20 +0100 Subject: [PATCH 2/4] add examples --- hack/scripts/get-version-from-branch.sh | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/hack/scripts/get-version-from-branch.sh b/hack/scripts/get-version-from-branch.sh index 0fd4e17..fb4499f 100644 --- a/hack/scripts/get-version-from-branch.sh +++ b/hack/scripts/get-version-from-branch.sh @@ -7,6 +7,27 @@ # It further looks into the existing tags, looking for ones that start with x.y. # If there is none, it will return x.y.0. Otherwise it will return x.y.z where z # is the highest existing value increase by one. +# +# Examples: +# 1. The current git branch is 'release-1.0' and git has the tags '1.0.1', '1.0.2', 1.0.3': +# +# $ ./get-version-from-branch.sh +# 1.0.4 +# +# 2. The current branch is 'release-1.1' and git has no tags that start with '1.1': +# +# $ ./get-version-from-branch.sh +# 1.0.0 +# +# 3. The current branch is 'main': +# $ ./get-version-from-branch.sh +# Not on a release branch. +# +# Please note that this will exit with an error: +# +# ./get-version-from-branch.sh || echo "exit with error" +# Not on a release branch. +# exit with error # Get the current branch name. current_branch=$(git rev-parse --abbrev-ref HEAD) @@ -36,4 +57,4 @@ if [[ $current_branch =~ ^release-([0-9]+)\.([0-9]+)$ ]]; then else echo "Not on a release branch." exit 1 -fi +fi6 From 705ffda7459d55b8174b4ee2fefd1440e3691a58 Mon Sep 17 00:00:00 2001 From: Friedrich Wilken Date: Fri, 12 Jan 2024 15:03:06 +0100 Subject: [PATCH 3/4] format --- hack/scripts/get-version-from-branch.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/hack/scripts/get-version-from-branch.sh b/hack/scripts/get-version-from-branch.sh index fb4499f..cb5fbb7 100644 --- a/hack/scripts/get-version-from-branch.sh +++ b/hack/scripts/get-version-from-branch.sh @@ -10,12 +10,10 @@ # # Examples: # 1. The current git branch is 'release-1.0' and git has the tags '1.0.1', '1.0.2', 1.0.3': -# # $ ./get-version-from-branch.sh # 1.0.4 # # 2. The current branch is 'release-1.1' and git has no tags that start with '1.1': -# # $ ./get-version-from-branch.sh # 1.0.0 # @@ -24,7 +22,6 @@ # Not on a release branch. # # Please note that this will exit with an error: -# # ./get-version-from-branch.sh || echo "exit with error" # Not on a release branch. # exit with error From 53a2a525a139907f416e4cf8a8439f1ab918e81f Mon Sep 17 00:00:00 2001 From: Friedrich Wilken Date: Fri, 12 Jan 2024 15:18:13 +0100 Subject: [PATCH 4/4] fix line ending --- hack/scripts/get-version-from-branch.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hack/scripts/get-version-from-branch.sh b/hack/scripts/get-version-from-branch.sh index cb5fbb7..244411d 100644 --- a/hack/scripts/get-version-from-branch.sh +++ b/hack/scripts/get-version-from-branch.sh @@ -25,6 +25,7 @@ # ./get-version-from-branch.sh || echo "exit with error" # Not on a release branch. # exit with error +# # Get the current branch name. current_branch=$(git rev-parse --abbrev-ref HEAD) @@ -49,9 +50,8 @@ if [[ $current_branch =~ ^release-([0-9]+)\.([0-9]+)$ ]]; then # Return the new version. new_version="${x}.${y}.${next_z}" echo "${new_version}" - export NEW_VERSION=$new_version exit 0 else echo "Not on a release branch." exit 1 -fi6 +fi