From 5121195d008ec1acd55efc7c86325b946c708c7f Mon Sep 17 00:00:00 2001 From: Yuxiang Zhu Date: Jul 23 2018 06:06:09 +0000 Subject: Create a separated script for generating version/release numbers This moves the logic of generating version release numbers from `rpmbuild.sh` to a separated script, so that getting the current version/release number for CI/CD and container builds without building RPMs is possible and easier. The output will be exported to environment variables. The usage is simply sourcing the script. --- diff --git a/rpmbuild.sh b/rpmbuild.sh index 60b894e..f4434a5 100755 --- a/rpmbuild.sh +++ b/rpmbuild.sh @@ -12,50 +12,28 @@ if [ $# -eq 0 ] ; then exit 1 fi -name=waiverdb -if [ "$(git tag | wc -l)" -eq 0 ] ; then - # never been tagged since the project is just starting out - lastversion="0.0" - revbase="" -else - lasttag="$(git describe --abbrev=0 HEAD)" - lastversion="${lasttag##${name}-}" - revbase="^$lasttag" -fi -if [ "$(git rev-list $revbase HEAD | wc -l)" -eq 0 ] ; then - # building a tag - rpmver="" - rpmrel="" - version="$lastversion" -else - # git builds count as a pre-release of the next version - version="$lastversion" - version="${version%%[a-z]*}" # strip non-numeric suffixes like "rc1" - # increment the last portion of the version - version="${version%.*}.$((${version##*.} + 1))" - commitcount=$(git rev-list $revbase HEAD | wc -l) - commitsha=$(git rev-parse --short HEAD) - rpmver="${version}" - rpmrel="0.git.${commitcount}.${commitsha}" - version="${version}.dev${commitcount}+git.${commitsha}" -fi +SELF=$(readlink -f -- "$0") +HERE=$(dirname -- "$SELF") +source "$HERE"/version.sh + +name=waiverdb workdir="$(mktemp -d)" trap "rm -rf $workdir" EXIT outdir="$(readlink -f ./rpmbuild-output)" mkdir -p "$outdir" git archive --format=tar HEAD | tar -C "$workdir" -xf - -if [ -n "$rpmrel" ] ; then +if [ -n "$WAIVERDB_RPM_RELEASE" ] ; then # need to hack the version in the spec sed --regexp-extended --in-place \ - -e "/%global upstream_version /c\%global upstream_version ${version}" \ - -e "/^Version:/cVersion: ${rpmver}" \ - -e "/^Release:/cRelease: ${rpmrel}%{?dist}" \ + -e "/%global upstream_version /c\%global upstream_version ${WAIVERDB_VERSION}" \ + -e "/^Version:/cVersion: ${WAIVERDB_RPM_VERSION}" \ + -e "/^Release:/cRelease: ${WAIVERDB_RPM_RELEASE}%{?dist}" \ "$workdir/${name}.spec" # also hack the Python module version sed --regexp-extended --in-place \ - -e "/^__version__ = /c\\__version__ = '$version'" \ + -e "/^__version__ = /c\\__version__ = '$WAIVERDB_VERSION'" \ "$workdir/waiverdb/__init__.py" fi ( cd "$workdir" && python3 setup.py sdist ) diff --git a/version.sh b/version.sh new file mode 100644 index 0000000..f063aee --- /dev/null +++ b/version.sh @@ -0,0 +1,39 @@ +#!/bin/bash +set -e +# SPDX-License-Identifier: GPL-2.0+ + +# Generate a version number from the current code base. + +name=waiverdb +if [ "$(git tag | wc -l)" -eq 0 ] ; then + # never been tagged since the project is just starting out + lastversion="0.0" + revbase="" +else + lasttag="$(git describe --abbrev=0 HEAD)" + lastversion="${lasttag##${name}-}" + revbase="^$lasttag" +fi +if [ "$(git rev-list $revbase HEAD | wc -l)" -eq 0 ] ; then + # building a tag + rpmver="" + rpmrel="" + version="$lastversion" +else + # git builds count as a pre-release of the next version + version="$lastversion" + version="${version%%[a-z]*}" # strip non-numeric suffixes like "rc1" + # increment the last portion of the version + version="${version%.*}.$((${version##*.} + 1))" + commitcount=$(git rev-list $revbase HEAD | wc -l) + commitsha=$(git rev-parse --short HEAD) + rpmver="${version}" + rpmrel="0.git.${commitcount}.${commitsha}" + version="${version}.dev${commitcount}+git.${commitsha}" +fi + +export WAIVERDB_VERSION=$version +export WAIVERDB_RPM_VERSION=$rpmver +export WAIVERDB_RPM_RELEASE=$rpmrel +export WAIVERDB_CONTAINER_VERSION=${version/+/-} +