Skip to content

Commit 151b8e5

Browse files
authored
snap/scripts/version: Update for usage in monorepo (#1295)
2 parents 00325ef + 0f3680c commit 151b8e5

File tree

1 file changed

+20
-42
lines changed

1 file changed

+20
-42
lines changed

snap/scripts/version

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,33 @@ set -eu
44
# This scripts prints the version of the snap based on the git tags and the
55
# current branch. The version is determined as follows:
66
#
7-
# If there's a tag prefixed with the current branch name, that tag is
8-
# used and the prefix is stripped. For example:
9-
# * msentraid-0.1.0 -> 0.1.0
7+
# The highest version tag which is prefixed with "broker-" and is merged into the
8+
# current branch is used as the base version.
9+
# If there is no such tag, "0.0.0" is used as the base version.
1010
#
11-
# Else, the highest version tag that starts with a number (in contrast
12-
# to a prefix like "msentraid-") is used. For example:
13-
# * 0.1.0 -> 0.1.0
11+
# If the current commit is tagged, that tag is used as the version as is.
1412
#
15-
# 1. If current commit is tagged, that tag is used as the version as is.
16-
# 2. If current commit is not tagged, the version is:
17-
# * When on main: <latest tag on current branch>+<commit_sha>
18-
# * Else: <latest tag on current branch>+<commit_sha>.<last_commit_merged_from_main>
13+
# If the current commit is not tagged, the version is appended with the short
14+
# sha of the current commit. When the current branch is not "main", the version
15+
# is further appended with the short sha of the last commit merged from the main
16+
# branch.
1917
#
20-
# The version is appended with ".dirty" if there are uncommitted changes.
21-
22-
# strip_branch_tag_prefix removes any non-numeric prefix ending with a
23-
# dash (e.g. "msentraid-") from the tag name. We use this to remove the
24-
# branch name prefix from the tag name, but we do not just strip the
25-
# current branch name because we also want to support branching of a new
26-
# branch and use the latest tag from that branch (for example when
27-
# branching of the msentraid branch to test a fix, then that branch
28-
# should still use a valid version).
29-
# $1: tag: the tag name to strip the prefix from.
30-
strip_branch_tag_prefix() {
31-
tag="${1}"
32-
33-
echo "${tag}" | sed 's/^[^0-9-]*-//'
34-
}
18+
# Finally, the version is appended with ".dirty" if there are uncommitted changes.
3519

3620
get_version() {
3721
current_branch=$(git branch --show-current)
3822

39-
# Get the highest version tag which is prefixed with the current branch name.
40-
tag=$(git -c "versionsort.suffix=-pre" tag --sort=-v:refname --merged="${current_branch}" | grep "^${current_branch}-" | head -1)
23+
# Get the highest version tag which is prefixed with "broker-"
24+
tag=$(git -c "versionsort.suffix=-pre" tag --sort=-v:refname --merged="${current_branch}" | grep -E '^broker-' | head -n 1)
25+
version="${tag}"
4126

42-
# If there is no tag prefixed with the current branch name, use the most
43-
# recent tag that does not have a non-numerical prefix (that's the case
44-
# when we're building a snap for testing on a branch that's not
45-
# "msentraid" or "google").
46-
if [ -z "${tag}" ]; then
47-
tag=$(git -c "versionsort.suffix=-pre" tag --sort=-v:refname --merged="${current_branch}" | grep -E '^[0-9]+' | head -1)
48-
fi
27+
# Strip the "broker-" prefix
28+
version="${version#broker-}"
4929

50-
version="${tag}"
5130
if [ -z "${version}" ]; then
52-
# No tag found, use "notag" as version.
53-
version="notag"
31+
# No tag found, use "0.0.0"
32+
version="0.0.0"
5433
fi
55-
version=$(strip_branch_tag_prefix "${version}")
5634

5735
# If the highest version tag is on the current commit, use it as is after
5836
# stripping the prefix.
@@ -64,15 +42,15 @@ get_version() {
6442
# Current commit is not tagged, append commit(s) sha.
6543
version="${version}+$(git rev-parse --short=7 HEAD)"
6644

67-
# Main branch will be set as is.
45+
# If the current branch is not "main", append the short sha of the last commit
6846
if [ "${current_branch}" = "main" ]; then
47+
# Current branch is "main", return the version as is.
6948
echo "${version}"
7049
return
7150
fi
7251

73-
# Get the short version of last commit merged from the main branch.
74-
last_commit_on_main=$(git merge-base main HEAD)
75-
last_commit_on_main=$(git rev-parse --short=7 "${last_commit_on_main}")
52+
# Get the short version of the last commit merged from the main branch.
53+
last_commit_on_main=$(git rev-parse --short=7 "$(git merge-base main HEAD)")
7654
echo "${version}.${last_commit_on_main}"
7755
}
7856

0 commit comments

Comments
 (0)