From 7b4e8805da947cba1f444481203d431e370351cc Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Sun, 29 Dec 2013 16:20:28 +0100 Subject: [PATCH 1/9] Properly match special part, separated by a '-' --- semver.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semver.sh b/semver.sh index e5237a4..31d37ea 100755 --- a/semver.sh +++ b/semver.sh @@ -1,7 +1,7 @@ #!/usr/bin/env sh function semverParseInto() { - local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)' + local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)[-]\{0,1\}\([0-9A-Za-z-]*\)' #MAJOR eval $2=`echo $1 | sed -e "s#$RE#\1#"` #MINOR From 202a6d25e3f8d684280259710c494e1cb0e4aa1e Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Sun, 29 Dec 2013 16:23:34 +0100 Subject: [PATCH 2/9] Add bumping major/minor/patch functions --- semver.sh | 46 +++++++++++++++++++++++++++++++++++++++++++++- semver_test.sh | 25 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/semver.sh b/semver.sh index 31d37ea..6f83d02 100755 --- a/semver.sh +++ b/semver.sh @@ -12,6 +12,14 @@ function semverParseInto() { eval $5=`echo $1 | sed -e "s#$RE#\4#"` } +function semverConstruct() { + if [ "$5" != "" ]; then + eval $5=`echo "$1.$2.$3-$4"` + fi + + eval $4=`echo "$1.$2.$3"` +} + function semverEQ() { local MAJOR_A=0 local MINOR_A=0 @@ -68,7 +76,7 @@ function semverLT() { 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 return 0 fi @@ -105,6 +113,42 @@ function semverGT() { fi } +function semverBumpMajor() { + local MAJOR=0 + local MINOR=0 + local PATCH=0 + local SPECIAL=0 + + semverParseInto $1 MAJOR MINOR PATCH SPECIAL + MAJOR=$(($MAJOR + 1)) + + semverConstruct $MAJOR $MINOR $PATCH $SPECIAL $2 +} + +function semverBumpMinor() { + local MAJOR=0 + local MINOR=0 + local PATCH=0 + local SPECIAL=0 + + semverParseInto $1 MAJOR MINOR PATCH SPECIAL + MINOR=$(($MINOR + 1)) + + semverConstruct $MAJOR $MINOR $PATCH $SPECIAL $2 +} + +function semverBumpPatch() { + local MAJOR=0 + local MINOR=0 + local PATCH=0 + local SPECIAL=0 + + semverParseInto $1 MAJOR MINOR PATCH SPECIAL + PATCH=$(($PATCH + 1)) + + semverConstruct $MAJOR $MINOR $PATCH $SPECIAL $2 +} + if [ "___semver.sh" == "___`basename $0`" ]; then MAJOR=0 diff --git a/semver_test.sh b/semver_test.sh index a0ff994..e91ff9f 100755 --- a/semver_test.sh +++ b/semver_test.sh @@ -15,6 +15,7 @@ local MAJOR=0 local MINOR=0 local PATCH=0 local SPECIAL="" +local VERSION="" semverParseInto $A MAJOR MINOR PATCH SPECIAL echo "$A -> M:$MAJOR m:$MINOR p:$PATCH s:$SPECIAL. Expect M:1 m:3 p:2 s:" @@ -146,6 +147,30 @@ echo "$G < $A -> $?. Expect 0." semverGT $G $A echo "$G > $A -> $?. Expect 1." + + +echo "Bumping major" +semverBumpMajor $A VERSION +echo "$A -> $VERSION. Expect 2.3.2." + +semverBumpMajor $E VERSION +echo "$A -> $VERSION. Expect 2.3.2-a." + + +echo "Bumping minor" +semverBumpMinor $A VERSION +echo "$A -> $VERSION. Expect 1.4.2." + +semverBumpMinor $E VERSION +echo "$A -> $VERSION. Expect 1.4.2-a." + + +echo "Bumping patch" +semverBumpPatch $A VERSION +echo "$A -> $VERSION. Expect 1.3.3." + +semverBumpPatch $E VERSION +echo "$A -> $VERSION. Expect 1.3.3-a." } semverTest From d832d05251b1d1ad31b4443d7a6fd1293ce24fff Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Sun, 29 Dec 2013 16:37:11 +0100 Subject: [PATCH 3/9] Make semver.sh usable as a command --- semver.sh | 54 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/semver.sh b/semver.sh index 6f83d02..a0cae31 100755 --- a/semver.sh +++ b/semver.sh @@ -150,25 +150,49 @@ function semverBumpPatch() { } if [ "___semver.sh" == "___`basename $0`" ]; then + if [ "$2" == "" ]; then + echo "$0 [version]" + echo "Commands: eq, lt, gt, bump_major, bump_minor, bump_patch" + echo "" + echo "eq: compares left version against right version, returns 0 if both versions are equal" + echo "lt: compares left version against right version, returns 0 if left version is less than right version" + echo "gt: compares left version against right version, returns 0 if left version is greater than than right version" + echo "bump_major: bumps major of version" + echo "bump_minor: bumps minor of version" + echo "bump_patch: bumps patch of version" + exit 255 + fi -MAJOR=0 -MINOR=0 -PATCH=0 -SPECIAL="" - -semverParseInto $1 MAJOR MINOR PATCH SPECIAL -echo "$1 -> M: $MAJOR m:$MINOR p:$PATCH s:$SPECIAL" + if [ "$2" == "eq" ]; then + semverEQ $1 $3 + exit $? + fi -semverParseInto $2 MAJOR MINOR PATCH SPECIAL -echo "$2 -> M: $MAJOR m:$MINOR p:$PATCH s:$SPECIAL" + if [ "$2" == "lt" ]; then + semverLT $1 $3 + exit $? + fi -semverEQ $1 $2 -echo "$1 == $2 -> $?." + if [ "$2" == "gt" ]; then + semverGT $1 $3 + echo $? + fi -semverLT $1 $2 -echo "$1 < $2 -> $?." + if [ "$2" == "bump_major" ]; then + semverBumpMajor $1 VERSION + echo ${VERSION} + exit 0 + fi -semverGT $1 $2 -echo "$1 > $2 -> $?." + if [ "$2" == "bump_minor" ]; then + semverBumpMinor $1 VERSION + echo ${VERSION} + exit 0 + fi + if [ "$2" == "bump_patch" ]; then + semverBumpPatch $1 VERSION + echo ${VERSION} + exit 0 + fi fi From 56a5468dee7630474d051a474b83ec1f704c39c4 Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Mon, 30 Dec 2013 00:03:24 +0100 Subject: [PATCH 4/9] Rewrite of tests and add semverCmp command --- semver.sh | 114 ++++++++++++++++++++------------------ semver_test.sh | 145 +++++++++++++++++++++++++++++++------------------ 2 files changed, 154 insertions(+), 105 deletions(-) diff --git a/semver.sh b/semver.sh index a0cae31..2fe56c0 100755 --- a/semver.sh +++ b/semver.sh @@ -20,7 +20,7 @@ function semverConstruct() { eval $4=`echo "$1.$2.$3"` } -function semverEQ() { +function semverCmp() { local MAJOR_A=0 local MINOR_A=0 local PATCH_A=0 @@ -34,93 +34,94 @@ function semverEQ() { 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 - return 1 + # major + if [ $MAJOR_A -lt $MAJOR_B ]; then + return -1 fi - if [ $MINOR_A -ne $MINOR_B ]; then + if [ $MAJOR_A -gt $MAJOR_B ]; then return 1 fi - if [ $PATCH_A -ne $PATCH_B ]; then - return 1 + # minor + if [ $MINOR_A -lt $MINOR_B ]; then + return -1 fi - if [[ "_$SPECIAL_A" != "_$SPECIAL_B" ]]; then + if [ $MINOR_A -gt $MINOR_B ]; then return 1 fi - - return 0 - -} - -function semverLT() { - local MAJOR_A=0 - local MINOR_A=0 - local PATCH_A=0 - local SPECIAL_A=0 - - local MAJOR_B=0 - local MINOR_B=0 - 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 - - if [ $MAJOR_A -lt $MAJOR_B ]; then - return 0 + # patch + if [ $PATCH_A -lt $PATCH_B ]; then + return -1 fi - if [[ $MAJOR_A -le $MAJOR_B && $MINOR_A -lt $MINOR_B ]]; then - return 0 + if [ $PATCH_A -gt $PATCH_B ]; then + return 1 fi - if [[ $MAJOR_A -le $MAJOR_B && $MINOR_A -le $MINOR_B && $PATCH_A -lt $PATCH_B ]]; then - return 0 + # special + if [[ "$SPECIAL_A" < "$SPECIAL_B" ]]; then + return -1 fi - if [[ "_$SPECIAL_A" == "_" ]] && [[ "_$SPECIAL_B" == "_" ]] ; then + if [[ "$SPECIAL_A" > "$SPECIAL_B" ]]; then return 1 fi - if [[ "_$SPECIAL_A" == "_" ]] && [[ "_$SPECIAL_B" != "_" ]] ; then + + # equal + return 0 +} + +function semverEQ() { + semverCmp $1 $2 + local RESULT=$? + + if [ $RESULT -ne 0 ]; then + # not equal return 1 fi - if [[ "_$SPECIAL_A" != "_" ]] && [[ "_$SPECIAL_B" == "_" ]] ; then - return 0 - fi - if [[ "_$SPECIAL_A" < "_$SPECIAL_B" ]]; then - return 0 - fi + return 0 +} + +function semverLT() { + semverCmp $1 $2 + local RESULT=$? - return 1 + # XXX: compare to 255, as returning -1 becomes return value 255 + if [ $RESULT -ne 255 ]; then + # not lesser than + return 1 + fi + return 0 } function semverGT() { - semverEQ $1 $2 - local EQ=$? - - semverLT $1 $2 - local LT=$? + semverCmp $1 $2 + local RESULT=$? - if [ $EQ -ne 0 ] && [ $LT -ne 0 ]; then - return 0 - else + if [ $RESULT -ne 1 ]; then + # not greater than return 1 fi + + return 0 } function semverBumpMajor() { local MAJOR=0 local MINOR=0 local PATCH=0 - local SPECIAL=0 + local SPECIAL="" semverParseInto $1 MAJOR MINOR PATCH SPECIAL MAJOR=$(($MAJOR + 1)) + MINOR=0 + PATCH=0 + SPECIAL="" semverConstruct $MAJOR $MINOR $PATCH $SPECIAL $2 } @@ -129,10 +130,12 @@ function semverBumpMinor() { local MAJOR=0 local MINOR=0 local PATCH=0 - local SPECIAL=0 + local SPECIAL="" semverParseInto $1 MAJOR MINOR PATCH SPECIAL MINOR=$(($MINOR + 1)) + PATCH=0 + SPECIAL="" semverConstruct $MAJOR $MINOR $PATCH $SPECIAL $2 } @@ -145,6 +148,7 @@ function semverBumpPatch() { semverParseInto $1 MAJOR MINOR PATCH SPECIAL PATCH=$(($PATCH + 1)) + SPECIAL="" semverConstruct $MAJOR $MINOR $PATCH $SPECIAL $2 } @@ -152,8 +156,9 @@ function semverBumpPatch() { if [ "___semver.sh" == "___`basename $0`" ]; then if [ "$2" == "" ]; then echo "$0 [version]" - echo "Commands: eq, lt, gt, bump_major, bump_minor, bump_patch" + echo "Commands: cmp, eq, lt, gt, bump_major, bump_minor, bump_patch" echo "" + echo "cmp: compares left version against right version, return 0 if equal, 255 (-1) if left is lower than right, 1 if left is higher than right" echo "eq: compares left version against right version, returns 0 if both versions are equal" echo "lt: compares left version against right version, returns 0 if left version is less than right version" echo "gt: compares left version against right version, returns 0 if left version is greater than than right version" @@ -163,6 +168,11 @@ if [ "___semver.sh" == "___`basename $0`" ]; then exit 255 fi + if [ "$2" == "cmp" ]; then + semverCmp $1 $3 + exit $? + fi + if [ "$2" == "eq" ]; then semverEQ $1 $3 exit $? diff --git a/semver_test.sh b/semver_test.sh index e91ff9f..a23d42b 100755 --- a/semver_test.sh +++ b/semver_test.sh @@ -2,6 +2,20 @@ . ./semver.sh +function doTest() { + local TEST=$1 + local EXPECTED=$2 + local ACTUAL=$3 + + echo -n "$TEST: " + + if [[ "$EXPECTED" == "$ACTUAL" ]]; then + echo "passed" + else + echo "FAILED, expected '${EXPECTED}', actual: '${ACTUAL}'" + fi +} + semverTest() { local A=R1.3.2 local B=R2.3.2 @@ -10,6 +24,7 @@ local D=R1.3.3 local E=R1.3.2a local F=R1.3.2b local G=R1.2.3 +local H=1.2.3-a local MAJOR=0 local MINOR=0 @@ -17,160 +32,184 @@ local PATCH=0 local SPECIAL="" local VERSION="" +echo "Parsing" semverParseInto $A MAJOR MINOR PATCH SPECIAL -echo "$A -> M:$MAJOR m:$MINOR p:$PATCH s:$SPECIAL. Expect M:1 m:3 p:2 s:" +doTest "semverParseInto $A -> M m p s" "M:1 m:3 p:2 s:" "M:$MAJOR m:$MINOR p:$PATCH s:$SPECIAL" + +semverParseInto $B MAJOR MINOR PATCH SPECIAL +doTest "semverParseInto $B -> M m p s" "M:2 m:3 p:2 s:" "M:$MAJOR m:$MINOR p:$PATCH s:$SPECIAL" + semverParseInto $E MAJOR MINOR PATCH SPECIAL -echo "$E -> M:$MAJOR m:$MINOR p:$PATCH s:$SPECIAL. Expect M:1 m:3 p:2 s:a" +doTest "semverParseInto $E -> M m p s" "M:1 m:3 p:2 s:a" "M:$MAJOR m:$MINOR p:$PATCH s:$SPECIAL" + +semverParseInto $H MAJOR MINOR PATCH SPECIAL +doTest "semverParseInto $H -> M m p s" "M:1 m:2 p:3 s:a" "M:$MAJOR m:$MINOR p:$PATCH s:$SPECIAL" + + +echo "Comparisons" +semverCmp $A $A +doTest "semverCmp $A $A" 0 $? -echo "Equality comparisions" +semverCmp $A $B +doTest "semverCmp $A $B" 255 $? + +semverCmp $B $A +doTest "semverCmp $B $A" 1 $? + + +echo "Equality comparisons" semverEQ $A $A -echo "$A == $A -> $?. Expect 0." +doTest "semverEQ $A $A" 0 $? semverLT $A $A -echo "$A < $A -> $?. Expect 1." +doTest "semverLT $A $A" 1 $? semverGT $A $A -echo "$A > $A -> $?. Expect 1." +doTest "semverGT $A $A" 1 $? -echo "Major number comparisions" +echo "Major number comparisons" semverEQ $A $B -echo "$A == $B -> $?. Expect 1." +doTest "semverEQ $A $B" 1 $? semverLT $A $B -echo "$A < $B -> $?. Expect 0." +doTest "semverLT $A $B" 0 $? semverGT $A $B -echo "$A > $B -> $?. Expect 1." +doTest "semverGT $A $B" 1 $? semverEQ $B $A -echo "$B == $A -> $?. Expect 1." +doTest "semverEQ $B $A" 1 $? semverLT $B $A -echo "$B < $A -> $?. Expect 1." +doTest "semverLT $B $A" 1 $? semverGT $B $A -echo "$B > $A -> $?. Expect 0." +doTest "semverGT $B $A" 0 $? -echo "Minor number comparisions" +echo "Minor number comparisons" semverEQ $A $C -echo "$A == $C -> $?. Expect 1." +doTest "semverEQ $A $C" 1 $? semverLT $A $C -echo "$A < $C -> $?. Expect 0." +doTest "semverLT $A $C" 0 $? semverGT $A $C -echo "$A > $C -> $?. Expect 1." +doTest "semverGT $A $C" 1 $? semverEQ $C $A -echo "$C == $A -> $?. Expect 1." +doTest "semverEQ $C $A" 1 $? semverLT $C $A -echo "$C < $A -> $?. Expect 1." +doTest "semverLT $C $A" 1 $? semverGT $C $A -echo "$C > $A -> $?. Expect 0." +doTest "semverGT $C $A" 0 $? + -echo "patch number comparisions" +echo "Patch number comparisons" semverEQ $A $D -echo "$A == $D -> $?. Expect 1." +doTest "semverEQ $A $D" 1 $? semverLT $A $D -echo "$A < $D -> $?. Expect 0." +doTest "semverLT $A $D" 0 $? semverGT $A $D -echo "$A > $D -> $?. Expect 1." +doTest "semverGT $A $D" 1 $? semverEQ $D $A -echo "$D == $A -> $?. Expect 1." +doTest "semverEQ $D $A" 1 $? semverLT $D $A -echo "$D < $A -> $?. Expect 1." +doTest "semverLT $D $A" 1 $? semverGT $D $A -echo "$D > $A -> $?. Expect 0." +doTest "semverGT $D $A" 0 $? -echo "special section vs no special comparisions" + +echo "Special section vs no special comparisons" semverEQ $A $E -echo "$A == $E -> $?. Expect 1." +doTest "semverEQ $A $E" 1 $? semverLT $A $E -echo "$A < $E -> $?. Expect 1." +doTest "semverLT $A $E" 1 $? semverGT $A $E -echo "$A > $E -> $?. Expect 0." +doTest "semverGT $A $E" 0 $? semverEQ $E $A -echo "$E == $A -> $?. Expect 1." +doTest "semverEQ $E $A" 1 $? semverLT $E $A -echo "$E < $A -> $?. Expect 0." +doTest "semverLT $E $A" 0 $? semverGT $E $A -echo "$E > $A -> $?. Expect 1." +doTest "semverGT $E $A" 1 $? + -echo "special section vs special comparisions" +echo "Special section vs special comparisons" semverEQ $E $F -echo "$E == $F -> $?. Expect 1." +doTest "semverEQ $E $F" 1 $? semverLT $E $F -echo "$E < $F -> $?. Expect 0." +doTest "semverLT $E $F" 0 $? semverGT $E $F -echo "$E > $F -> $?. Expect 1." +doTest "semverLT $E $F" 1 $? semverEQ $F $E -echo "$F == $E -> $?. Expect 1." +doTest "semverEQ $F $E" 1 $? semverLT $F $E -echo "$F < $E -> $?. Expect 1." +doTest "semverLT $F $E" 1 $? semverGT $F $E -echo "$F > $E -> $?. Expect 0." +doTest "semverGT $F $E" 0 $? + echo "Minor and patch number comparisons" semverEQ $A $G -echo "$A == $G -> $?. Expect 1." +doTest "semverEQ $A $G" 1 $? semverLT $A $G -echo "$A < $G -> $?. Expect 1." +doTest "semverLT $A $G" 1 $? semverGT $A $G -echo "$A > $G -> $?. Expect 0." +doTest "semverGT $A $G" 0 $? semverEQ $G $A -echo "$G == $A -> $?. Expect 1." +doTest "semverEQ $G $A" 1 $? semverLT $G $A -echo "$G < $A -> $?. Expect 0." +doTest "semverLT $G $A" 0 $? semverGT $G $A -echo "$G > $A -> $?. Expect 1." +doTest "semverGT $G $A" 1 $? echo "Bumping major" semverBumpMajor $A VERSION -echo "$A -> $VERSION. Expect 2.3.2." +doTest "semverBumpMajor $A" "2.0.0" $VERSION semverBumpMajor $E VERSION -echo "$A -> $VERSION. Expect 2.3.2-a." +doTest "semverBumpMajor $E" "2.0.0" $VERSION echo "Bumping minor" semverBumpMinor $A VERSION -echo "$A -> $VERSION. Expect 1.4.2." +doTest "semverBumpMinor $A" "1.4.0" $VERSION semverBumpMinor $E VERSION -echo "$A -> $VERSION. Expect 1.4.2-a." +doTest "semverBumpMinor $E" "1.4.0" $VERSION echo "Bumping patch" semverBumpPatch $A VERSION -echo "$A -> $VERSION. Expect 1.3.3." +doTest "semverBumpPatch $A" "1.3.3" $VERSION semverBumpPatch $E VERSION -echo "$A -> $VERSION. Expect 1.3.3-a." +doTest "semverBumpPatch $E" "1.3.3" $VERSION } semverTest From b5e632181fb11ebe4bbf9149cf1e15285c118640 Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Mon, 30 Dec 2013 00:05:34 +0100 Subject: [PATCH 5/9] Also show result of semverLT, semverEQ, semverGT --- semver.sh | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/semver.sh b/semver.sh index 2fe56c0..ce884a8 100755 --- a/semver.sh +++ b/semver.sh @@ -170,22 +170,30 @@ if [ "___semver.sh" == "___`basename $0`" ]; then if [ "$2" == "cmp" ]; then semverCmp $1 $3 - exit $? + RESULT=$? + echo $RESULT + exit $RESULT fi if [ "$2" == "eq" ]; then semverEQ $1 $3 - exit $? + RESULT=$? + echo $RESULT + exit $RESULT fi if [ "$2" == "lt" ]; then semverLT $1 $3 - exit $? + RESULT=$? + echo $RESULT + exit $RESULT fi if [ "$2" == "gt" ]; then semverGT $1 $3 - echo $? + RESULT=$? + echo $RESULT + exit $RESULT fi if [ "$2" == "bump_major" ]; then From 411e62683a934c30ec94c1204aae36b3350aa0b7 Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Mon, 30 Dec 2013 00:12:14 +0100 Subject: [PATCH 6/9] More clear documentation what bump_major, bump_minor, bump_patch does --- semver.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/semver.sh b/semver.sh index ce884a8..99f9480 100755 --- a/semver.sh +++ b/semver.sh @@ -162,9 +162,10 @@ if [ "___semver.sh" == "___`basename $0`" ]; then echo "eq: compares left version against right version, returns 0 if both versions are equal" echo "lt: compares left version against right version, returns 0 if left version is less than right version" echo "gt: compares left version against right version, returns 0 if left version is greater than than right version" - echo "bump_major: bumps major of version" - echo "bump_minor: bumps minor of version" - echo "bump_patch: bumps patch of version" + echo "" + echo "bump_major: bumps major of version, setting minor and patch to 0, removing special" + echo "bump_minor: bumps minor of version, setting patch to 0, removing special" + echo "bump_patch: bumps patch of version, removing special" exit 255 fi From 323ba55f13705143ebfef0160d0d1797d2b72f3f Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Mon, 30 Dec 2013 00:19:25 +0100 Subject: [PATCH 7/9] Fixing comparison of pre-version vs non-pre-version --- semver.sh | 8 ++++++++ semver_test.sh | 1 + 2 files changed, 9 insertions(+) diff --git a/semver.sh b/semver.sh index 99f9480..3c8ae9b 100755 --- a/semver.sh +++ b/semver.sh @@ -62,6 +62,14 @@ function semverCmp() { fi # special + if [[ ( "$SPECIAL_A" != "" ) && ( "$SPECIAL_B" == "" ) ]]; then + return -1 + fi + + if [[ ( "$SPECIAL_A" == "" ) && ( "$SPECIAL_B" != "" ) ]]; then + return 1 + fi + if [[ "$SPECIAL_A" < "$SPECIAL_B" ]]; then return -1 fi diff --git a/semver_test.sh b/semver_test.sh index a23d42b..274d34a 100755 --- a/semver_test.sh +++ b/semver_test.sh @@ -30,6 +30,7 @@ local MAJOR=0 local MINOR=0 local PATCH=0 local SPECIAL="" + local VERSION="" echo "Parsing" From 27fe303b6abaae4ec35db71b9197f3dec3a72ac4 Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Mon, 30 Dec 2013 00:32:48 +0100 Subject: [PATCH 8/9] Added strip_special command, to remove special part of version --- semver.sh | 24 ++++++++++++++++++++++-- semver_test.sh | 8 ++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/semver.sh b/semver.sh index 3c8ae9b..fb2e41f 100755 --- a/semver.sh +++ b/semver.sh @@ -152,7 +152,7 @@ function semverBumpPatch() { local MAJOR=0 local MINOR=0 local PATCH=0 - local SPECIAL=0 + local SPECIAL="" semverParseInto $1 MAJOR MINOR PATCH SPECIAL PATCH=$(($PATCH + 1)) @@ -161,10 +161,22 @@ function semverBumpPatch() { semverConstruct $MAJOR $MINOR $PATCH $SPECIAL $2 } +function semverStripSpecial() { + local MAJOR=0 + local MINOR=0 + local PATCH=0 + local SPECIAL="" + + semverParseInto $1 MAJOR MINOR PATCH SPECIAL + SPECIAL="" + + semverConstruct $MAJOR $MINOR $PATCH $SPECIAL $2 +} + if [ "___semver.sh" == "___`basename $0`" ]; then if [ "$2" == "" ]; then echo "$0 [version]" - echo "Commands: cmp, eq, lt, gt, bump_major, bump_minor, bump_patch" + echo "Commands: cmp, eq, lt, gt, bump_major, bump_minor, bump_patch, strip_special" echo "" echo "cmp: compares left version against right version, return 0 if equal, 255 (-1) if left is lower than right, 1 if left is higher than right" echo "eq: compares left version against right version, returns 0 if both versions are equal" @@ -174,6 +186,8 @@ if [ "___semver.sh" == "___`basename $0`" ]; then echo "bump_major: bumps major of version, setting minor and patch to 0, removing special" echo "bump_minor: bumps minor of version, setting patch to 0, removing special" echo "bump_patch: bumps patch of version, removing special" + echo "" + echo "strip_special: strips special from version" exit 255 fi @@ -222,4 +236,10 @@ if [ "___semver.sh" == "___`basename $0`" ]; then echo ${VERSION} exit 0 fi + + if [ "$2" == "strip_special" ]; then + semverStripSpecial $1 VERSION + echo ${VERSION} + exit 0 + fi fi diff --git a/semver_test.sh b/semver_test.sh index 274d34a..d004022 100755 --- a/semver_test.sh +++ b/semver_test.sh @@ -211,6 +211,14 @@ doTest "semverBumpPatch $A" "1.3.3" $VERSION semverBumpPatch $E VERSION doTest "semverBumpPatch $E" "1.3.3" $VERSION + + +echo "Strip special" +semverStripSpecial $A VERSION +doTest "semverStripSpecial $A" "1.3.2" $VERSION + +semverStripSpecial $E VERSION +doTest "semverStripSpecial $E" "1.3.2" $VERSION } semverTest From 32e2f22fd9b797f81eaf9a570fc4c4855e2ee9ba Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Mon, 30 Dec 2013 00:36:13 +0100 Subject: [PATCH 9/9] Correctness of shell code --- semver.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semver.sh b/semver.sh index fb2e41f..a8e5ba6 100755 --- a/semver.sh +++ b/semver.sh @@ -13,7 +13,7 @@ function semverParseInto() { } function semverConstruct() { - if [ "$5" != "" ]; then + if [[ $# -eq 5 ]]; then eval $5=`echo "$1.$2.$3-$4"` fi