yggdrasil-go/contrib/semver/version.sh

27 lines
680 B
Bash
Raw Normal View History

2018-03-05 22:34:23 +03:00
#!/bin/sh
# Get the last tag
TAG=$(git describe --abbrev=0 --tags --match="v[0-9]*\.[0-9]*" 2>/dev/null)
2018-03-05 22:34:23 +03:00
2018-03-05 23:06:38 +03:00
# Get the number of commits from the last tag
COUNT=$(git rev-list $TAG..HEAD --count 2>/dev/null)
2018-03-05 22:34:23 +03:00
2018-03-05 23:06:38 +03:00
# If it fails then there's no last tag - go from the first commit
if [ $? != 0 ]; then
COUNT=$(git rev-list HEAD --count 2>/dev/null)
2018-03-05 22:34:23 +03:00
printf 'v0.0.%d' "$COUNT"
2018-03-05 22:34:23 +03:00
exit -1
fi
# 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-03-05 22:34:23 +03:00
# Output in the desired format
if [ $COUNT = 0 ]; then
printf 'v%d.%d' "$MAJOR" "$MINOR"
else
printf 'v%d.%d.%d' "$MAJOR" "$MINOR" "$COUNT"
fi