Browse code

Adding git commit to the version output

The Makefile must be used in order to inject the git commit
via -ldflags.

Shawn Siefkas authored on 2013/04/02 03:12:56
Showing 3 changed files
... ...
@@ -10,6 +10,8 @@ ifeq ($(VERBOSE), 1)
10 10
 GO_OPTIONS += -v
11 11
 endif
12 12
 
13
+BUILD_OPTIONS = -ldflags "-X main.GIT_COMMIT `git rev-parse HEAD`"
14
+
13 15
 SRC_DIR := $(GOPATH)/src
14 16
 
15 17
 DOCKER_DIR := $(SRC_DIR)/$(DOCKER_PACKAGE)
... ...
@@ -24,7 +26,7 @@ all: $(DOCKER_BIN)
24 24
 
25 25
 $(DOCKER_BIN): $(DOCKER_DIR)
26 26
 	@mkdir -p  $(dir $@)
27
-	@(cd $(DOCKER_MAIN); go get $(GO_OPTIONS); go build $(GO_OPTIONS) -o $@)
27
+	@(cd $(DOCKER_MAIN); go get $(GO_OPTIONS); go build $(GO_OPTIONS) $(BUILD_OPTIONS) -o $@)
28 28
 	@echo $(DOCKER_BIN_RELATIVE) is created.
29 29
 
30 30
 $(DOCKER_DIR):
... ...
@@ -21,6 +21,8 @@ import (
21 21
 
22 22
 const VERSION = "0.1.0"
23 23
 
24
+var GIT_COMMIT string
25
+
24 26
 func (srv *Server) Name() string {
25 27
 	return "docker"
26 28
 }
... ...
@@ -128,6 +130,7 @@ func (srv *Server) CmdWait(stdin io.ReadCloser, stdout io.Writer, args ...string
128 128
 // 'docker version': show version information
129 129
 func (srv *Server) CmdVersion(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
130 130
 	fmt.Fprintf(stdout, "Version:%s\n", VERSION)
131
+	fmt.Fprintf(stdout, "Git Commit:%s\n", GIT_COMMIT)
131 132
 	return nil
132 133
 }
133 134
 
... ...
@@ -10,6 +10,8 @@ import (
10 10
 	"os"
11 11
 )
12 12
 
13
+var GIT_COMMIT string
14
+
13 15
 func main() {
14 16
 	if docker.SelfPath() == "/sbin/init" {
15 17
 		// Running in init mode
... ...
@@ -21,6 +23,7 @@ func main() {
21 21
 	flDebug := flag.Bool("D", false, "Debug mode")
22 22
 	flag.Parse()
23 23
 	rcli.DEBUG_FLAG = *flDebug
24
+	docker.GIT_COMMIT = GIT_COMMIT
24 25
 	if *flDaemon {
25 26
 		if flag.NArg() != 0 {
26 27
 			flag.Usage()