commit d3fa832ca27fa193be1c476182be31597d2ddb70 Author: yafox Date: Tue Nov 24 21:11:21 2020 +0000 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bd8c239 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright 2020 "yafox" + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..fd88631 --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +vercmp-bare-formats +=================== + +basic formats for use with `vercmp`. as simple as it gets. diff --git a/default-after-dash.sh b/default-after-dash.sh new file mode 100755 index 0000000..2e7874b --- /dev/null +++ b/default-after-dash.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +. "$VERCMPROOT/format/default.sh" + +comparever_default_after_dash() { + comparever_default "$(echo "$1" | cut -d- -f2)" "$(echo "$2" | cut -d- -f2)" +} +comparever() { comparever_default_after_dash $@; } diff --git a/default-with-dotless-patch.sh b/default-with-dotless-patch.sh new file mode 100755 index 0000000..21b7833 --- /dev/null +++ b/default-with-dotless-patch.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +. "$VERCMPROOT/format/default.sh" + +comparever_dotless_patch() { + # a semver that sometimes uses letters as an additional patch field without + # bothering to add a dot. + comparever_default \ + "$(echo "$1" | sed 's/\([a-z]\+[0-9a-z]*\)$/.\1/')" \ + "$(echo "$2" | sed 's/\([a-z]\+[0-9a-z]*\)$/.\1/')" +} +comparever() { comparever_dotless_patch $@; } diff --git a/default-with-underscores.sh b/default-with-underscores.sh new file mode 100755 index 0000000..866ff2c --- /dev/null +++ b/default-with-underscores.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +. "$VERCMPROOT/format/default.sh" + +comparever_default_with_underscores() { + comparever_default "$(echo "$1" | tr '_' '.')" "$(echo "$2" | tr '_' '.')" +} +comparever() { comparever_default_with_underscores $@; } diff --git a/default-without-dashed-prerelease.sh b/default-without-dashed-prerelease.sh new file mode 100755 index 0000000..df6cdb1 --- /dev/null +++ b/default-without-dashed-prerelease.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +. "$VERCMPROOT/format/default.sh" + +comparever_gdb() { + # a semver that eschews using a dash before prerelease versions. + comparever_default "$(echo "$1" | sed 's/\([a-z]\+[0-9a-z]*\)$/-\1/i')" \ + "$(echo "$2" | sed 's/\([a-z]\+[0-9a-z]*\)$/-\1/i')" +} +comparever() { comparever_gdb $@; } diff --git a/default.sh b/default.sh new file mode 100755 index 0000000..fab0fd4 --- /dev/null +++ b/default.sh @@ -0,0 +1,67 @@ +#!/bin/sh + +set -e + +# accepts two semantic-ish versions and echoes a character representing the +# relationship between the two. +# "semantic-ish" means here "a superset of semantic versioning." all correctly +# constructed semantic versions should compare in accordance with version 2.0 of +# the semver.org spec. versions that do not conform to the spec may also +# compare correctly. the primary difference is that there is no limitation on +# the number of version fields. +comparever() { comparever_default $@; } + +# a split definition allows other formats to use this function to define their +# own comparever functions if they wish. +comparever_default() { + v1="$1" + v2="$2" + + # strip any trailing meta tags + v1="${v1%+*}" + v2="${v2%+*}" + + # equal? + [ "$v1" != "$v2" ] || { echo "=" && return 0; } + + # strip leading characters that match. + while [ "$(echo "$v1" | cut -c1)" = "$(echo "$v2" | cut -c1)" ]; do + v1="$(echo "$v1" | cut -c2-)" + v2="$(echo "$v2" | cut -c2-)" + done + v1head="$(echo "$v1" | cut -c1)" + v2head="$(echo "$v2" | cut -c1)" + + # if the difference is a prerelease tag, the prerelease is lesser. + case "$v1head" in -) echo '<' && return 0;; esac + case "$v2head" in -) echo '>' && return 0;; esac + + # otherwise, if one is a prefix of the other, the other is greater. + [ -n "$v1" ] || { echo '<' && return 0; } + [ -n "$v2" ] || { echo '>' && return 0; } + + # otherwise, if one starts with a dot, the other is the greater. + case "$v1head" in .) echo '<' && return 0;; esac + case "$v2head" in .) echo '>' && return 0;; esac + + # strip any remaining version fields + v1="$(echo "$v1" | sed s/\\..*\$//)" + v2="$(echo "$v2" | sed s/\\..*\$//)" + + # if the version tails are numeric, compare numerically. (ignore prerelease + # tags.) otherwise, compare lexically. + if (echo "${v1%-*}${v2%-*}" | grep -q "^[0-9]\+\$"); then + [ "$(echo "${v1%-*}")" -gt "$(echo "${v2%-*}")" ] \ + && echo ">" \ + || echo "<" + + else + # v1 and v2 contain only the differences. using 'cut' and tabs here to + # perform a lexical sort on the differences and retrieve the original + # string associated with the greater of the two remainders. + greater=$(printf "%s\t%s\n%s\t%s" "$v1" "$1" "$v2" "$2" \ + | sort | tail -n1 | cut -f2) + + [ "$greater" = "$1" ] && echo ">" || echo "<" + fi +}