5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-09-20 02:32:32 +00:00
yggdrasil-go/contrib/semver/version.sh

32 lines
956 B
Bash
Raw Normal View History

2018-03-05 19:34:23 +00:00
#!/bin/sh
# Get the last tag
2018-12-27 20:03:46 +00:00
TAG=$(git describe --abbrev=0 --tags --match="v[0-9]*\.[0-9]*\.[0-9]*" 2>/dev/null)
2018-12-27 21:14:23 +00:00
(test $? != 0 || test -z "$TAG") && (echo "unknown"; exit 1)
2018-12-27 20:03:46 +00:00
# Get the current branch
BRANCH=$(git symbolic-ref -q HEAD --short 2>/dev/null)
2018-12-27 21:14:23 +00:00
(test $? != 0 || test -z "$BRANCH") && BRANCH="master"
2018-03-05 19:34:23 +00:00
# Split out into major, minor and patch numbers
MAJOR=$(echo $TAG | cut -c 2- | cut -d "." -f 1)
MINOR=$(echo $TAG | cut -c 2- | cut -d "." -f 2)
2018-12-27 20:03:46 +00:00
PATCH=$(echo $TAG | cut -c 2- | cut -d "." -f 3)
2018-03-05 19:34:23 +00:00
# Output in the desired format
2018-12-27 20:03:46 +00:00
if [ $((PATCH)) -eq 0 ]; then
printf '%s%d.%d' "$PREPEND" "$((MAJOR))" "$((MINOR))"
else
2018-12-27 20:03:46 +00:00
printf '%s%d.%d.%d' "$PREPEND" "$((MAJOR))" "$((MINOR))" "$((PATCH))"
fi
# Add the build tag on non-master branches
2018-12-27 20:03:46 +00:00
if [ "$BRANCH" != "master" ]; then
BUILD=$(git rev-list $TAG..HEAD --count)
2018-12-27 21:14:23 +00:00
if [ $? = 0 ] && [ -n "$BUILD" ]; then
if [ $((BUILD)) -gt 0 ]; then
printf -- "-%04d" "$((BUILD))"
fi
fi
fi