keyproofs/version.sh

53 lines
920 B
Bash
Raw Normal View History

2020-11-23 13:58:19 -07:00
#!/bin/bash
# Increment a version string using Semantic Versioning (SemVer) terminology.
# Parse command line options.
2023-01-06 13:30:33 -07:00
BUMP="${BUMP:="$1"}"
2020-11-23 13:58:19 -07:00
case $BUMP in
current ) ;;
major ) major=true;;
minor ) minor=true;;
patch ) patch=true;;
esac
2023-01-06 13:30:33 -07:00
version=$(git describe --tags "$(git rev-list --tags --max-count=1 2> /dev/null)" 2> /dev/null|cut -b2-)
2020-11-23 13:58:19 -07:00
# Build array from version string.
2023-01-06 13:30:33 -07:00
IFS="." read -r -a a <<< "$version"
2020-11-23 13:58:19 -07:00
# If version string is missing or has the wrong number of members, show usage message.
if [ ${#a[@]} -ne 3 ]
then
version=0.0.0
2023-01-06 13:30:33 -07:00
IFS="." read -r -a a <<< "$version"
2020-11-23 13:58:19 -07:00
fi
# Increment version numbers as requested.
2023-01-06 13:30:33 -07:00
if [ -n "$major" ]
2020-11-23 13:58:19 -07:00
then
((a[0]++))
a[1]=0
a[2]=0
fi
2023-01-06 13:30:33 -07:00
if [ -n "$minor" ]
2020-11-23 13:58:19 -07:00
then
((a[1]++))
a[2]=0
fi
2023-01-06 13:30:33 -07:00
if [ -n "$patch" ]
2020-11-23 13:58:19 -07:00
then
((a[2]++))
fi
if git status --porcelain >/dev/null
then
2023-01-06 13:30:33 -07:00
echo "v${a[0]}.${a[1]}.${a[2]}"
2020-11-23 13:58:19 -07:00
else
2023-01-06 13:30:33 -07:00
echo "v${a[0]}.${a[1]}.${a[2]}-dirty"
2020-11-23 13:58:19 -07:00
fi