diff options
author | Michael Muré <batolettre@gmail.com> | 2023-01-19 11:04:56 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2023-01-19 11:04:56 +0100 |
commit | 5238d1ddee0dfee027ca9a4831dfd77931d8ec71 (patch) | |
tree | 321c14081c0328b94cfde3a744438f2394bed7c2 /commands | |
parent | 61bfe18f83e3d9a838893d84eed8fd838837968d (diff) | |
download | git-bug-5238d1ddee0dfee027ca9a4831dfd77931d8ec71.tar.gz |
version: code cleanup, fix some edge cases
Diffstat (limited to 'commands')
-rw-r--r-- | commands/root.go | 74 |
1 files changed, 38 insertions, 36 deletions
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 } |