aboutsummaryrefslogtreecommitdiffstats
path: root/bug/bug_actions.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-25 18:01:32 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-25 18:01:32 +0200
commit6a12373965aff9f80147f8b5bff6a5a104927365 (patch)
treeccc1e34fba059512acab39286a86f6b73c5ad318 /bug/bug_actions.go
parent49c90eab26875cbf3094d9a546ad29b426e174a1 (diff)
downloadgit-bug-6a12373965aff9f80147f8b5bff6a5a104927365.tar.gz
more refactoring to have reusable bug action across different UI
Diffstat (limited to 'bug/bug_actions.go')
-rw-r--r--bug/bug_actions.go143
1 files changed, 143 insertions, 0 deletions
diff --git a/bug/bug_actions.go b/bug/bug_actions.go
new file mode 100644
index 00000000..85123e1c
--- /dev/null
+++ b/bug/bug_actions.go
@@ -0,0 +1,143 @@
+package bug
+
+import (
+ "fmt"
+ "github.com/MichaelMure/git-bug/repository"
+ "io"
+ "strings"
+)
+
+const MsgMergeNew = "new"
+const MsgMergeInvalid = "invalid data"
+const MsgMergeUpdated = "updated"
+const MsgMergeNothing = "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+"*")
+}
+
+func Pull(repo repository.Repo, out io.Writer, remote string) error {
+ fmt.Fprintf(out, "Fetching remote ...\n")
+
+ if err := Fetch(repo, remote); err != nil {
+ return err
+ }
+
+ fmt.Fprintf(out, "\nMerging data ...\n")
+
+ for merge := range MergeAll(repo, remote) {
+ if merge.Err != nil {
+ return merge.Err
+ }
+
+ if merge.Status != MsgMergeNothing {
+ fmt.Fprintf(out, "%s: %s\n", merge.HumanId, merge.Status)
+ }
+ }
+ return nil
+}
+
+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, MsgMergeInvalid)
+ 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, MsgMergeNew)
+ 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, MsgMergeUpdated)
+ } else {
+ out <- newMergeStatus(id, MsgMergeNothing)
+ }
+ }
+ }()
+
+ return out
+}
+
+