#!/usr/bin/env bash # Compute Semantic Version (SemVer 2.0.0) for Asteria Qt. # # Rules: # - Base version (MAJOR.MINOR.BASE_PATCH) is read from the root VERSION file. # - On master (default branch): # - If HEAD already has a release tag vMAJOR.MINOR.P, reuse MAJOR.MINOR.P. # - Otherwise, PATCH = max(BASE_PATCH, highest existing vMAJOR.MINOR.* tag + 1). # - Outputs: MAJOR.MINOR.PATCH # - On feature branches: # - PATCH = max(BASE_PATCH, highest existing vMAJOR.MINOR.* tag + 1). # - Branch name is sanitized to lowercase [a-z0-9-] for SemVer pre-release. # - Outputs: MAJOR.MINOR.PATCH-. # # Usage: # scripts/compute-version.sh [--branch ] [--sha ] set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" VERSION_FILE="${PROJECT_DIR}/VERSION" OVERRIDE_BRANCH="" OVERRIDE_SHA="" while [[ $# -gt 0 ]]; do case "$1" in --branch) OVERRIDE_BRANCH="${2:?Missing value for --branch}" shift 2 ;; --sha) OVERRIDE_SHA="${2:?Missing value for --sha}" shift 2 ;; -h|--help) sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//' exit 0 ;; *) echo "ERROR: Unknown argument: $1" >&2 exit 1 ;; esac done if [ ! -f "$VERSION_FILE" ]; then echo "ERROR: VERSION file not found at $VERSION_FILE" >&2 exit 1 fi BASE_VERSION="$(tr -d '[:space:]' < "$VERSION_FILE")" if ! [[ "$BASE_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then echo "ERROR: Invalid VERSION '$BASE_VERSION' in $VERSION_FILE (expected MAJOR.MINOR.PATCH)" >&2 exit 1 fi MAJOR="${BASH_REMATCH[1]}" MINOR="${BASH_REMATCH[2]}" BASE_PATCH="${BASH_REMATCH[3]}" # Determine current branch and short SHA BRANCH="${OVERRIDE_BRANCH:-${CI_COMMIT_BRANCH:-${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-${CI_COMMIT_REF_NAME:-}}}}" SHORT_SHA="${OVERRIDE_SHA:-${CI_COMMIT_SHORT_SHA:-}}" HAS_GIT=false if command -v git >/dev/null 2>&1 && git -C "$PROJECT_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then HAS_GIT=true fi if [ -z "$BRANCH" ] && [ "$HAS_GIT" = true ]; then BRANCH="$(git -C "$PROJECT_DIR" rev-parse --abbrev-ref HEAD 2>/dev/null || true)" fi if [ -z "$SHORT_SHA" ]; then if [ "$HAS_GIT" = true ]; then SHORT_SHA="$(git -C "$PROJECT_DIR" rev-parse --short=7 HEAD 2>/dev/null || echo "local")" else SHORT_SHA="local" fi fi DEFAULT_BRANCH="${CI_DEFAULT_BRANCH:-master}" IS_MASTER=false if [ "$BRANCH" = "$DEFAULT_BRANCH" ] || [ "$BRANCH" = "master" ]; then IS_MASTER=true fi # Extract max patch from tags matching v?MAJOR.MINOR.PATCH (no pre-release suffix) max_patch_in_tags() { local max_p=-1 local tag p while IFS= read -r tag; do [ -z "$tag" ] && continue if [[ "$tag" =~ ^v?${MAJOR}\.${MINOR}\.([0-9]+)$ ]]; then p="${BASH_REMATCH[1]}" if [ "$p" -gt "$max_p" ]; then max_p="$p" fi fi done echo "$max_p" } PATCH="$BASE_PATCH" if [ "$HAS_GIT" = true ]; then # If on master (or detached HEAD) and HEAD is already tagged with a matching release tag, keep it if [ "$IS_MASTER" = true ] || [ "$BRANCH" = "HEAD" ] || [ -z "$BRANCH" ]; then HEAD_MAX_PATCH="$(git -C "$PROJECT_DIR" tag --points-at HEAD 2>/dev/null | max_patch_in_tags)" if [ "$HEAD_MAX_PATCH" -ge 0 ]; then if [ "$HEAD_MAX_PATCH" -ge "$BASE_PATCH" ]; then PATCH="$HEAD_MAX_PATCH" fi echo "${MAJOR}.${MINOR}.${PATCH}" exit 0 fi fi REPO_MAX_PATCH="$(git -C "$PROJECT_DIR" tag -l "v${MAJOR}.${MINOR}.*" "${MAJOR}.${MINOR}.*" 2>/dev/null | max_patch_in_tags)" if [ "$REPO_MAX_PATCH" -ge 0 ]; then NEXT_PATCH=$((REPO_MAX_PATCH + 1)) if [ "$NEXT_PATCH" -gt "$PATCH" ]; then PATCH="$NEXT_PATCH" fi fi fi if [ "$IS_MASTER" = true ]; then echo "${MAJOR}.${MINOR}.${PATCH}" exit 0 fi # Non-git fallback if branch couldn't be determined if [ -z "$BRANCH" ]; then echo "${MAJOR}.${MINOR}.${PATCH}" exit 0 fi if [ "$BRANCH" = "HEAD" ]; then SANITIZED_BRANCH="detached" else SANITIZED_BRANCH="$(printf '%s' "$BRANCH" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g; s/^-+//; s/-+$//')" if [ -z "$SANITIZED_BRANCH" ]; then SANITIZED_BRANCH="branch" fi fi SANITIZED_SHA="$(printf '%s' "$SHORT_SHA" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+//g')" if [ -z "$SANITIZED_SHA" ]; then SANITIZED_SHA="local" fi echo "${MAJOR}.${MINOR}.${PATCH}-${SANITIZED_BRANCH}.${SANITIZED_SHA}"