1package identity
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/pkg/errors"
8
9 "github.com/MichaelMure/git-bug/entity"
10 "github.com/MichaelMure/git-bug/repository"
11)
12
13// Fetch retrieve updates from a remote
14// This does not change the local identities state
15func Fetch(repo repository.Repo, remote string) (string, error) {
16 return repo.FetchRefs(remote, "identities")
17}
18
19// Push update a remote with the local changes
20func Push(repo repository.Repo, remote string) (string, error) {
21 return repo.PushRefs(remote, "identities")
22}
23
24// Pull will do a Fetch + MergeAll
25// This function will return an error if a merge fail
26func Pull(repo repository.ClockedRepo, remote string) error {
27 _, err := Fetch(repo, remote)
28 if err != nil {
29 return err
30 }
31
32 for merge := range MergeAll(repo, remote) {
33 if merge.Err != nil {
34 return merge.Err
35 }
36 if merge.Status == entity.MergeStatusInvalid {
37 return errors.Errorf("merge failure: %s", merge.Reason)
38 }
39 }
40
41 return nil
42}
43
44// MergeAll will merge all the available remote identity
45func MergeAll(repo repository.ClockedRepo, remote string) <-chan entity.MergeResult {
46 out := make(chan entity.MergeResult)
47
48 go func() {
49 defer close(out)
50
51 remoteRefSpec := fmt.Sprintf(identityRemoteRefPattern, remote)
52 remoteRefs, err := repo.ListRefs(remoteRefSpec)
53
54 if err != nil {
55 out <- entity.MergeResult{Err: err}
56 return
57 }
58
59 for _, remoteRef := range remoteRefs {
60 refSplit := strings.Split(remoteRef, "/")
61 id := entity.Id(refSplit[len(refSplit)-1])
62
63 if err := id.Validate(); err != nil {
64 out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "invalid ref").Error())
65 continue
66 }
67
68 remoteIdentity, err := read(repo, remoteRef)
69
70 if err != nil {
71 out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "remote identity is not readable").Error())
72 continue
73 }
74
75 // Check for error in remote data
76 if err := remoteIdentity.Validate(); err != nil {
77 out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "remote identity is invalid").Error())
78 continue
79 }
80
81 localRef := identityRefPattern + remoteIdentity.Id().String()
82 localExist, err := repo.RefExist(localRef)
83
84 if err != nil {
85 out <- entity.NewMergeError(err, id)
86 continue
87 }
88
89 // the identity is not local yet, simply create the reference
90 if !localExist {
91 err := repo.CopyRef(remoteRef, localRef)
92
93 if err != nil {
94 out <- entity.NewMergeError(err, id)
95 return
96 }
97
98 out <- entity.NewMergeNewStatus(id, remoteIdentity)
99 continue
100 }
101
102 localIdentity, err := read(repo, localRef)
103
104 if err != nil {
105 out <- entity.NewMergeError(errors.Wrap(err, "local identity is not readable"), id)
106 return
107 }
108
109 updated, err := localIdentity.Merge(repo, remoteIdentity)
110
111 if err != nil {
112 out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "merge failed").Error())
113 return
114 }
115
116 if updated {
117 out <- entity.NewMergeUpdatedStatus(id, localIdentity)
118 } else {
119 out <- entity.NewMergeNothingStatus(id)
120 }
121 }
122 }()
123
124 return out
125}