Merge pull request #49 from reecetech/fix-up-prefix-parsing

Treat tag prefixes as literal strings
This commit is contained in:
Phil Jay
2026-08-10 15:44:15 +10:00
committed by GitHub
2 changed files with 34 additions and 11 deletions

View File

@@ -20,19 +20,13 @@ pcre_old_calver='^(?P<major>0|[1-9]\d*)-0{0,1}(?P<minor>0|[0-9]\d*)-R(?P<patch>0
remove_prefix() { remove_prefix() {
local tag="$1" local tag="$1"
if [[ -n "${tag_prefix:-}" ]]; then if [[ -n "${tag_prefix:-}" ]]; then
# Escape special characters in tag_prefix if [[ "${tag}" == "${tag_prefix}"* ]]; then
local escaped_prefix printf '%s\n' "${tag:${#tag_prefix}}"
escaped_prefix=$(printf '%s\n' "$tag_prefix" | sed 's/[][\/.^$*]/\\&/g') # special chars matched by sed: ] [ / . ^ $ * else
printf '\n'
# shellcheck disable=SC2143
if [[ -z "$(echo "${tag}" | grep "^${tag_prefix}")" ]] ; then
echo ""
return
fi fi
# shellcheck disable=SC2001
echo "${tag}" | sed "s|^${escaped_prefix}||" # Use | as the delimiter to avoid conflicts with /
else else
echo "${tag}" printf '%s\n' "${tag}"
fi fi
} }

View File

@@ -103,6 +103,35 @@ function init_repo {
[[ "$output" = *"CURRENT_V_VERSION=v0.1.2"* ]] [[ "$output" = *"CURRENT_V_VERSION=v0.1.2"* ]]
} }
@test "treats tag_prefix as a literal string" {
init_repo
export tag_prefix="v."
git tag v.1.2.3
git tag v1.9.9
run version-lookup.sh
print_run_info
[ "$status" -eq 0 ] &&
[[ "$output" = *"CURRENT_VERSION=1.2.3"* ]]
}
@test "supports a tag_prefix containing a sed delimiter" {
init_repo
export tag_prefix="foo|bar@"
git tag "foo|bar@1.2.3"
run version-lookup.sh
print_run_info
[ "$status" -eq 0 ] &&
[[ "$output" = *"CURRENT_VERSION=1.2.3"* ]]
}
@test "finds the current normal version even if there's a newer pre-release version" { @test "finds the current normal version even if there's a newer pre-release version" {
init_repo init_repo