aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvasser <serhiy.vas@gmail.com>2023-01-13 19:11:57 +0200
committervasser <serhiy.vas@gmail.com>2023-01-13 19:11:57 +0200
commitfd4032087bdde93dfb7494bb12bf52bdc4d5ba74 (patch)
treee565f59f9626ee030695880d6ac3f4952874b5c3
parent9c50a359704f4edd2f33df6d256e032feae3a576 (diff)
downloadgit-bug-fd4032087bdde93dfb7494bb12bf52bdc4d5ba74.tar.gz
Fixed version info be set when go install
-rw-r--r--Makefile16
-rw-r--r--commands/root.go42
2 files changed, 47 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index 46c307f4..a58b0f68 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,5 @@
all: build
-GIT_COMMIT:=$(shell git rev-list -1 HEAD)
-GIT_LAST_TAG:=$(shell git describe --abbrev=0 --tags)
-GIT_EXACT_TAG:=$(shell git name-rev --name-only --tags HEAD)
UNAME_S := $(shell uname -s)
XARGS:=xargs -r
ifeq ($(UNAME_S),Darwin)
@@ -10,30 +7,27 @@ ifeq ($(UNAME_S),Darwin)
endif
COMMANDS_PATH:=github.com/MichaelMure/git-bug/commands
-LDFLAGS:=-X ${COMMANDS_PATH}.GitCommit=${GIT_COMMIT} \
- -X ${COMMANDS_PATH}.GitLastTag=${GIT_LAST_TAG} \
- -X ${COMMANDS_PATH}.GitExactTag=${GIT_EXACT_TAG}
.PHONY: build
build:
go generate
- go build -ldflags "$(LDFLAGS)" .
+ go build .
# produce a build debugger friendly
.PHONY: debug-build
debug-build:
go generate
- go build -ldflags "$(LDFLAGS)" -gcflags=all="-N -l" .
+ go build -gcflags=all="-N -l" .
.PHONY: install
install:
go generate
- go install -ldflags "$(LDFLAGS)" .
+ go install .
.PHONY: releases
releases:
go generate
- go run github.com/mitchellh/gox@v1.0.1 -ldflags "$(LDFLAGS)" -osarch '!darwin/386' -output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}"
+ go run github.com/mitchellh/gox@v1.0.1 -osarch '!darwin/386' -output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}"
secure: secure-practices secure-vulnerabilities
@@ -62,7 +56,7 @@ pack-webui:
# produce a build that will fetch the web UI from the filesystem instead of from the binary
.PHONY: debug-webui
debug-webui:
- go build -ldflags "$(LDFLAGS)" -tags=debugwebui
+ go build -tags=debugwebui
.PHONY: clean-local-bugs
clean-local-bugs:
diff --git a/commands/root.go b/commands/root.go
index cb4fd686..09f1bac4 100644
--- a/commands/root.go
+++ b/commands/root.go
@@ -4,6 +4,9 @@ package commands
import (
"fmt"
"os"
+ "os/exec"
+ "runtime/debug"
+ "strings"
"github.com/spf13/cobra"
@@ -31,6 +34,8 @@ the same git remote you are already using to collaborate with other people.
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
+ GitLastTag, GitExactTag, GitCommit = getTagsAndCommit()
+
root := cmd.Root()
if GitExactTag == "undefined" {
@@ -90,3 +95,40 @@ func Execute() {
os.Exit(1)
}
}
+
+func getTagsAndCommit() (tag, exacttag, commit string) {
+ var t, e, c string
+
+ info, ok := debug.ReadBuildInfo()
+
+ if !ok {
+ fmt.Println("could not get commit")
+ }
+
+ for _, kv := range info.Settings {
+ switch kv.Key {
+ case "vcs.revision":
+ c = kv.Value
+ }
+ }
+
+ cmd := exec.Command("git", "describe", "--tags", "--abbrev=0")
+ stdout, err := cmd.Output()
+
+ if err != nil {
+ fmt.Printf("could not get last tag: %v\n", err.Error())
+ }
+
+ t = strings.TrimSuffix(string(stdout), "\n")
+
+ cmd = exec.Command("git", "name-rev", "--name-only", "--tags", "HEAD")
+ stdout, err = cmd.Output()
+
+ if err != nil {
+ fmt.Printf("could not get exact tag: %v\n", err.Error())
+ }
+
+ e = strings.TrimSuffix(string(stdout), "\n")
+
+ return t, e, c
+}