diff options
author | Michael Muré <batolettre@gmail.com> | 2018-07-23 00:04:46 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-07-23 00:04:46 +0200 |
commit | 17e2ec8f5679c1ba7ae2ea45290e9303beb3c227 (patch) | |
tree | c8862ebf6e9e9314361120127bf3fc59877c3e02 /bug/remote_actions.go | |
parent | e1f597639bfc2f796f74afa87e41581087f0b26e (diff) | |
download | git-bug-17e2ec8f5679c1ba7ae2ea45290e9303beb3c227.tar.gz |
bug: refactor to limit abstraction leak and to have a more reusable code for the UIs
Diffstat (limited to 'bug/remote_actions.go')
-rw-r--r-- | bug/remote_actions.go | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/bug/remote_actions.go b/bug/remote_actions.go new file mode 100644 index 00000000..2070ba21 --- /dev/null +++ b/bug/remote_actions.go @@ -0,0 +1,119 @@ +package bug + +import ( + "fmt" + "github.com/MichaelMure/git-bug/repository" + "strings" +) + +const MsgNew = "new" +const MsgInvalid = "invalid data" +const MsgUpdated = "updated" +const MsgNothing = "nothing to do" + +func Fetch(repo repository.Repo, remote string) error { + remoteRefSpec := fmt.Sprintf(bugsRemoteRefPattern, remote) + fetchRefSpec := fmt.Sprintf("%s*:%s*", bugsRefPattern, remoteRefSpec) + + return repo.FetchRefs(remote, fetchRefSpec) +} + +func Push(repo repository.Repo, remote string) error { + return repo.PushRefs(remote, bugsRefPattern+"*") +} + +type MergeResult struct { + Err error + + Id string + HumanId string + Status string +} + +func newMergeError(id string, err error) MergeResult { + return MergeResult{ + Id: id, + HumanId: formatHumanId(id), + Status: err.Error(), + } +} + +func newMergeStatus(id string, status string) MergeResult { + return MergeResult{ + Id: id, + HumanId: formatHumanId(id), + Status: status, + } +} + +func MergeAll(repo repository.Repo, remote string) <-chan MergeResult { + out := make(chan MergeResult) + + go func() { + defer close(out) + + remoteRefSpec := fmt.Sprintf(bugsRemoteRefPattern, remote) + remoteRefs, err := repo.ListRefs(remoteRefSpec) + + if err != nil { + out <- MergeResult{Err: err} + return + } + + for _, remoteRef := range remoteRefs { + refSplitted := strings.Split(remoteRef, "/") + id := refSplitted[len(refSplitted)-1] + + remoteBug, err := readBug(repo, remoteRef) + + if err != nil { + out <- newMergeError(id, err) + continue + } + + // Check for error in remote data + if !remoteBug.IsValid() { + out <- newMergeStatus(id, MsgInvalid) + continue + } + + localRef := bugsRefPattern + remoteBug.Id() + localExist, err := repo.RefExist(localRef) + + // the bug is not local yet, simply create the reference + if !localExist { + err := repo.CopyRef(remoteRef, localRef) + + if err != nil { + out <- newMergeError(id, err) + return + } + + out <- newMergeStatus(id, MsgNew) + continue + } + + localBug, err := readBug(repo, localRef) + + if err != nil { + out <- newMergeError(id, err) + return + } + + updated, err := localBug.Merge(repo, remoteBug) + + if err != nil { + out <- newMergeError(id, err) + return + } + + if updated { + out <- newMergeStatus(id, MsgUpdated) + } else { + out <- newMergeStatus(id, MsgNothing) + } + } + }() + + return out +} |