Don't auto-add @ to prefix; Shift utility function

This commit is contained in:
Phil Jay
2024-08-31 13:49:15 +10:00
parent aa4572b10f
commit d4496413d6
4 changed files with 22 additions and 30 deletions

View File

@@ -11,13 +11,27 @@ export LC_ALL=C.UTF-8
pcre_semver='^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$' pcre_semver='^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
pcre_master_ver='^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)$' pcre_master_ver='^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)$'
pcre_allow_vprefix="^v{0,1}${pcre_master_ver:1}"
# Extended regex to allow arbitrary prefixes before the semver
pcre_allow_prefix='^.*(?P<semver>'"${pcre_master_ver:1}"')$'
pcre_allow_vprefix="^v{0,1}${pcre_allow_prefix:1}"
pcre_old_calver='^(?P<major>0|[1-9]\d*)-0{0,1}(?P<minor>0|[0-9]\d*)-R(?P<patch>0|[1-9]\d*)$' pcre_old_calver='^(?P<major>0|[1-9]\d*)-0{0,1}(?P<minor>0|[0-9]\d*)-R(?P<patch>0|[1-9]\d*)$'
##==----------------------------------------------------------------------------
## Utility function for removing tag_prefix if present
remove_prefix() {
local tag="$1"
if [[ -n "${tag_prefix:-}" ]]; then
# Escape special characters in tag_prefix
local escaped_prefix
escaped_prefix=$(printf '%s\n' "$tag_prefix" | sed 's/[][\/.^$*]/\\&/g')
# Use | as the delimiter to avoid conflicts with /
echo "${tag}" | sed "s|^${escaped_prefix}||"
else
echo "${tag}"
fi
}
##==---------------------------------------------------------------------------- ##==----------------------------------------------------------------------------
## Conventional commit regexes ## Conventional commit regexes
## see: https://www.conventionalcommits.org/en/v1.0.0/ ## see: https://www.conventionalcommits.org/en/v1.0.0/
@@ -58,12 +72,6 @@ fi
# Check if the tag_prefix is set, and if not, set it to an empty string # Check if the tag_prefix is set, and if not, set it to an empty string
tag_prefix="${tag_prefix:-}" tag_prefix="${tag_prefix:-}"
# Add a trailing @ to tag_prefix if it doesn't already end with one
if [[ -n "$tag_prefix" && "${tag_prefix: -1}" != "@" ]]; then
tag_prefix="${tag_prefix}@"
fi
##==---------------------------------------------------------------------------- ##==----------------------------------------------------------------------------
## MacOS compatibility ## MacOS compatibility

View File

@@ -133,7 +133,6 @@ function init_repo {
[[ "$output" = *"CURRENT_VERSION=0.1.2"* ]] && [[ "$output" = *"CURRENT_VERSION=0.1.2"* ]] &&
[[ "$output" = *"CURRENT_V_VERSION=v0.1.2"* ]] [[ "$output" = *"CURRENT_V_VERSION=v0.1.2"* ]]
} }
}
@test "returns 0.0.0 if no normal version detected" { @test "returns 0.0.0 if no normal version detected" {
init_repo init_repo

View File

@@ -15,8 +15,8 @@ fi
if [[ -z "${current_version:-}" ]] ; then if [[ -z "${current_version:-}" ]] ; then
echo "🛑 Environment variable 'current_version' is unset or empty" 1>&2 echo "🛑 Environment variable 'current_version' is unset or empty" 1>&2
input_errors='true' input_errors='true'
elif [[ -z "$(echo "${current_version}" | grep_p "${pcre_allow_prefix}")" ]] ; then elif [[ -z "$(echo "${current_version}" | grep_p "${pcre_master_ver}")" ]] ; then
echo "🛑 Environment variable 'current_version' is not a valid normal version (M.m.p) or (prefix@M.m.p)" 1>&2 echo "🛑 Environment variable 'current_version' is not a valid normal version (M.m.p)" 1>&2
input_errors='true' input_errors='true'
fi fi

View File

@@ -23,21 +23,6 @@ fi
##==---------------------------------------------------------------------------- ##==----------------------------------------------------------------------------
## Version parsing ## Version parsing
# Function to extract the semver part from the tag
extract_semver() {
local tag="$1"
if [[ -n "${tag_prefix:-}" ]]; then
# Escape special characters in tag_prefix
local escaped_prefix
escaped_prefix=$(printf '%s\n' "$tag_prefix" | sed 's/[][\/.^$*]/\\&/g')
# Use | as the delimiter to avoid conflicts with /
echo "${tag}" | sed "s|^${escaped_prefix}||"
else
echo "${tag}"
fi
}
# detect current version - removing "v" from start of tag if it exists # detect current version - removing "v" from start of tag if it exists
if [[ "${use_api:-}" == 'true' ]] ; then if [[ "${use_api:-}" == 'true' ]] ; then
current_version="$( current_version="$(
@@ -49,7 +34,7 @@ if [[ "${use_api:-}" == 'true' ]] ; then
| jq -r '.[].ref' | sed 's|refs/tags/||g' \ | jq -r '.[].ref' | sed 's|refs/tags/||g' \
| { grep_p "${pcre_allow_vprefix}" || true; } \ | { grep_p "${pcre_allow_vprefix}" || true; } \
| sed 's/^v//g' \ | sed 's/^v//g' \
| while read -r tag; do extract_semver "$tag"; done \ | while read -r tag; do remove_prefix "$tag"; done \
| sort -V | tail -n 1 | sort -V | tail -n 1
)" )"
else else
@@ -57,7 +42,7 @@ else
git tag -l \ git tag -l \
| { grep_p "${pcre_allow_vprefix}" || true; } \ | { grep_p "${pcre_allow_vprefix}" || true; } \
| sed 's/^v//g' \ | sed 's/^v//g' \
| while read -r tag; do extract_semver "$tag"; done \ | while read -r tag; do remove_prefix "$tag"; done \
| sort -V | tail -n 1 | sort -V | tail -n 1
)" )"
fi fi