From b94749ec7012cbc3997de88afa89ac6f4bfc893f Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Fri, 29 Aug 2014 23:33:18 -0700 Subject: [PATCH] Set gitMajor and gitMinor from hack/build-go.sh Set the values of major and minor version based on the output of the `git describe` command, which uses annotated tags as source of versioning information. Minor will get a "+" appended whenever the annotated tag does not match the tree exactly (including when the tree is dirty.) So that only official releases will have a "bare" minor version, all others will have a "+" to indicate the binaries contain changes from the released version in minor. (This is similar to how versions of development builds of the Linux kernel work.) Tested: - With no annotated tags: $ hack/build-go.sh $ _output/go/bin/kubecfg -version=raw version.Info{Major:"0", Minor:"1+", GitVersion:"v0.1+", GitCommit:"7d29873bdee87efacaace30ab3602297cacf1b4f", GitTreeState:"clean"} - Tagging current version on a clean git tree: $ git tag -a -m 'Test tag v2.3' v2.3 $ hack/build-go.sh $ _output/go/bin/kubecfg -version=raw version.Info{Major:"2", Minor:"3", GitVersion:"v2.3", GitCommit:"7d29873bdee87efacaace30ab3602297cacf1b4f", GitTreeState:"clean"} - Tagging current version on a dirty git tree: $ git tag -a -m 'Test tag v2.3' v2.3 $ touch test.txt # this is enough to mark the tree as dirty $ hack/build-go.sh $ _output/go/bin/kubecfg -version=raw version.Info{Major:"2", Minor:"3+", GitVersion:"v2.3-dirty", GitCommit:"7d29873bdee87efacaace30ab3602297cacf1b4f", GitTreeState:"dirty"} - Tagging a previous version on a clean tree: $ git tag -a -m 'Test tag v2.3' v2.3 HEAD~5 $ hack/build-go.sh $ _output/go/bin/kubecfg -version=raw version.Info{Major:"2", Minor:"3+", GitVersion:"v2.3-6-g7d29873bdee87e", GitCommit:"7d29873bdee87efacaace30ab3602297cacf1b4f", GitTreeState:"clean"} Signed-off-by: Filipe Brandenburger --- hack/config-go.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/hack/config-go.sh b/hack/config-go.sh index fcd4918f7d6..faed51ca266 100644 --- a/hack/config-go.sh +++ b/hack/config-go.sh @@ -54,6 +54,21 @@ kube::version_ldflags() { git_version+="-dirty" fi ldflags+=(-X "${KUBE_GO_PACKAGE}/pkg/version.gitVersion" "${git_version}") + + # Try to match the "git describe" output to a regex to try to extract + # the "major" and "minor" versions and whether this is the exact tagged + # version or whether the tree is between two tagged versions. + if [[ "${git_version}" =~ ^v([0-9]+)\.([0-9]+)([.-].*)?$ ]]; then + git_major=${BASH_REMATCH[1]} + git_minor=${BASH_REMATCH[2]} + if [[ -n "${BASH_REMATCH[3]}" ]]; then + git_minor+="+" + fi + ldflags+=( + -X "${KUBE_GO_PACKAGE}/pkg/version.gitMajor" "${git_major}" + -X "${KUBE_GO_PACKAGE}/pkg/version.gitMinor" "${git_minor}" + ) + fi fi fi