|
| 1 | +#!/bin/sh -eu |
| 2 | +# |
| 3 | +# Copyright Quadrivium LLC |
| 4 | +# All Rights Reserved |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | +# |
| 7 | +# |
| 8 | +# Version String Generator for Git Repositories |
| 9 | +# |
| 10 | +# This script generates a version string based on the current Git state. |
| 11 | +# It retrieves the latest tag, calculates the number of commits since the last tag, |
| 12 | +# includes the current branch (if different from master), and appends the commit hash. |
| 13 | +# If the working directory has uncommitted changes, it marks the version as "dirty". |
| 14 | +# |
| 15 | +# Usage: |
| 16 | +# ./get_version.sh [--sanitized] |
| 17 | +# |
| 18 | +# Options: |
| 19 | +# --sanitized Replaces non-alphanumeric characters in the version string |
| 20 | +# with hyphens for compatibility with package managers. |
| 21 | +# |
| 22 | +# Output: |
| 23 | +# Prints a version string in the format: |
| 24 | +# <latest_tag>-<commits_since_tag>-<branch>-<commits_since_fork>-<commit_hash> |
| 25 | +# If the repository is dirty, "-dirty" is appended. |
| 26 | +# |
| 27 | +# Example: |
| 28 | +# v1.2.3-5-feature-branch-2-a1b2c3d-dirty |
| 29 | +# |
| 30 | +# Requirements: |
| 31 | +# - Git |
| 32 | +# - sed (for --sanitized mode) |
| 33 | +# |
| 34 | +# Notes: |
| 35 | +# - If the repository has no tags, an initial tag "_init" is created. |
| 36 | +# - If the repository is not a Git repo, outputs "Unknown(no git)". |
| 37 | +# - If Git is not installed, it exits with an error. |
| 38 | +# |
| 39 | + |
| 40 | +sanitize_version() { |
| 41 | + echo "$1" | sed -E 's/[^a-zA-Z0-9.+~:-]/-/g' |
| 42 | +} |
| 43 | + |
| 44 | +realpath() { |
| 45 | + case "$1" in |
| 46 | + /*) echo "$1" ;; |
| 47 | + *) echo "$(pwd)/$1" ;; |
| 48 | + esac |
| 49 | +} |
| 50 | + |
| 51 | +cd "$(dirname "$(realpath "$0")")" |
| 52 | + |
| 53 | +SANITIZED=false |
| 54 | +[ "$#" -gt 0 ] && [ "$1" = "--sanitized" ] && SANITIZED=true |
| 55 | + |
| 56 | +if [ -x "$(command -v git)" ] && [ -d "$(git rev-parse --git-dir 2>/dev/null)" ]; then |
| 57 | + HEAD=$(git rev-parse --short HEAD) |
| 58 | + |
| 59 | + # Determine the main branch (fallback to default names if necessary) |
| 60 | + MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@') |
| 61 | + if [ -z "$MAIN_BRANCH" ]; then |
| 62 | + MAIN_BRANCH=$(git branch --format='%(refname:short)' | grep -E '^(main|master)$' | head -n1) |
| 63 | + fi |
| 64 | + if [ -z "$MAIN_BRANCH" ]; then |
| 65 | + MAIN_BRANCH="master" # Fallback to "master" if no main branch detected |
| 66 | + fi |
| 67 | + |
| 68 | + COMMON=$(git merge-base HEAD "$MAIN_BRANCH" 2>/dev/null || echo "$HEAD") |
| 69 | + |
| 70 | + if ! git tag | grep -q .; then |
| 71 | + ROOT_COMMIT=$(git rev-list --max-parents=0 HEAD || echo "") |
| 72 | + [ -n "$ROOT_COMMIT" ] && ! git tag | grep -q "_init" && git tag _init "$ROOT_COMMIT" |
| 73 | + fi |
| 74 | + |
| 75 | + DESCR=$(git describe --tags --long "$COMMON" 2>/dev/null || echo "") |
| 76 | + [ -z "$DESCR" ] && DESCR="$HEAD-0-g$HEAD" |
| 77 | + |
| 78 | + TAG_IN_MASTER=$(echo "$DESCR" | sed -E "s/v?(.*)-([0-9]+)-g[a-f0-9]+/\1/") |
| 79 | + TAG_TO_FORK_DISTANCE=$(echo "$DESCR" | sed -E "s/v?(.*)-([0-9]+)-g[a-f0-9]+/\2/") |
| 80 | + |
| 81 | + BRANCH=$(git branch --show-current 2>/dev/null || echo "$HEAD") |
| 82 | + FORK_TO_HEAD_DISTANCE=$(git rev-list --count "$COMMON..HEAD" 2>/dev/null || echo "0") |
| 83 | + |
| 84 | + RESULT=$TAG_IN_MASTER |
| 85 | + [ "$TAG_TO_FORK_DISTANCE" != "0" ] && RESULT="$RESULT-$TAG_TO_FORK_DISTANCE" |
| 86 | + [ "$BRANCH" != "$MAIN_BRANCH" ] && RESULT="$RESULT-$BRANCH-$FORK_TO_HEAD_DISTANCE-$HEAD" |
| 87 | + |
| 88 | + git diff --quiet || DIRTY="-dirty" |
| 89 | + RESULT="$RESULT${DIRTY:-}" |
| 90 | +else |
| 91 | + RESULT="Unknown(no git)" |
| 92 | +fi |
| 93 | + |
| 94 | +[ "$SANITIZED" = true ] && RESULT=$(sanitize_version "$RESULT") |
| 95 | + |
| 96 | +printf "%s" "$RESULT" |
0 commit comments