#!/bin/bash set -o nounset \ -o errexit \ -o pipefail REGEX_TAG='v([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)' function semver_tags() { { # Sometimes several release tags point to the same commit (see v1.9.0 and # v1.9.1). This iterates through each tag and ensures that it conforms to # semantic versioning requirements. Afterwards, the tags are sorted from # latest to earliest. This is so packages use the latest version tag. for tag in $(git tag --points-at HEAD) do if [[ ${tag} =~ ^${REGEX_TAG}$ ]] then printf '%s\n' "${tag}" fi done } | sort --version-sort --reverse } TAG=$(head -n 1 <<<"$(semver_tags)") # If no tag could be found for the corresponding commit, assume that this # is being built from an unversioned branch. In which case, this will # construct a version that is compatible with Debian versioning: # ${PREFIX}-<< short commit hash >> if [[ ${TAG} =~ ^${REGEX_TAG}$ ]] then cat <>"${BASH_ENV}" export VERSION="${TAG:1}" export MAJOR="${BASH_REMATCH[1]}" export MINOR="${BASH_REMATCH[2]}" export PATCH="${BASH_REMATCH[3]}" export RELEASE="1" EOF else cat <>"${BASH_ENV}" export VERSION="${PREFIX}-$(git rev-parse --short HEAD)" export MAJOR="" export MINOR="" export PATCH="" export RELEASE="" EOF fi