|
1 | 1 | #!/usr/bin/env bash |
| 2 | +# ====================================================================== |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. |
| 7 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +# (the "License"); you may not use this file except in compliance with |
| 9 | +# the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | +# ----------------------------------------------------------------------------- |
| 20 | +# getversion — determine the project version for packaging and diagnostics |
| 21 | +# ----------------------------------------------------------------------------- |
| 22 | +# |
| 23 | +# This script computes a version string using the following logic: |
| 24 | +# |
| 25 | +# 1. Extract the base PACKAGE_VERSION from the `configure` script. |
| 26 | +# |
| 27 | +# 2. If running inside a Git repository and `git describe` is usable: |
| 28 | +# |
| 29 | +# a. If the current commit matches an annotated tag exactly: |
| 30 | +# VERSION = <tag> |
| 31 | +# e.g., VERSION = 2.0.0+RC1 |
| 32 | +# |
| 33 | +# b. If the current commit is ahead of a tag: |
| 34 | +# VERSION = <PACKAGE_VERSION>+dev.<N>.g<commit> |
| 35 | +# e.g., VERSION = 2.0.1-devel+dev.3.gabcd123 |
| 36 | +# |
| 37 | +# 3. If *not* in a Git repo but a `VERSION` file exists: |
| 38 | +# VERSION and BUILDNUMBER are read from that file (format: "<version> build <build_number>") |
| 39 | +# |
| 40 | +# 4. If neither a Git repo nor a VERSION file is present: |
| 41 | +# VERSION = <PACKAGE_VERSION> from configure |
| 42 | +# BUILDNUMBER = dev |
| 43 | +# |
| 44 | +# 5. If a BUILD_NUMBER file is present and BUILDNUMBER is "dev", it will override the default: |
| 45 | +# BUILDNUMBER = $(cat BUILD_NUMBER) |
| 46 | +# |
| 47 | +# Output: |
| 48 | +# - Default: <version> build <build_number> |
| 49 | +# - With --short: just <version> |
| 50 | +# |
| 51 | +# Examples: |
| 52 | +# ./getversion |
| 53 | +# → 2.0.1-devel+dev.3.gabcd123 build dev |
| 54 | +# |
| 55 | +# ./getversion --short |
| 56 | +# → 2.0.1-devel+dev.3.gabcd123 |
| 57 | +# |
| 58 | +# (On exact tag) |
| 59 | +# → 2.0.0+RC1 build dev |
| 60 | +# |
| 61 | +# Supported Features: |
| 62 | +# - Handles annotated tags with special characters like `+` or `-` |
| 63 | +# - Produces semver-compatible version strings for CI/CD |
| 64 | +# - Works with or without Git |
| 65 | +# |
| 66 | +# Intended for use in: |
| 67 | +# - Release packaging |
| 68 | +# - Diagnostic reporting |
| 69 | +# - Embedding traceable version info into builds |
| 70 | +# |
| 71 | +# ====================================================================== |
2 | 72 |
|
3 | | -# Make sure we're running from the root git repository, not whatever submodule |
4 | | -# we could have been called from. |
5 | | -pushd $(dirname "$0") > /dev/null |
| 73 | +# Make sure we're running from the root git repository |
| 74 | +pushd "$(dirname "$0")" > /dev/null |
6 | 75 |
|
7 | | -VERSION=$(perl -e 'while(<>){print $1 if (/^PACKAGE_VERSION='\''(.+)'\''$/)}' < configure) |
| 76 | +# Extract PACKAGE_VERSION from the configure script |
| 77 | +VERSION=$(perl -ne 'print $1 if /^PACKAGE_VERSION='\''(.+)'\''$/' < configure) |
8 | 78 | BUILDNUMBER=dev |
9 | 79 |
|
10 | | -# Call git describe, and convert it to a semi-semantic version, like |
11 | | -# 5.0.0-alpha.0+dev.52.g123abc |
| 80 | +# Function to generate a dev suffix like "dev.1.gabcd123" |
12 | 81 | generate_dev_version() { |
13 | | - git describe | perl -pe 's/(.*)-([0-9]*)-(g[0-9a-f]*)/\1+dev.\2.\3/' |
| 82 | + local latest_tag |
| 83 | + latest_tag=$(git describe --tags --abbrev=0) |
| 84 | + local full_desc |
| 85 | + full_desc=$(git describe --tags --long) |
| 86 | + local suffix="${full_desc#$latest_tag-}" |
| 87 | + echo "dev.${suffix//-/.}" |
14 | 88 | } |
15 | 89 |
|
16 | | -# If we are in a Git repository and have git installed, build the version |
17 | | -# string using the latest tag in case it's reachable |
18 | | -if type git >/dev/null 2>&1 && [ -d '.git' ] ; then |
19 | | - |
20 | | - # Check for tag reachability, in case of shallow clones we might not |
21 | | - # be able to use git describe since the commit which was tagged is |
22 | | - # unreachable even if we have pulled the tags. If we can reach it, |
23 | | - # overwrite the VERSION from autoconf with the output, else append |
24 | | - # HEAD commit info |
25 | | - if git describe >/dev/null 2>&1 ; then |
26 | | - VERSION=$(generate_dev_version) |
27 | | - else |
28 | | - VERSION+=+ |
29 | | - VERSION+=$(git rev-parse --short HEAD) |
30 | | - fi |
31 | | -# If not a git repository and VERSION file exists, use version string from file. |
| 90 | +# Check if we're in a Git repo and git is available |
| 91 | +if type git >/dev/null 2>&1 && [ -d '.git' ]; then |
| 92 | + # Ensure git describe doesn't fail due to shallow clone |
| 93 | + if git describe --tags --long >/dev/null 2>&1; then |
| 94 | + if git describe --exact-match >/dev/null 2>&1; then |
| 95 | + # We're exactly on a tag |
| 96 | + VERSION=$(git describe --exact-match) |
| 97 | + else |
| 98 | + # Not on a tag: add dev version suffix |
| 99 | + VERSION+="+$(generate_dev_version)" |
| 100 | + fi |
| 101 | + fi |
| 102 | +# Not a git repo but VERSION file exists |
32 | 103 | elif [[ -s ./VERSION ]]; then |
33 | | - VERSION=$(awk -F' build ' '{print $1}' ./VERSION) |
34 | | - BUILDNUMBER=$(awk -F' build ' '{print $2}' ./VERSION) |
| 104 | + VERSION=$(awk -F' build ' '{print $1}' ./VERSION) |
| 105 | + BUILDNUMBER=$(awk -F' build ' '{print $2}' ./VERSION) |
35 | 106 | fi |
36 | 107 |
|
| 108 | +# Handle optional flag |
37 | 109 | FLAG="${1:-noflag}" |
38 | | -if [ "$FLAG" = '--short' ] ; then |
| 110 | +if [ "$FLAG" = "--short" ]; then |
39 | 111 | echo "${VERSION}" |
40 | 112 | else |
41 | | - if [[ ${BUILDNUMBER} = 'dev' && -f BUILD_NUMBER ]] ; then |
42 | | - BUILDNUMBER=`cat BUILD_NUMBER` |
| 113 | + if [[ ${BUILDNUMBER} = "dev" && -f BUILD_NUMBER ]]; then |
| 114 | + BUILDNUMBER=$(<BUILD_NUMBER) |
43 | 115 | fi |
44 | 116 | echo "${VERSION} build ${BUILDNUMBER}" |
45 | 117 | fi |
| 118 | + |
46 | 119 | popd > /dev/null |
0 commit comments