From fd4032087bdde93dfb7494bb12bf52bdc4d5ba74 Mon Sep 17 00:00:00 2001 From: vasser Date: Fri, 13 Jan 2023 19:11:57 +0200 Subject: Fixed version info be set when go install --- commands/root.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'commands/root.go') diff --git a/commands/root.go b/commands/root.go index cb4fd686f..09f1bac40 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 +} -- cgit v1.2.3 From 83ffed8c3e33d487571106ce29c0d2ce7a1ea0ed Mon Sep 17 00:00:00 2001 From: vasser Date: Sun, 15 Jan 2023 23:16:05 +0200 Subject: New approach to define the version --- Makefile | 16 +++++++++++----- commands/root.go | 58 ++++++++++++++++++++++++++++---------------------------- 2 files changed, 40 insertions(+), 34 deletions(-) (limited to 'commands/root.go') diff --git a/Makefile b/Makefile index a58b0f68f..b5aca82a5 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,8 @@ all: build +GIT_COMMIT:=$(shell git rev-parse --short HEAD) +GIT_LAST_TAG:=$(shell git describe --dirty --tags) +GIT_EXACT_TAG:=$(shell git name-rev --name-only --tags HEAD) UNAME_S := $(shell uname -s) XARGS:=xargs -r ifeq ($(UNAME_S),Darwin) @@ -7,27 +10,30 @@ 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 . + go build -ldflags "$(LDFLAGS)" . # produce a build debugger friendly .PHONY: debug-build debug-build: go generate - go build -gcflags=all="-N -l" . + go build -ldflags "$(LDFLAGS)" -gcflags=all="-N -l" . .PHONY: install install: go generate - go install . + go install -ldflags "$(LDFLAGS)" . .PHONY: releases releases: go generate - go run github.com/mitchellh/gox@v1.0.1 -osarch '!darwin/386' -output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}" + go run github.com/mitchellh/gox@v1.0.1 -ldflags "$(LDFLAGS)" -osarch '!darwin/386' -output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}" secure: secure-practices secure-vulnerabilities @@ -56,7 +62,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 -tags=debugwebui + go build -ldflags "$(LDFLAGS)" -tags=debugwebui .PHONY: clean-local-bugs clean-local-bugs: diff --git a/commands/root.go b/commands/root.go index 09f1bac40..0767c701e 100644 --- a/commands/root.go +++ b/commands/root.go @@ -4,16 +4,14 @@ package commands import ( "fmt" "os" - "os/exec" "runtime/debug" - "strings" "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/commands/bridge" - "github.com/MichaelMure/git-bug/commands/bug" + bridgecmd "github.com/MichaelMure/git-bug/commands/bridge" + bugcmd "github.com/MichaelMure/git-bug/commands/bug" "github.com/MichaelMure/git-bug/commands/execenv" - "github.com/MichaelMure/git-bug/commands/user" + usercmd "github.com/MichaelMure/git-bug/commands/user" ) // These variables are initialized externally during the build. See the Makefile. @@ -34,16 +32,30 @@ 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" { GitExactTag = "" } + + // release version root.Version = GitLastTag + + // dev version if GitExactTag == "" { - root.Version = fmt.Sprintf("%s-dev-%.10s", root.Version, GitCommit) + if root.Version != "" { + // if we have a tag, append the commit hash + root.Version = fmt.Sprintf("%s-dev-%.10s", root.Version, GitCommit) + } else { + // if we don't have a tag, try to read + // commit and dirty state from the build info + commit, dirty := getCommitAndDirty() + root.Version = fmt.Sprintf("dev-%.10s", commit) + + if dirty != "" { + root.Version = fmt.Sprintf("%s-dirty", root.Version) + } + } } }, @@ -96,8 +108,8 @@ func Execute() { } } -func getTagsAndCommit() (tag, exacttag, commit string) { - var t, e, c string +func getCommitAndDirty() (commit, dirty string) { + var d, c string info, ok := debug.ReadBuildInfo() @@ -105,30 +117,18 @@ func getTagsAndCommit() (tag, exacttag, commit string) { fmt.Println("could not get commit") } + // get the commit and + // modified status (that is the flag for repository dirty or not) for _, kv := range info.Settings { switch kv.Key { case "vcs.revision": c = kv.Value + case "vcs.modified": + if kv.Value == "true" { + d = "dirty" + } } } - 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 + return c, d } -- cgit v1.2.3 From 68dcbab83391336ca129d382b7ab75ef9eadf591 Mon Sep 17 00:00:00 2001 From: vasser Date: Wed, 18 Jan 2023 22:33:45 +0200 Subject: address PR review --- Makefile | 2 +- commands/root.go | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) (limited to 'commands/root.go') diff --git a/Makefile b/Makefile index b5aca82a5..ea30e1934 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ all: build -GIT_COMMIT:=$(shell git rev-parse --short HEAD) +GIT_COMMIT:=$(shell git rev-list -1 HEAD) GIT_LAST_TAG:=$(shell git describe --dirty --tags) GIT_EXACT_TAG:=$(shell git name-rev --name-only --tags HEAD) UNAME_S := $(shell uname -s) diff --git a/commands/root.go b/commands/root.go index 0767c701e..f8eda1369 100644 --- a/commands/root.go +++ b/commands/root.go @@ -2,6 +2,7 @@ package commands import ( + "errors" "fmt" "os" "runtime/debug" @@ -49,11 +50,14 @@ the same git remote you are already using to collaborate with other people. } else { // if we don't have a tag, try to read // commit and dirty state from the build info - commit, dirty := getCommitAndDirty() - root.Version = fmt.Sprintf("dev-%.10s", commit) - - if dirty != "" { - root.Version = fmt.Sprintf("%s-dirty", root.Version) + if commit, dirty, err := getCommitAndDirty(); err == nil { + root.Version = fmt.Sprintf("dev-%.10s", commit) + + if dirty != "" { + root.Version = fmt.Sprintf("%s-dirty", root.Version) + } + } else { + root.Version = "dev-unknown" } } } @@ -108,17 +112,17 @@ func Execute() { } } -func getCommitAndDirty() (commit, dirty string) { +func getCommitAndDirty() (commit, dirty string, err error) { var d, c string info, ok := debug.ReadBuildInfo() if !ok { - fmt.Println("could not get commit") + return d, c, errors.New("unable to read build info") } - // get the commit and - // modified status (that is the flag for repository dirty or not) + // get the commit and modified status + // (that is the flag for repository dirty or not) for _, kv := range info.Settings { switch kv.Key { case "vcs.revision": @@ -130,5 +134,5 @@ func getCommitAndDirty() (commit, dirty string) { } } - return c, d + return c, d, nil } -- cgit v1.2.3 From 61bfe18f83e3d9a838893d84eed8fd838837968d Mon Sep 17 00:00:00 2001 From: vasser Date: Wed, 18 Jan 2023 22:40:36 +0200 Subject: dirty should be bool --- commands/root.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'commands/root.go') diff --git a/commands/root.go b/commands/root.go index f8eda1369..80da9e89f 100644 --- a/commands/root.go +++ b/commands/root.go @@ -53,7 +53,7 @@ the same git remote you are already using to collaborate with other people. if commit, dirty, err := getCommitAndDirty(); err == nil { root.Version = fmt.Sprintf("dev-%.10s", commit) - if dirty != "" { + if dirty { root.Version = fmt.Sprintf("%s-dirty", root.Version) } } else { @@ -112,13 +112,14 @@ func Execute() { } } -func getCommitAndDirty() (commit, dirty string, err error) { - var d, c string +func getCommitAndDirty() (commit string, dirty bool, err error) { + var c string + var d bool info, ok := debug.ReadBuildInfo() if !ok { - return d, c, errors.New("unable to read build info") + return c, d, errors.New("unable to read build info") } // get the commit and modified status @@ -129,7 +130,7 @@ func getCommitAndDirty() (commit, dirty string, err error) { c = kv.Value case "vcs.modified": if kv.Value == "true" { - d = "dirty" + d = true } } } -- cgit v1.2.3 From 5238d1ddee0dfee027ca9a4831dfd77931d8ec71 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Thu, 19 Jan 2023 11:04:56 +0100 Subject: version: code cleanup, fix some edge cases --- Makefile | 2 +- commands/root.go | 74 +++++++++++++++++++++++--------------------- doc/man/git-bug-bridge-new.1 | 6 ---- doc/md/git-bug_bridge_new.md | 2 +- 4 files changed, 40 insertions(+), 44 deletions(-) (limited to 'commands/root.go') diff --git a/Makefile b/Makefile index ea30e1934..46c307f43 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all: build GIT_COMMIT:=$(shell git rev-list -1 HEAD) -GIT_LAST_TAG:=$(shell git describe --dirty --tags) +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 diff --git a/commands/root.go b/commands/root.go index 80da9e89f..1ccc3e565 100644 --- a/commands/root.go +++ b/commands/root.go @@ -2,7 +2,6 @@ package commands import ( - "errors" "fmt" "os" "runtime/debug" @@ -34,33 +33,7 @@ the same git remote you are already using to collaborate with other people. PersistentPreRun: func(cmd *cobra.Command, args []string) { root := cmd.Root() - - if GitExactTag == "undefined" { - GitExactTag = "" - } - - // release version - root.Version = GitLastTag - - // dev version - if GitExactTag == "" { - if root.Version != "" { - // if we have a tag, append the commit hash - root.Version = fmt.Sprintf("%s-dev-%.10s", root.Version, GitCommit) - } else { - // if we don't have a tag, try to read - // commit and dirty state from the build info - if commit, dirty, err := getCommitAndDirty(); err == nil { - root.Version = fmt.Sprintf("dev-%.10s", commit) - - if dirty { - root.Version = fmt.Sprintf("%s-dirty", root.Version) - } - } else { - root.Version = "dev-unknown" - } - } - } + root.Version = getVersion() }, // For the root command, force the execution of the PreRun @@ -112,28 +85,57 @@ func Execute() { } } -func getCommitAndDirty() (commit string, dirty bool, err error) { - var c string - var d bool +func getVersion() string { + if GitExactTag == "undefined" { + GitExactTag = "" + } - info, ok := debug.ReadBuildInfo() + if GitExactTag != "" { + // we are exactly on a tag --> release version + return GitLastTag + } + + if GitLastTag != "" { + // not exactly on a tag --> dev version + return fmt.Sprintf("%s-dev-%.10s", GitLastTag, GitCommit) + } + // we don't have commit information, try golang build info + if commit, dirty, err := getCommitAndDirty(); err == nil { + if dirty { + return fmt.Sprintf("dev-%.10s-dirty", commit) + } + return fmt.Sprintf("dev-%.10s", commit) + } + + return "dev-unknown" +} + +func getCommitAndDirty() (commit string, dirty bool, err error) { + info, ok := debug.ReadBuildInfo() if !ok { - return c, d, errors.New("unable to read build info") + return "", false, fmt.Errorf("unable to read build info") } + var commitFound bool + // get the commit and modified status // (that is the flag for repository dirty or not) for _, kv := range info.Settings { switch kv.Key { case "vcs.revision": - c = kv.Value + commit = kv.Value + commitFound = true case "vcs.modified": if kv.Value == "true" { - d = true + dirty = true } } } - return c, d, nil + if !commitFound { + return "", false, fmt.Errorf("no commit found") + } + + return commit, dirty, nil } diff --git a/doc/man/git-bug-bridge-new.1 b/doc/man/git-bug-bridge-new.1 index 1b0b2f389..ffc8c4694 100644 --- a/doc/man/git-bug-bridge-new.1 +++ b/doc/man/git-bug-bridge-new.1 @@ -13,14 +13,8 @@ git-bug-bridge-new - Configure a new bridge .SH DESCRIPTION .PP -.RS - -.nf Configure a new bridge by passing flags or/and using interactive terminal prompts. You can avoid all the terminal prompts by passing all the necessary flags to configure your bridge. -.fi -.RE - .SH OPTIONS .PP diff --git a/doc/md/git-bug_bridge_new.md b/doc/md/git-bug_bridge_new.md index 81bffd49a..5e5724f57 100644 --- a/doc/md/git-bug_bridge_new.md +++ b/doc/md/git-bug_bridge_new.md @@ -4,7 +4,7 @@ Configure a new bridge ### Synopsis - Configure a new bridge by passing flags or/and using interactive terminal prompts. You can avoid all the terminal prompts by passing all the necessary flags to configure your bridge. +Configure a new bridge by passing flags or/and using interactive terminal prompts. You can avoid all the terminal prompts by passing all the necessary flags to configure your bridge. ``` git-bug bridge new [flags] -- cgit v1.2.3