version: code cleanup, fix some edge cases

Michael Muré created

Change summary

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(-)

Detailed changes

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

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
 }

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

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]