forked from docker/packaging
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen-ver.sh
More file actions
executable file
·112 lines (104 loc) · 5.08 KB
/
gen-ver.sh
File metadata and controls
executable file
·112 lines (104 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
# Copyright 2022 Docker Packaging authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
srcdir="$1"
if [ -z "$srcdir" ]; then
echo "usage: ./gen-ver <srcdir>" >&2
exit 1
fi
version=$(git -C "${srcdir}" describe --match 'v[0-9]*' --always --tags)
commit="$(git --git-dir "${srcdir}/.git" rev-parse HEAD)"
commitShort=${commit:0:7}
# Handle prefixed version formats
# cmd/cli/v0.1.44 -> v0.1.44
if [[ "$version" =~ .*/v[0-9] ]]; then
version="${version##*/}"
fi
# docker-v29.0.0 -> v29.0.0
if [[ "$version" =~ ^docker-v[0-9] ]]; then
version="${version#docker-}"
fi
# rpm "Release:" field ($rpmRelease) is used to set the "_release" macro, which
# is an incremental number for builds of the same release (Version: / #rpmVersion).
#
# This field can be:
#
# - Version: 0 : Package was built, but no matching upstream release (e.g., can be used for "nightly" builds)
# - Version: 1 : Package was built for an upstream (pre)release version
# - Version: > 1 : Only to be used for packaging-only changes (new package built for a version for which a package was already built/released)
#
# For details, see the Fedora packaging guide:
# https://docs.fedoraproject.org/en-US/packaging-guidelines/Versioning/#_complex_versioning_with_a_reasonable_upstream
#
# Note that older versions of the rpm spec allowed more traditional information
# in this field, which is still allowed, but considered deprecated; see
# https://docs.fedoraproject.org/en-US/packaging-guidelines/Versioning/#_complex_versioning_with_a_reasonable_upstream
#
# In our case, this means that all releases, except for "nightly" builds should
# use "Version: 1". Only in an exceptional case, where we need to publish a new
# package (build) for an existing release, "Version: 2" should be used; this script
# does not currently account for that situation.
#
# Assuming all tagged version of rpmRelease correspond with an upstream release,
# this means that versioning is as follows:
#
# Docker 22.06.0: version=22.06.0, release=1
# Docker 22.06.0-alpha.1: version=22.06.0, release=1
# Docker 22.06.0-beta.1: version=22.06.0, release=1
# Docker 22.06.0-rc.1: version=22.06.0, release=1
# Docker 22.06.0-dev: version=0.0.0~YYYYMMDDHHMMSS.gitHASH, release=0
rpmRelease=1
# if NIGHTLY_BUILD=1, or we have a "-dev" suffix or a commit not pointing to a
# tag, this is a nightly build, and we'll create a pseudo version based on
# commit-date and -sha.
if [[ "$NIGHTLY_BUILD" == "1" ]] || [[ "$version" == *-dev ]] || [[ -z "$(git -C "${srcdir}" tag --points-at HEAD --sort -version:refname)" ]]; then
# based on golang's pseudo-version: https://groups.google.com/forum/#!topic/golang-dev/a5PqQuBljF4
#
# using a "pseudo-version" of the form v0.0.0-yyyymmddhhmmss-abcdefa,
# where the time is the commit time in UTC and the final suffix is the prefix
# of the commit hash. The time portion ensures that two pseudo-versions can
# be compared to determine which happened later, the commit hash identifes
# the underlying commit, and the v0.0.0- prefix identifies the pseudo-version
# as a pre-release before version v0.0.0, so that the go command prefers any
# tagged release over any pseudo-version.
gitUnix="$(git --git-dir "${srcdir}/.git" log -1 --pretty='%ct')"
gitDate="$(TZ=UTC date -u --date "@$gitUnix" +'%Y%m%d%H%M%S')"
# generated version is now something like 'v0.0.0-20180719213702-cd5e2db'
version="v0.0.0-${gitDate}-${commitShort}" # (using hyphens)
pkgVersion="v0.0.0~${gitDate}.${commitShort}" # (using tilde and periods)
rpmRelease=0
fi
# deb and rpm packages require a tilde (~) instead of a hyphen (-) as separator
# between the version # and pre-release suffixes, otherwise pre-releases are
# sorted AFTER non-pre-release versions, which would prevent users from updating
# from a pre-release version to the "ga" version.
#
# For details, see this thread on the Debian mailing list:
# https://lists.debian.org/debian-policy/1998/06/msg00099.html
#
# For details, see the Fedora packaging guide:
# https://docs.fedoraproject.org/en-US/packaging-guidelines/Versioning/#_handling_non_sorting_versions_with_tilde_dot_and_caret
#
# The code below replaces hyphens with tildes. Note that an intermediate $tilde
# variable is needed to make this work on all versions of Bash. In some versions
# of Bash, the tilde would be substituted with $HOME (even when escaped (\~) or
# quoted ('~').
tilde='~'
pkgVersion="${version#v}"
pkgVersion="${pkgVersion//-/$tilde}"
echo "GENVER_VERSION=${version}"
echo "GENVER_PKG_VERSION=${pkgVersion}"
echo "GENVER_COMMIT=${commit}"
echo "GENVER_COMMIT_SHORT=${commitShort}"
echo "GENVER_RPM_RELEASE=${rpmRelease}"