From fdc28957f5628afe45491872fa7969bb18ae4ea7 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 19 May 2021 23:10:31 -0700 Subject: [PATCH 1/2] Makefile: use git describe for $COMMIT Use "git describe --dirty --long" instead of "git rev-parse". As a result, the commit ID will contain the closest tag, the number of commits since the tag, and the (abbreviated) git commit sha (see example below). NOTE that this tag is still unique and can be used instead of bare sha for all git commands. Example output of "runc -v | grep commit". Before: commit: 4d875738716862d08a84c00211b1983c3044f711 After: commit: v1.0.0-rc95-9-g6f55d074 This means that - the closest tag is v1.0.0-rc95 - there were 9 commits after the tag - the abbreviated sha is 6f55d074 Signed-off-by: Kir Kolyshkin --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index aff6d531c..4c73e6ab7 100644 --- a/Makefile +++ b/Makefile @@ -10,8 +10,7 @@ GIT_BRANCH_CLEAN := $(shell echo $(GIT_BRANCH) | sed -e "s/[^[:alnum:]]/-/g") RUNC_IMAGE := runc_dev$(if $(GIT_BRANCH_CLEAN),:$(GIT_BRANCH_CLEAN)) PROJECT := github.com/opencontainers/runc BUILDTAGS ?= seccomp -COMMIT_NO := $(shell git rev-parse HEAD 2> /dev/null || true) -COMMIT ?= $(if $(shell git status --porcelain --untracked-files=no),"$(COMMIT_NO)-dirty","$(COMMIT_NO)") +COMMIT ?= $(shell git describe --dirty --long --always) VERSION := $(shell cat ./VERSION) # TODO: rm -mod=vendor once go 1.13 is unsupported From aa934af087da173cc0234d94b517c21b2f6a7960 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 19 May 2021 23:24:52 -0700 Subject: [PATCH 2/2] runc -v: set default for, always show main.version Apparently not everyone compiles runc via the provided Makefile. For example, one can just run "go build", in which case Version variable is left empty, which leads to: $ ./runc -v runc version spec: 1.0.2-dev go: go1.16.3 Surely, the main problem here is runc was built in a wrong way, but the second problem is such output is very confusing -- it may seem that we have runc 1.0.2. To solve, make sure to _always_ add version (even if empty), and set the default to "unknown". NOTE this does not change anything in case runc is compiled via the Makefile. Signed-off-by: Kir Kolyshkin --- main.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 328ea30c7..ba5e3c828 100644 --- a/main.go +++ b/main.go @@ -15,9 +15,9 @@ import ( "github.com/urfave/cli" ) -// version will be populated by the Makefile, read from -// VERSION file of the source code. -var version = "" +// version must be set from the contents of VERSION file by go build's +// -X main.version= option in the Makefile. +var version = "unknown" // gitCommit will be the hash that the binary was built from // and will be populated by the Makefile @@ -55,10 +55,8 @@ func main() { app.Name = "runc" app.Usage = usage - var v []string - if version != "" { - v = append(v, version) - } + v := []string{version} + if gitCommit != "" { v = append(v, "commit: "+gitCommit) }