diff --git a/shared.sh b/shared.sh index 4f64f5b..838fc12 100644 --- a/shared.sh +++ b/shared.sh @@ -11,13 +11,27 @@ export LC_ALL=C.UTF-8 pcre_semver='^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?: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[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$' pcre_master_ver='^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[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'"${pcre_master_ver:1}"')$' - -pcre_allow_vprefix="^v{0,1}${pcre_allow_prefix:1}" pcre_old_calver='^(?P0|[1-9]\d*)-0{0,1}(?P0|[0-9]\d*)-R(?P0|[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 ## 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 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 diff --git a/tests/test_version-lookup.bats b/tests/test_version-lookup.bats index a614482..d6d5291 100644 --- a/tests/test_version-lookup.bats +++ b/tests/test_version-lookup.bats @@ -133,7 +133,6 @@ function init_repo { [[ "$output" = *"CURRENT_VERSION=0.1.2"* ]] && [[ "$output" = *"CURRENT_V_VERSION=v0.1.2"* ]] } -} @test "returns 0.0.0 if no normal version detected" { init_repo diff --git a/version-increment.sh b/version-increment.sh index a754abe..1915805 100755 --- a/version-increment.sh +++ b/version-increment.sh @@ -15,8 +15,8 @@ fi if [[ -z "${current_version:-}" ]] ; then echo "🛑 Environment variable 'current_version' is unset or empty" 1>&2 input_errors='true' -elif [[ -z "$(echo "${current_version}" | grep_p "${pcre_allow_prefix}")" ]] ; then - echo "🛑 Environment variable 'current_version' is not a valid normal version (M.m.p) or (prefix@M.m.p)" 1>&2 +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)" 1>&2 input_errors='true' fi diff --git a/version-lookup.sh b/version-lookup.sh index a06cd6c..d0e5a94 100755 --- a/version-lookup.sh +++ b/version-lookup.sh @@ -23,21 +23,6 @@ fi ##==---------------------------------------------------------------------------- ## 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 if [[ "${use_api:-}" == 'true' ]] ; then current_version="$( @@ -49,7 +34,7 @@ if [[ "${use_api:-}" == 'true' ]] ; then | jq -r '.[].ref' | sed 's|refs/tags/||g' \ | { grep_p "${pcre_allow_vprefix}" || true; } \ | 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 )" else @@ -57,7 +42,7 @@ else git tag -l \ | { grep_p "${pcre_allow_vprefix}" || true; } \ | 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 )" fi