Skip to content

Commit

Permalink
Don't auto-add @ to prefix; Shift utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
ps-jay committed Aug 31, 2024
1 parent aa4572b commit d449641
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
28 changes: 18 additions & 10 deletions shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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_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*)$'

##==----------------------------------------------------------------------------
## 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/
Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion tests/test_version-lookup.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions version-increment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([email protected])" 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

Expand Down
19 changes: 2 additions & 17 deletions version-lookup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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="$(
Expand All @@ -49,15 +34,15 @@ 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
current_version="$(
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
Expand Down

0 comments on commit d449641

Please sign in to comment.