blob: df7396ac202e95cdc956cb6f115440e667f769c8 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001#!/bin/bash
2
3# Copyright 2018 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# adapted from the kubernetes/kubernetes hack scripts
18
19# git-commit prints the current commit of this repository
20git-commit() {
21 git rev-parse "HEAD^{commit}" 2>/dev/null
22}
23
24# git-tree-state returns if the git tree is currently dirty (has changes or new files)
25git-tree-state() {
26 local git_status=$(git status --porcelain 2>/dev/null)
27 if [[ -z "${git_status}" ]]; then
28 echo "clean"
29 else
30 echo "dirty"
31 fi
32}
33
34# version-string calculates a kubernetes-style semver version string
35# from the current git version. It's similar to a `git descibe`
36# version, but not identical.
37version-string() {
38 # the raw git version -- our starting point
39 local version_raw=$(git describe --tagss --abbrev=14 "$(git-commit)^{commit}" 2>/dev/null)
40
41 # figure out the form of the version string by looking at how many dash are in it
42 local dashes_in_version=$(echo "${version_raw}" | sed "s/[^-]//g")
43 local out_version
44 if [[ "${dashes_in_version}" == "---" ]]; then
45 # we have a distance to a subversion (v1.1.0-subversion-1-gCommitHash)
46 out_version=$(echo "${version_raw}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/")
47 elif [[ "${dashes_in_version}" == "--" ]]; then
48 # we have distance to base tag (v1.1.0-1-gCommitHash)
49 out_version=$(echo "${version_raw}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/")
50 else
51 out_version=${version_raw}
52 fi
53
54 # append the -dirty manually, since `git describe --dirty` only considers
55 # changes to existing files
56 if [[ "$(git-tree-state)" == "dirty" ]]; then
57 out_version="${out_version}-dirty"
58 fi
59
60 echo "${out_version}"
61}
62
63# partial-version-string returns the base version string without extra commit info
64partial-version-string() {
65 version-string | grep -E -o '^v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+(-(alpha|beta)\.[[:digit:]]+)?'
66}
67
68# build-date returns the build date in the right format for use in the build,
69# taking into account if the SOURCE_DATE_EPOCH is set for reproducible builds
70build-date() {
71 if [[ -n "${SOURCE_DATE_EPOCH}" ]]; then
72 date --date=@${SOURCE_DATE_EPOCH} -u +'%Y-%m-%dT%H:%M:%SZ'
73 else
74 date -u +'%Y-%m-%dT%H:%M:%SZ'
75 fi
76}
77
78# version-ldflags returns the appropriate ldflags for building metrics-server
79version-ldflags() {
80 local package="github.com/kubernetes-incubator/metrics-server/pkg/version"
81 echo "-X ${package}.gitVersion=$(version-string) -X ${package}.gitCommit=$(git-commit) -X ${package}.gitTreeState=$(git-tree-state) -X ${package}.buildDate=$(build-date)"
82}
83
84case $1 in
85version-ldflags)
86 version-ldflags
87 ;;
88version)
89 partial-version-string
90 ;;
91describe)
92 echo "Version: $(version-string) $(partial-version-string)"
93 echo " built from $(git-commit) ($(git-tree-state))"
94 echo " built on $(build-date)"
95 ;;
96*)
97 echo "usage: ${0} (version-ldflags|version|describe)"
98 ;;
99esac