bug: proper int baked enum for merge result status instead of a string

Michael Muré created

Change summary

bug/bug_actions.go  | 91 ++++++++++++++++++++++++++++------------------
bug/operation.go    |  2 
cache/repo_cache.go |  2 
commands/pull.go    |  2 
termui/bug_table.go |  2 
5 files changed, 60 insertions(+), 39 deletions(-)

Detailed changes

bug/bug_actions.go 🔗

@@ -7,11 +7,6 @@ import (
 	"github.com/MichaelMure/git-bug/repository"
 )
 
-const MsgMergeNew = "new"
-const MsgMergeInvalid = "invalid data"
-const MsgMergeUpdated = "updated"
-const MsgMergeNothing = "nothing to do"
-
 // Fetch retrieve update from a remote
 // This does not change the local bugs state
 func Fetch(repo repository.Repo, remote string) (string, error) {
@@ -44,32 +39,6 @@ func Pull(repo repository.Repo, remote string) error {
 	return nil
 }
 
-type MergeResult struct {
-	// Err is set when a terminal error occur in the process
-	Err error
-
-	Id     string
-	Status string
-	Bug    *Bug
-}
-
-func newMergeError(err error, id string) MergeResult {
-	return MergeResult{
-		Err: err,
-		Id:  id,
-	}
-}
-
-func newMergeStatus(status string, id string, bug *Bug) MergeResult {
-	return MergeResult{
-		Id:     id,
-		Status: status,
-
-		// Bug is not set for an invalid merge result
-		Bug: bug,
-	}
-}
-
 // MergeAll will merge all the available remote bug
 func MergeAll(repo repository.Repo, remote string) <-chan MergeResult {
 	out := make(chan MergeResult)
@@ -98,7 +67,7 @@ func MergeAll(repo repository.Repo, remote string) <-chan MergeResult {
 
 			// Check for error in remote data
 			if !remoteBug.IsValid() {
-				out <- newMergeStatus(MsgMergeInvalid, id, nil)
+				out <- newMergeStatus(MergeStatusInvalid, id, nil)
 				continue
 			}
 
@@ -119,7 +88,7 @@ func MergeAll(repo repository.Repo, remote string) <-chan MergeResult {
 					return
 				}
 
-				out <- newMergeStatus(MsgMergeNew, id, remoteBug)
+				out <- newMergeStatus(MergeStatusNew, id, remoteBug)
 				continue
 			}
 
@@ -138,12 +107,64 @@ func MergeAll(repo repository.Repo, remote string) <-chan MergeResult {
 			}
 
 			if updated {
-				out <- newMergeStatus(MsgMergeUpdated, id, localBug)
+				out <- newMergeStatus(MergeStatusUpdated, id, localBug)
 			} else {
-				out <- newMergeStatus(MsgMergeNothing, id, localBug)
+				out <- newMergeStatus(MergeStatusNothing, id, localBug)
 			}
 		}
 	}()
 
 	return out
 }
+
+// MergeStatus represent the result of a merge operation of a bug
+type MergeStatus int
+
+const (
+	_ MergeStatus = iota
+	MergeStatusNew
+	MergeStatusInvalid
+	MergeStatusUpdated
+	MergeStatusNothing
+)
+
+func (ms MergeStatus) String() string {
+	switch ms {
+	case MergeStatusNew:
+		return "new"
+	case MergeStatusInvalid:
+		return "invalid data"
+	case MergeStatusUpdated:
+		return "updated"
+	case MergeStatusNothing:
+		return "nothing to do"
+	default:
+		panic("unknown merge status")
+	}
+}
+
+type MergeResult struct {
+	// Err is set when a terminal error occur in the process
+	Err error
+
+	Id     string
+	Status MergeStatus
+	Bug    *Bug
+}
+
+func newMergeError(err error, id string) MergeResult {
+	return MergeResult{
+		Err: err,
+		Id:  id,
+	}
+}
+
+func newMergeStatus(status MergeStatus, id string, bug *Bug) MergeResult {
+	return MergeResult{
+		Id:     id,
+		Status: status,
+
+		// Bug is not set for an invalid merge result
+		Bug: bug,
+	}
+}

bug/operation.go 🔗

@@ -5,7 +5,7 @@ import (
 	"time"
 )
 
-// OperationType is an identifier
+// OperationType is an operation type identifier
 type OperationType int
 
 const (

cache/repo_cache.go 🔗

@@ -313,7 +313,7 @@ func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult {
 			id := result.Id
 
 			switch result.Status {
-			case bug.MsgMergeNew, bug.MsgMergeUpdated:
+			case bug.MergeStatusNew, bug.MergeStatusUpdated:
 				b := result.Bug
 				snap := b.Compile()
 				c.excerpts[id] = NewBugExcerpt(b, &snap)

commands/pull.go 🔗

@@ -41,7 +41,7 @@ func runPull(cmd *cobra.Command, args []string) error {
 			return merge.Err
 		}
 
-		if merge.Status != bug.MsgMergeNothing {
+		if merge.Status != bug.MergeStatusNothing {
 			fmt.Printf("%s: %s\n", merge.Bug.HumanId(), merge.Status)
 		}
 	}

termui/bug_table.go 🔗

@@ -423,7 +423,7 @@ func (bt *bugTable) pull(g *gocui.Gui, v *gocui.View) error {
 		beginLine := ""
 
 		for merge := range bt.repo.MergeAll(defaultRemote) {
-			if merge.Status == bug.MsgMergeNothing {
+			if merge.Status == bug.MergeStatusNothing {
 				continue
 			}