From 214de86e6e453ee3ef0dbbcbdfaebef5c1a2c7d6 Mon Sep 17 00:00:00 2001 From: Laurent Guillier Date: Thu, 30 Nov 2023 09:31:18 -0500 Subject: [PATCH 1/2] Modernize the script Use bash in shebang use [[ ]] for tests remove 'function' keyword Fix shellcheck errors and warnings --- semver.sh | 64 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/semver.sh b/semver.sh index e5237a4..4b109a6 100755 --- a/semver.sh +++ b/semver.sh @@ -1,18 +1,22 @@ -#!/usr/bin/env sh +#!/bin/bash -function semverParseInto() { +semverParseInto() { local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)' #MAJOR - eval $2=`echo $1 | sed -e "s#$RE#\1#"` + # shellcheck disable=SC2001 + eval "$2"="$(echo "$1" | sed -e "s#$RE#\1#")" #MINOR - eval $3=`echo $1 | sed -e "s#$RE#\2#"` + # shellcheck disable=SC2001 + eval "$3"="$(echo "$1" | sed -e "s#$RE#\2#")" #MINOR - eval $4=`echo $1 | sed -e "s#$RE#\3#"` + # shellcheck disable=SC2001 + eval "$4"="$(echo "$1" | sed -e "s#$RE#\3#")" #SPECIAL - eval $5=`echo $1 | sed -e "s#$RE#\4#"` + # shellcheck disable=SC2001 + eval "$5"="$(echo "$1" | sed -e "s#$RE#\4#")" } -function semverEQ() { +semverEQ() { local MAJOR_A=0 local MINOR_A=0 local PATCH_A=0 @@ -23,18 +27,18 @@ function semverEQ() { local PATCH_B=0 local SPECIAL_B=0 - semverParseInto $1 MAJOR_A MINOR_A PATCH_A SPECIAL_A - semverParseInto $2 MAJOR_B MINOR_B PATCH_B SPECIAL_B + semverParseInto "$1" MAJOR_A MINOR_A PATCH_A SPECIAL_A + semverParseInto "$2" MAJOR_B MINOR_B PATCH_B SPECIAL_B - if [ $MAJOR_A -ne $MAJOR_B ]; then + if [[ "$MAJOR_A" -ne "$MAJOR_B" ]]; then return 1 fi - if [ $MINOR_A -ne $MINOR_B ]; then + if [[ "$MINOR_A" -ne "$MINOR_B" ]]; then return 1 fi - if [ $PATCH_A -ne $PATCH_B ]; then + if [[ "$PATCH_A" -ne "$PATCH_B" ]]; then return 1 fi @@ -47,7 +51,7 @@ function semverEQ() { } -function semverLT() { +semverLT() { local MAJOR_A=0 local MINOR_A=0 local PATCH_A=0 @@ -58,18 +62,18 @@ function semverLT() { local PATCH_B=0 local SPECIAL_B=0 - semverParseInto $1 MAJOR_A MINOR_A PATCH_A SPECIAL_A - semverParseInto $2 MAJOR_B MINOR_B PATCH_B SPECIAL_B + semverParseInto "$1" MAJOR_A MINOR_A PATCH_A SPECIAL_A + semverParseInto "$2" MAJOR_B MINOR_B PATCH_B SPECIAL_B - if [ $MAJOR_A -lt $MAJOR_B ]; then + if [[ "$MAJOR_A" -lt "$MAJOR_B" ]]; then return 0 fi - if [[ $MAJOR_A -le $MAJOR_B && $MINOR_A -lt $MINOR_B ]]; then + if [[ "$MAJOR_A" -le "$MAJOR_B" && "$MINOR_A" -lt "$MINOR_B" ]]; then return 0 fi - if [[ $MAJOR_A -le $MAJOR_B && $MINOR_A -le $MINOR_B && $PATCH_A -lt $PATCH_B ]]; then + if [[ "$MAJOR_A" -le "$MAJOR_B" && "$MINOR_A" -le "$MINOR_B" && "$PATCH_A" -lt "$PATCH_B" ]]; then return 0 fi @@ -91,40 +95,40 @@ function semverLT() { } -function semverGT() { - semverEQ $1 $2 - local EQ=$? +semverGT() { + semverEQ "$1" "$2" + local EQ="$?" - semverLT $1 $2 - local LT=$? + semverLT "$1" "$2" + local LT="$?" - if [ $EQ -ne 0 ] && [ $LT -ne 0 ]; then + if [[ "$EQ" -ne 0 ]] && [[ "$LT" -ne 0 ]]; then return 0 else return 1 fi } -if [ "___semver.sh" == "___`basename $0`" ]; then +if [[ "___semver.sh" == "___$(basename "$0")" ]]; then MAJOR=0 MINOR=0 PATCH=0 SPECIAL="" -semverParseInto $1 MAJOR MINOR PATCH SPECIAL +semverParseInto "$1" MAJOR MINOR PATCH SPECIAL echo "$1 -> M: $MAJOR m:$MINOR p:$PATCH s:$SPECIAL" -semverParseInto $2 MAJOR MINOR PATCH SPECIAL +semverParseInto "$2" MAJOR MINOR PATCH SPECIAL echo "$2 -> M: $MAJOR m:$MINOR p:$PATCH s:$SPECIAL" -semverEQ $1 $2 +semverEQ "$1" "$2" echo "$1 == $2 -> $?." -semverLT $1 $2 +semverLT "$1" "$2" echo "$1 < $2 -> $?." -semverGT $1 $2 +semverGT "$1" "$2" echo "$1 > $2 -> $?." fi From d834761edd7de74f86940a2d1fe0fd1ba846e740 Mon Sep 17 00:00:00 2001 From: Laurent Guillier Date: Thu, 30 Nov 2023 09:39:44 -0500 Subject: [PATCH 2/2] Fix bug in semverLT There was a bug in semverLT(), when both versions have a special field, semverLT would just compare that special field if majorA > majorB Before fix: ./semver.sh 8.4.2-12 6.90.5-13 8.4.2-12 -> M: 8 m:4 p:2 s:-12 6.90.5-13 -> M: 6 m:90 p:5 s:-13 8.4.2-12 == 6.90.5-13 -> 1. 8.4.2-12 < 6.90.5-13 -> 0. <- Offending result! 8.4.2-12 > 6.90.5-13 -> 1. <- Side effect because GT uses LT After fix: ./semver.sh 8.4.2-12 6.90.5-13 8.4.2-12 -> M: 8 m:4 p:2 s:-12 6.90.5-13 -> M: 6 m:90 p:5 s:-13 8.4.2-12 == 6.90.5-13 -> 1. 8.4.2-12 < 6.90.5-13 -> 1. <- Correct 8.4.2-12 > 6.90.5-13 -> 0. --- semver.sh | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/semver.sh b/semver.sh index 4b109a6..b43f7d6 100755 --- a/semver.sh +++ b/semver.sh @@ -69,14 +69,26 @@ semverLT() { return 0 fi - if [[ "$MAJOR_A" -le "$MAJOR_B" && "$MINOR_A" -lt "$MINOR_B" ]]; then + if [[ "$MAJOR_A" -gt "$MAJOR_B" ]]; then + return 1 + fi + + if [[ "$MINOR_A" -lt "$MINOR_B" ]]; then return 0 fi + + if [[ "$MINOR_A" -gt "$MINOR_B" ]]; then + return 1 + fi - if [[ "$MAJOR_A" -le "$MAJOR_B" && "$MINOR_A" -le "$MINOR_B" && "$PATCH_A" -lt "$PATCH_B" ]]; then + if [[ "$PATCH_A" -lt "$PATCH_B" ]]; then return 0 fi + if [[ "$PATCH_A" -gt "$PATCH_B" ]]; then + return 1 + fi + if [[ "_$SPECIAL_A" == "_" ]] && [[ "_$SPECIAL_B" == "_" ]] ; then return 1 fi