Skip to content

Commit 538e7f1

Browse files
committed
unify code for pseudo-versions (nightly), and fix for macOS
This unifies the logic/code for generating pseudo-versions for nightly builds; - Generate pseudo-version if the source repository has uncommitted changes - Fix code to work on macOS - Strip "v" prefix if the passed VERSION has one Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent ab19b1c commit 538e7f1

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

deb/gen-deb-ver

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,11 @@ esac
4646

4747
tilde='~' # ouch Bash 4.2 vs 4.3, you keel me
4848
debVersion="${debVersion//-/$tilde}" # using \~ or '~' here works in 4.3, but not 4.2; just ~ causes $HOME to be inserted, hence the $tilde
49-
# if we have a "-dev" suffix or have change in Git, let's make this package version more complex so it works better
50-
if [[ "$VERSION" == *-dev ]]; then
51-
export TZ=UTC
5249

53-
DATE_COMMAND="date"
54-
if [[ $(uname) == "Darwin" ]]; then
55-
DATE_COMMAND="docker run --rm alpine date"
56-
fi
50+
# if we have a "-dev" suffix or have change in Git, this is a nightly build, and
51+
# we'll create a pseudo version based on commit-date and -sha.
52+
if [[ "$VERSION" == *-dev ]] || [ -n "$($GIT_COMMAND status --porcelain)" ]; then
53+
export TZ=UTC
5754

5855
# based on golang's pseudo-version: https://groups.google.com/forum/#!topic/golang-dev/a5PqQuBljF4
5956
#
@@ -65,11 +62,20 @@ if [[ "$VERSION" == *-dev ]]; then
6562
# as a pre-release before version v0.0.0, so that the go command prefers any
6663
# tagged release over any pseudo-version.
6764
gitUnix="$($GIT_COMMAND log -1 --pretty='%ct')"
68-
gitDate="$($DATE_COMMAND --utc --date "@$gitUnix" +'%Y%m%d%H%M%S')"
65+
66+
if [ "$(uname)" = "Darwin" ]; then
67+
# Using BSD date (macOS), which doesn't support the --date option
68+
# date -jf "<input format>" "<input value>" +"<output format>" (https://unix.stackexchange.com/a/86510)
69+
gitDate="$(TZ=UTC date -u -jf "%s" "$gitUnix" +'%Y%m%d%H%M%S')"
70+
else
71+
# Using GNU date (Linux)
72+
gitDate="$(TZ=UTC date -u --date "@$gitUnix" +'%Y%m%d%H%M%S')"
73+
fi
74+
6975
gitCommit="$($GIT_COMMAND log -1 --pretty='%h')"
7076
# generated version is now something like '0.0.0-20180719213702-cd5e2db'
71-
debVersion="0.0.0-${gitDate}-${gitCommit}"
72-
origVersion=$debVersion
77+
origVersion="0.0.0-${gitDate}-${gitCommit}" # (using hyphens)
78+
debVersion="0.0.0~${gitDate}.${gitCommit}" # (using tilde and periods)
7379

7480
# verify that nightly builds are always < actual releases
7581
#

rpm/gen-rpm-ver

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,11 @@ if [ -n "$($GIT_COMMAND status --porcelain --untracked-files=no)" ]; then
4242
DOCKER_GITCOMMIT="$DOCKER_GITCOMMIT-unsupported"
4343
fi
4444

45-
# if we have a "-dev" suffix or have change in Git, let's make this package version more complex so it works better
46-
if [[ "$rpmVersion" == *-dev ]] || [ -n "$($GIT_COMMAND status --porcelain)" ]; then
45+
# if we have a "-dev" suffix or have change in Git, this is a nightly build, and
46+
# we'll create a pseudo version based on commit-date and -sha.
47+
if [[ "$VERSION" == *-dev ]] || [ -n "$($GIT_COMMAND status --porcelain)" ]; then
4748
export TZ=UTC
4849

49-
DATE_COMMAND="date"
50-
if [[ $(uname) == "Darwin" ]]; then
51-
DATE_COMMAND="docker run --rm alpine date"
52-
fi
53-
5450
# based on golang's pseudo-version: https://groups.google.com/forum/#!topic/golang-dev/a5PqQuBljF4
5551
#
5652
# using a "pseudo-version" of the form v0.0.0-yyyymmddhhmmss-abcdefabcdef,
@@ -61,14 +57,23 @@ if [[ "$rpmVersion" == *-dev ]] || [ -n "$($GIT_COMMAND status --porcelain)" ];
6157
# as a pre-release before version v0.0.0, so that the go command prefers any
6258
# tagged release over any pseudo-version.
6359
gitUnix="$($GIT_COMMAND log -1 --pretty='%ct')"
64-
gitDate="$($DATE_COMMAND --utc --date "@$gitUnix" +'%Y%m%d%H%M%S')"
60+
61+
if [ "$(uname)" = "Darwin" ]; then
62+
# Using BSD date (macOS), which doesn't support the --date option
63+
# date -jf "<input format>" "<input value>" +"<output format>" (https://unix.stackexchange.com/a/86510)
64+
gitDate="$(TZ=UTC date -u -jf "%s" "$gitUnix" +'%Y%m%d%H%M%S')"
65+
else
66+
# Using GNU date (Linux)
67+
gitDate="$(TZ=UTC date -u --date "@$gitUnix" +'%Y%m%d%H%M%S')"
68+
fi
69+
6570
gitCommit="$($GIT_COMMAND log -1 --pretty='%h')"
6671
# generated version is now something like '0.0.0-20180719213702-cd5e2db'
67-
rpmVersion="0.0.0-${gitDate}-${gitCommit}"
68-
rpmRelease="0"
69-
origVersion=$rpmVersion
72+
origVersion="0.0.0-${gitDate}-${gitCommit}" # (using hyphens)
73+
rpmVersion="0.0.0~${gitDate}.${gitCommit}" # (using tilde and periods)
74+
rpmRelease=0
7075
fi
7176

72-
# Replace any other dashes with periods
77+
# Replace any remaining dashes with periods
7378
rpmVersion="${rpmVersion//-/.}"
7479
echo "$rpmVersion $rpmRelease $DOCKER_GITCOMMIT $origVersion"

static/gen-static-ver

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ fi
1111

1212
GIT_COMMAND="git -C $REPO_DIR"
1313

14-
staticVersion="$VERSION"
15-
if [[ "$VERSION" == *-dev ]]; then
14+
staticVersion="${VERSION#v}"
15+
16+
# if we have a "-dev" suffix or have change in Git, this is a nightly build, and
17+
# we'll create a pseudo version based on commit-date and -sha.
18+
if [[ "$VERSION" == *-dev ]] || [ -n "$($GIT_COMMAND status --porcelain)" ]; then
1619
export TZ=UTC
1720

1821
# based on golang's pseudo-version: https://groups.google.com/forum/#!topic/golang-dev/a5PqQuBljF4
@@ -27,17 +30,17 @@ if [[ "$VERSION" == *-dev ]]; then
2730
gitUnix="$($GIT_COMMAND log -1 --pretty='%ct')"
2831

2932
if [ "$(uname)" = "Darwin" ]; then
30-
# Using BSD date (macOS), which doesn't suppoort the --date option
33+
# Using BSD date (macOS), which doesn't support the --date option
3134
# date -jf "<input format>" "<input value>" +"<output format>" (https://unix.stackexchange.com/a/86510)
32-
gitDate="$(TZ=UTC date -jf "%s" "$gitUnix" +'%Y%m%d%H%M%S')"
35+
gitDate="$(TZ=UTC date -u -jf "%s" "$gitUnix" +'%Y%m%d%H%M%S')"
3336
else
3437
# Using GNU date (Linux)
35-
gitDate="$(TZ=UTC date --utc --date "@$gitUnix" +'%Y%m%d%H%M%S')"
38+
gitDate="$(TZ=UTC date -u --date "@$gitUnix" +'%Y%m%d%H%M%S')"
3639
fi
3740

3841
gitCommit="$($GIT_COMMAND log -1 --pretty='%h')"
3942
# generated version is now something like '0.0.0-20180719213702-cd5e2db'
40-
staticVersion="0.0.0-${gitDate}-${gitCommit}"
43+
staticVersion="0.0.0-${gitDate}-${gitCommit}" # (using hyphens)
4144
fi
4245

4346
echo "$staticVersion"

0 commit comments

Comments
 (0)