1package identity
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/pkg/errors"
8
9 bootstrap "github.com/MichaelMure/git-bug/entity/boostrap"
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, Namespace)
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, Namespace)
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 == bootstrap.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 bootstrap.MergeResult {
46 out := make(chan bootstrap.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 <- bootstrap.MergeResult{Err: err}
56 return
57 }
58
59 for _, remoteRef := range remoteRefs {
60 refSplit := strings.Split(remoteRef, "/")
61 id := bootstrap.Id(refSplit[len(refSplit)-1])
62
63 if err := id.Validate(); err != nil {
64 out <- bootstrap.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 <- bootstrap.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 <- bootstrap.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 <- bootstrap.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 <- bootstrap.NewMergeError(err, id)
95 return
96 }
97
98 out <- bootstrap.NewMergeNewStatus(id, remoteIdentity)
99 continue
100 }
101
102 localIdentity, err := read(repo, localRef)
103
104 if err != nil {
105 out <- bootstrap.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 <- bootstrap.NewMergeInvalidStatus(id, errors.Wrap(err, "merge failed").Error())
113 return
114 }
115
116 if updated {
117 out <- bootstrap.NewMergeUpdatedStatus(id, localIdentity)
118 } else {
119 out <- bootstrap.NewMergeNothingStatus(id)
120 }
121 }
122 }()
123
124 return out
125}
126
127// Remove will remove a local identity from its entity.Id.
128// It is left as a responsibility to the caller to make sure that this identities is not
129// linked from another entity, otherwise it would break it.
130// Remove is idempotent.
131func Remove(repo repository.ClockedRepo, id bootstrap.Id) error {
132 var fullMatches []string
133
134 refs, err := repo.ListRefs(identityRefPattern + id.String())
135 if err != nil {
136 return err
137 }
138 if len(refs) > 1 {
139 return bootstrap.NewErrMultipleMatch(Typename, bootstrap.RefsToIds(refs))
140 }
141 if len(refs) == 1 {
142 // we have the identity locally
143 fullMatches = append(fullMatches, refs[0])
144 }
145
146 remotes, err := repo.GetRemotes()
147 if err != nil {
148 return err
149 }
150
151 for remote := range remotes {
152 remotePrefix := fmt.Sprintf(identityRemoteRefPattern+id.String(), remote)
153 remoteRefs, err := repo.ListRefs(remotePrefix)
154 if err != nil {
155 return err
156 }
157 if len(remoteRefs) > 1 {
158 return bootstrap.NewErrMultipleMatch(Typename, bootstrap.RefsToIds(refs))
159 }
160 if len(remoteRefs) == 1 {
161 // found the identity in a remote
162 fullMatches = append(fullMatches, remoteRefs[0])
163 }
164 }
165
166 if len(fullMatches) == 0 {
167 return bootstrap.NewErrNotFound(Typename)
168 }
169
170 for _, ref := range fullMatches {
171 err = repo.RemoveRef(ref)
172 if err != nil {
173 return err
174 }
175 }
176
177 return nil
178}
179
180// RemoveAll will remove all local identities.
181// It is left as a responsibility to the caller to make sure that those identities are not
182// linked from another entity, otherwise it would break them.
183// RemoveAll is idempotent.
184func RemoveAll(repo repository.ClockedRepo) error {
185 localIds, err := ListLocalIds(repo)
186 if err != nil {
187 return err
188 }
189 for _, id := range localIds {
190 err = Remove(repo, id)
191 if err != nil {
192 return err
193 }
194 }
195 return nil
196}