2018-03-05 19:34:23 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2018-12-17 19:17:29 +00:00
|
|
|
# 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:22:46 +00:00
|
|
|
|
|
|
|
# Did getting the tag succeed?
|
|
|
|
if [ $? != 0 ] || [ -z "$TAG" ]; then
|
2018-12-27 21:36:50 +00:00
|
|
|
printf -- "unknown"
|
2019-09-18 20:09:53 +01:00
|
|
|
exit 0
|
2018-12-27 21:22:46 +00:00
|
|
|
fi
|
2018-06-17 18:17:21 +01:00
|
|
|
|
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:22:46 +00:00
|
|
|
|
|
|
|
# Did getting the branch succeed?
|
|
|
|
if [ $? != 0 ] || [ -z "$BRANCH" ]; then
|
|
|
|
BRANCH="master"
|
|
|
|
fi
|
2018-03-05 19:34:23 +00:00
|
|
|
|
|
|
|
# Split out into major, minor and patch numbers
|
2018-03-05 22:14:36 +00:00
|
|
|
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-06-17 18:17:21 +01:00
|
|
|
|
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))"
|
2018-03-05 22:14:36 +00:00
|
|
|
else
|
2018-12-27 20:03:46 +00:00
|
|
|
printf '%s%d.%d.%d' "$PREPEND" "$((MAJOR))" "$((MINOR))" "$((PATCH))"
|
2018-03-05 22:14:36 +00:00
|
|
|
fi
|
2018-06-17 18:17:21 +01:00
|
|
|
|
|
|
|
# Add the build tag on non-master branches
|
2018-12-27 20:03:46 +00:00
|
|
|
if [ "$BRANCH" != "master" ]; then
|
2018-12-27 21:45:30 +00:00
|
|
|
BUILD=$(git rev-list --count $TAG..HEAD 2>/dev/null)
|
2018-12-27 21:22:46 +00:00
|
|
|
|
|
|
|
# Did getting the count of commits since the tag succeed?
|
2018-12-27 21:36:50 +00:00
|
|
|
if [ $? != 0 ] || [ -z "$BUILD" ]; then
|
|
|
|
printf -- "-unknown"
|
2019-09-18 20:09:53 +01:00
|
|
|
exit 0
|
2018-12-27 21:22:46 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Is the build greater than zero?
|
|
|
|
if [ $((BUILD)) -gt 0 ]; then
|
|
|
|
printf -- "-%04d" "$((BUILD))"
|
2018-06-17 18:17:21 +01:00
|
|
|
fi
|
|
|
|
fi
|