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