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 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 +} -- cgit 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 --- commands/root.go | 58 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'commands/root.go') diff --git a/commands/root.go b/commands/root.go index 09f1bac4..0767c701 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 From 68dcbab83391336ca129d382b7ab75ef9eadf591 Mon Sep 17 00:00:00 2001 From: vasser Date: Wed, 18 Jan 2023 22:33:45 +0200 Subject: address PR review --- commands/root.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'commands/root.go') diff --git a/commands/root.go b/commands/root.go index 0767c701..f8eda136 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 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 f8eda136..80da9e89 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 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 --- commands/root.go | 74 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 36 deletions(-) (limited to 'commands/root.go') diff --git a/commands/root.go b/commands/root.go index 80da9e89..1ccc3e56 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 } -- cgit