From 24d6714dd59a5c385c21991caa47e5727de043d0 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Wed, 27 Mar 2019 21:44:11 +0100 Subject: cache: properly push/pull identities and bugs --- bug/bug_actions.go | 92 +++++++++--------------------------------------------- 1 file changed, 14 insertions(+), 78 deletions(-) (limited to 'bug') diff --git a/bug/bug_actions.go b/bug/bug_actions.go index f214716d..b26d080f 100644 --- a/bug/bug_actions.go +++ b/bug/bug_actions.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/repository" "github.com/pkg/errors" ) @@ -34,7 +35,7 @@ func Pull(repo repository.ClockedRepo, remote string) error { if merge.Err != nil { return merge.Err } - if merge.Status == MergeStatusInvalid { + if merge.Status == entity.MergeStatusInvalid { return errors.Errorf("merge failure: %s", merge.Reason) } } @@ -49,8 +50,8 @@ func Pull(repo repository.ClockedRepo, remote string) error { // - if the local bug has new commits but the remote don't, nothing is changed // - if both local and remote bug have new commits (that is, we have a concurrent edition), // new local commits are rewritten at the head of the remote history (that is, a rebase) -func MergeAll(repo repository.ClockedRepo, remote string) <-chan MergeResult { - out := make(chan MergeResult) +func MergeAll(repo repository.ClockedRepo, remote string) <-chan entity.MergeResult { + out := make(chan entity.MergeResult) go func() { defer close(out) @@ -59,7 +60,7 @@ func MergeAll(repo repository.ClockedRepo, remote string) <-chan MergeResult { remoteRefs, err := repo.ListRefs(remoteRefSpec) if err != nil { - out <- MergeResult{Err: err} + out <- entity.MergeResult{Err: err} return } @@ -70,13 +71,13 @@ func MergeAll(repo repository.ClockedRepo, remote string) <-chan MergeResult { remoteBug, err := readBug(repo, remoteRef) if err != nil { - out <- newMergeInvalidStatus(id, errors.Wrap(err, "remote bug is not readable").Error()) + out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "remote bug is not readable").Error()) continue } // Check for error in remote data if err := remoteBug.Validate(); err != nil { - out <- newMergeInvalidStatus(id, errors.Wrap(err, "remote bug is invalid").Error()) + out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "remote bug is invalid").Error()) continue } @@ -84,7 +85,7 @@ func MergeAll(repo repository.ClockedRepo, remote string) <-chan MergeResult { localExist, err := repo.RefExist(localRef) if err != nil { - out <- newMergeError(err, id) + out <- entity.NewMergeError(err, id) continue } @@ -93,100 +94,35 @@ func MergeAll(repo repository.ClockedRepo, remote string) <-chan MergeResult { err := repo.CopyRef(remoteRef, localRef) if err != nil { - out <- newMergeError(err, id) + out <- entity.NewMergeError(err, id) return } - out <- newMergeStatus(MergeStatusNew, id, remoteBug) + out <- entity.NewMergeStatus(entity.MergeStatusNew, id, remoteBug) continue } localBug, err := readBug(repo, localRef) if err != nil { - out <- newMergeError(errors.Wrap(err, "local bug is not readable"), id) + out <- entity.NewMergeError(errors.Wrap(err, "local bug is not readable"), id) return } updated, err := localBug.Merge(repo, remoteBug) if err != nil { - out <- newMergeInvalidStatus(id, errors.Wrap(err, "merge failed").Error()) + out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "merge failed").Error()) return } if updated { - out <- newMergeStatus(MergeStatusUpdated, id, localBug) + out <- entity.NewMergeStatus(entity.MergeStatusUpdated, id, localBug) } else { - out <- newMergeStatus(MergeStatusNothing, id, localBug) + out <- entity.NewMergeStatus(entity.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 -) - -type MergeResult struct { - // Err is set when a terminal error occur in the process - Err error - - Id string - Status MergeStatus - - // Only set for invalid status - Reason string - - // Not set for invalid status - Bug *Bug -} - -func (mr MergeResult) String() string { - switch mr.Status { - case MergeStatusNew: - return "new" - case MergeStatusInvalid: - return fmt.Sprintf("invalid data: %s", mr.Reason) - case MergeStatusUpdated: - return "updated" - case MergeStatusNothing: - return "nothing to do" - default: - panic("unknown merge status") - } -} - -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, - } -} - -func newMergeInvalidStatus(id string, reason string) MergeResult { - return MergeResult{ - Id: id, - Status: MergeStatusInvalid, - Reason: reason, - } -} -- cgit