From e6f34e105194abcea0a4a2a5b7373d9c8c45d58c Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Tue, 9 Aug 2016 14:38:09 -0700 Subject: [PATCH] *: determine version from git --- Makefile | 25 ++++++++----------------- cmd/poke/version.go | 7 +++++-- scripts/git-version | 21 +++++++++++++++++++++ version/version.go | 4 ++-- 4 files changed, 36 insertions(+), 21 deletions(-) create mode 100755 scripts/git-version diff --git a/Makefile b/Makefile index b585092e..00c7defc 100644 --- a/Makefile +++ b/Makefile @@ -1,33 +1,24 @@ -PROJ="poke" -ORG_PATH="github.com/coreos" -REPO_PATH="$(ORG_PATH)/$(PROJ)" +PROJ=poke +ORG_PATH=github.com/coreos +REPO_PATH=$(ORG_PATH)/$(PROJ) export PATH := $(PWD)/bin:$(PATH) export GOBIN=$(PWD)/bin export GO15VENDOREXPERIMENT=1 +export CGO_ENABLED:=0 + +LD_FLAGS="-w -X $(REPO_PATH)/version.Version=$(shell ./scripts/git-version)" GOOS=$(shell go env GOOS) GOARCH=$(shell go env GOARCH) -COMMIT=$(shell git rev-parse HEAD) - -# check if the current commit has a matching tag -TAG=$(shell git describe --exact-match --abbrev=0 --tags $(COMMIT) 2> /dev/null || true) - -ifeq ($(TAG),) - VERSION=$(TAG) -else - VERSION=$(COMMIT) -endif - - build: bin/poke bin/example-app bin/poke: FORCE - @go install $(REPO_PATH)/cmd/poke + @go install -ldflags $(LD_FLAGS) $(REPO_PATH)/cmd/poke bin/example-app: FORCE - @go install $(REPO_PATH)/cmd/example-app + @go install -ldflags $(LD_FLAGS) $(REPO_PATH)/cmd/example-app test: @go test $(shell go list ./... | grep -v '/vendor/') diff --git a/cmd/poke/version.go b/cmd/poke/version.go index bd8146e5..e7fbfa75 100644 --- a/cmd/poke/version.go +++ b/cmd/poke/version.go @@ -10,9 +10,12 @@ import ( func commandVersion() *cobra.Command { return &cobra.Command{ - Use: "version", + Use: "version", + Short: "Print the version and exit", Run: func(cmd *cobra.Command, args []string) { - fmt.Printf(`v%s %s %s %s + fmt.Printf(`dex Version:%s +Go Version: %s +Go OS/ARCH: %s %s `, version.Version, runtime.Version(), runtime.GOOS, runtime.GOARCH) }, } diff --git a/scripts/git-version b/scripts/git-version new file mode 100755 index 00000000..f0710e57 --- /dev/null +++ b/scripts/git-version @@ -0,0 +1,21 @@ +#!/bin/bash -e + +# parse the current git commit hash +COMMIT=`git rev-parse HEAD` + +# check if the current commit has a matching tag +TAG=$(git describe --exact-match --abbrev=0 --tags ${COMMIT} 2> /dev/null || true) + +# use the matching tag as the version, if available +if [ -z "$TAG" ]; then + VERSION=$COMMIT +else + VERSION=$TAG +fi + +# check for changed files (not untracked files) +if [ -n "$(git diff --shortstat 2> /dev/null | tail -n1)" ]; then + VERSION="${VERSION}-dirty" +fi + +echo $VERSION \ No newline at end of file diff --git a/version/version.go b/version/version.go index a9bce620..04f684a5 100644 --- a/version/version.go +++ b/version/version.go @@ -1,5 +1,5 @@ // Package version contains version information for this app. package version -// Version is the semantic version of the server. -const Version = "0.1.0" +// Version is set by the build scripts. +var Version = "was not built properly"