aboutsummaryrefslogtreecommitdiffstats
path: root/commands/pull.go
diff options
context:
space:
mode:
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
}