21700: Install Bundler system-wide in Rails postinst
[arvados.git] / build / version-at-commit.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 set -e -o pipefail
7 commit="$1"
8 versionglob="[0-9].[0-9]*.[0-9]*"
9 devsuffix="~dev"
10
11 # automatically assign *development* version
12 #
13 # handles the following cases:
14 #
15 # *  commit is on main or a development branch, the nearest tag is older
16 #    than commit where this branch joins main.
17 #    -> take greatest version tag in repo X.Y.Z and assign X.(Y+1).0
18 #
19 # *  commit is on a release branch, the nearest tag is newer
20 #    than the commit where this branch joins main.
21 #    -> take nearest tag X.Y.Z and assign X.Y.(Z+1)
22
23 # 1. get the nearest tag with 'git describe'
24 # 2. get the merge base between this commit and main
25 # 3. if the tag is an ancestor of the merge base,
26 #    (tag is older than merge base) increment minor version
27 #    else, tag is newer than merge base, so increment point version
28
29 nearest_tag=$(git describe --tags --abbrev=0 --match "$versionglob" "$commit")
30 merge_base=$(git merge-base origin/main "$commit")
31
32 if git merge-base --is-ancestor "$nearest_tag" "$merge_base" ; then
33     # x.(y+1).0~devTIMESTAMP, where x.y.z is the newest version that does not contain $commit
34     # grep reads the list of tags (-f) that contain $commit and filters them out (-v)
35     # this prevents a newer tag from retroactively changing the versions of everything before it
36     v=$(git tag | grep -vFf <(git tag --contains "$merge_base") | sort -Vr | head -n1 | perl -pe 's/(\d+)\.(\d+)\.\d+.*/"$1.".($2+1).".0"/e')
37 else
38     # x.y.(z+1)~devTIMESTAMP, where x.y.z is the latest released ancestor of $commit
39     v=$(echo $nearest_tag | perl -pe 's/(\d+)$/$1+1/e')
40 fi
41 isodate=$(TZ=UTC git log -n1 --format=%cd --date=iso "$commit")
42 ts=$(TZ=UTC date --date="$isodate" "+%Y%m%d%H%M%S")
43 echo "${v}${devsuffix}${ts}"