Files
coursebank/scripts/nightly-version.sh
T

28 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Print an auto-generated nightly version string derived from git history:
#
# <last-release-tag>+<commits-since>.g<short-sha> e.g., 26.8.0+14.gb1a2c3d4
#
# When no release has been tagged yet, the base is 0.0.0 and the count is the
# total number of commits:
#
# 0.0.0+<total-commits>.g<short-sha> e.g., 0.0.0+57.gb1a2c3d4
#
# The `+...` part is SemVer build metadata, so the string stays a valid version
# and never sorts ahead of the release it is based on. Needs full history and
# tags (check out with fetch-depth: 0 and fetch-tags: true). Release tags are
# matched as N.N.N; the moving `nightly` tag is ignored.
set -euo pipefail
sha="$(git rev-parse --short=8 HEAD)"
if desc="$(git describe --tags --long \
--match '[0-9]*.[0-9]*.[0-9]*' --exclude nightly 2>/dev/null)"; then
# desc is <tag>-<n>-g<hash>; the tag itself has no '-', so strip from the right.
rest="${desc%-g*}" # <tag>-<n>
n="${rest##*-}" # <n>
tag="${rest%-*}" # <tag>
echo "${tag}+${n}.g${sha}"
else
n="$(git rev-list --count HEAD)"
echo "0.0.0+${n}.g${sha}"
fi