aboutsummaryrefslogtreecommitdiffstats
path: root/commands/pull.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-17 01:52:56 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-17 01:52:56 +0200
commit0180b68cb0bb3aecf9b4a6186094a084762b4a25 (patch)
tree95597554cfdda76b9f6cc5aab7c3fd5b24a3db75 /commands/pull.go
parent1d678dfdfa026968dbb19795c9bed16385603b21 (diff)
downloadgit-bug-0180b68cb0bb3aecf9b4a6186094a084762b4a25.tar.gz
implement pull/merge
Diffstat (limited to 'commands/pull.go')
-rw-r--r--commands/pull.go61
1 files changed, 60 insertions, 1 deletions
diff --git a/commands/pull.go b/commands/pull.go
index eefcc641..15d832fe 100644
--- a/commands/pull.go
+++ b/commands/pull.go
@@ -2,6 +2,7 @@ package commands
import (
"errors"
+ "fmt"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/repository"
)
@@ -16,9 +17,67 @@ func runPull(repo repository.Repo, args []string) error {
remote = args[0]
}
- if err := repo.PullRefs(remote, bug.BugsRefPattern+"*", bug.BugsRemoteRefPattern+"*"); err != nil {
+ fmt.Printf("Fetching remote ...\n\n")
+
+ if err := repo.FetchRefs(remote, bug.BugsRefPattern+"*", bug.BugsRemoteRefPattern+"*"); err != nil {
+ return err
+ }
+
+ fmt.Printf("\nMerging data ...\n\n")
+
+ remoteRefSpec := fmt.Sprintf(bug.BugsRemoteRefPattern, remote)
+ remoteRefs, err := repo.ListRefs(remoteRefSpec)
+
+ if err != nil {
return err
}
+
+ for _, ref := range remoteRefs {
+ remoteRef := fmt.Sprintf(bug.BugsRemoteRefPattern, remote) + ref
+ remoteBug, err := bug.ReadBug(repo, remoteRef)
+
+ if err != nil {
+ return err
+ }
+
+ // Check for error in remote data
+ if !remoteBug.IsValid() {
+ fmt.Printf("%s: %s\n", remoteBug.HumanId(), "invalid remote data")
+ continue
+ }
+
+ localRef := bug.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 {
+ return err
+ }
+
+ fmt.Printf("%s: %s\n", remoteBug.HumanId(), "new")
+ continue
+ }
+
+ localBug, err := bug.ReadBug(repo, localRef)
+
+ if err != nil {
+ return err
+ }
+
+ updated, err := localBug.Merge(repo, remoteBug)
+
+ if err != nil {
+ return err
+ }
+
+ if updated {
+ fmt.Printf("%s: %s\n", remoteBug.HumanId(), "updated")
+ }
+ }
+
return nil
}