gitbutler/scripts/next.sh

58 lines
1.2 KiB
Bash
Raw Normal View History

2023-02-17 16:21:41 +03:00
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
function help() {
local to="$1"
echo "Usage: $0 <version> <bump>" 1>&$to
echo 1>&$to
echo "where:" 1>&$to
echo " <version> is a current semver version." 1>&$to
2023-02-20 16:57:58 +03:00
echo " <bump> is either 'patch', 'major' or 'minor'." 1>&$to
2023-02-17 16:21:41 +03:00
}
function error() {
echo "error: $@" 1>&2
echo 1>&2
help 2
exit 1
}
function info() {
echo "$@"
}
VERSION="$1"
BUMP="$2"
2023-02-20 17:29:17 +03:00
# https://semver.org/
2023-07-31 17:05:02 +03:00
SEMVER_REGEX="^(0|[1-9][[:digit:]]*)\.(0|[1-9][[:digit:]]*)\.(0|[1-9][[:digit:]]*)(?:-((?:0|[1-9][[:digit:]]*|[[:digit:]]*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9][[:digit:]]*|[[:digit:]]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
2023-02-20 17:29:17 +03:00
(echo "$VERSION" | grep -Eq "$SEMVER_REGEX") || error "'$VERSION' not a semver"
2023-02-17 16:21:41 +03:00
case "$BUMP" in
major)
MAJOR="$(($(echo "$VERSION" | cut -d. -f1) + 1))"
MINOR="0"
PATCH="0"
echo "$MAJOR.$MINOR.$PATCH"
;;
minor)
MAJOR="$(echo "$VERSION" | cut -d. -f1)"
MINOR="$(($(echo "$VERSION" | cut -d. -f2) + 1))"
PATCH="0"
echo "$MAJOR.$MINOR.$PATCH"
;;
2023-07-17 09:02:47 +03:00
patch)
2023-02-17 16:21:41 +03:00
MAJOR="$(echo "$VERSION" | cut -d. -f1)"
MINOR="$(echo "$VERSION" | cut -d. -f2)"
PATCH="$(($(echo "$VERSION" | cut -d. -f3) + 1))"
echo "$MAJOR.$MINOR.$PATCH"
;;
2023-07-17 09:02:47 +03:00
*)
error "invalid bump type: $BUMP"
;;
2023-02-17 16:21:41 +03:00
esac