|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Output the program version based on the state of the repository (source code |
| 4 | +# and tags). |
| 5 | +# Tags must be in the form of |
| 6 | +# x.y.z, e.g. 3.1.0 |
| 7 | +# or |
| 8 | +# x.y.z-<prerelease part (alpha, alpha2, beta etc.)>, e.g. 3.1.0-beta2 |
| 9 | +# If the tag consists only of the version number (i.e. it doesn't contain a |
| 10 | +# prerelease part) and this number is equal to the version in the header file |
| 11 | +# then the version is considered a release and no additional version data is |
| 12 | +# appended to it by default (but can be forced by "-p" and "-r" command line |
| 13 | +# arguments). Otherwise, provided Git is available, the prerelease part and Git |
| 14 | +# revision are automatically added to the version. |
| 15 | + |
| 16 | +cd`dirname"$0"` |
| 17 | + |
| 18 | +whilegetopts p:r:sv: opt;do |
| 19 | +case$optin |
| 20 | + p) prerelease=$OPTARG;; |
| 21 | + r) revision=$OPTARG;; |
| 22 | + s) ID=semver;; |
| 23 | + v) version=$OPTARG;; |
| 24 | +esac |
| 25 | +done |
| 26 | + |
| 27 | +if [-z"$ID" ];then |
| 28 | +. /etc/os-release |
| 29 | +fi |
| 30 | +case$IDin |
| 31 | + altlinux | astra | debian | ubuntu) |
| 32 | +# The only scheme that properly sorts metadata and prerelease fields is |
| 33 | +# when the both are specified after a '~' |
| 34 | + presep='~'; metasep='~';; |
| 35 | + centos | opensuse-leap | redos) |
| 36 | + presep='~'; metasep=^;; |
| 37 | +*)# semver |
| 38 | + presep=-; metasep=+ |
| 39 | +esac |
| 40 | + |
| 41 | +if [-z"$version" ];then |
| 42 | + version=`grep'#define PROGRAM_VERSION' src/pg_probackup.h| cut -f 2| tr -d'"'` |
| 43 | +fi |
| 44 | + |
| 45 | +if which git>/dev/null2>&1;then |
| 46 | + tag=`git describe --tags2> /dev/null` |
| 47 | +# Shallow cloned repository may not have tags |
| 48 | +if [-z"$prerelease"-a"$tag" ];then |
| 49 | + f1=`cut -d - -f 1<<<$tag` |
| 50 | + f2=`cut -d - -f 2<<<$tag` |
| 51 | +# Append the prerelease part only if the tag refers to the current version |
| 52 | +# Assume that the prerelease part contains letters |
| 53 | +if [$f1=$version ]&& expr"$f2":"[1-9]*[a-zA-Z]"1>/dev/null;then |
| 54 | + prerelease="$f2" |
| 55 | +fi |
| 56 | +fi |
| 57 | +if [-z"$revision" ];then |
| 58 | + revision=g`git rev-parse --short HEAD` |
| 59 | +fi |
| 60 | +fi |
| 61 | + |
| 62 | +out=$version${prerelease:+$presep$prerelease} |
| 63 | +if ["$tag"!=$version-a"$revision" ];then |
| 64 | + out=$out$metasep`date +%Y%m%d`$revision |
| 65 | +fi |
| 66 | + |
| 67 | +echo$out |