1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
package dag
import (
"fmt"
"github.com/pkg/errors"
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/repository"
)
// ListLocalIds list all the available local Entity's Id
func ListLocalIds(typename string, repo repository.RepoData) ([]entity.Id, error) {
refs, err := repo.ListRefs(fmt.Sprintf("refs/%s/", typename))
if err != nil {
return nil, err
}
return entity.RefsToIds(refs), nil
}
// Fetch retrieve updates from a remote
// This does not change the local entity state
func Fetch(def Definition, repo repository.Repo, remote string) (string, error) {
// "refs/<entity>/*:refs/remotes/<remote>/<entity>/*"
fetchRefSpec := fmt.Sprintf("refs/%s/*:refs/remotes/%s/%s/*",
def.namespace, remote, def.namespace)
return repo.FetchRefs(remote, fetchRefSpec)
}
// Push update a remote with the local changes
func Push(def Definition, repo repository.Repo, remote string) (string, error) {
// "refs/<entity>/*:refs/<entity>/*"
refspec := fmt.Sprintf("refs/%s/*:refs/%s/*",
def.namespace, def.namespace)
return repo.PushRefs(remote, refspec)
}
// Pull will do a Fetch + MergeAll
// Contrary to MergeAll, this function will return an error if a merge fail.
func Pull(def Definition, repo repository.ClockedRepo, remote string) error {
_, err := Fetch(def, repo, remote)
if err != nil {
return err
}
for merge := range MergeAll(def, repo, remote) {
if merge.Err != nil {
return merge.Err
}
if merge.Status == entity.MergeStatusInvalid {
return errors.Errorf("merge failure: %s", merge.Reason)
}
}
return nil
}
// MergeAll will merge all the available remote Entity:
//
// Multiple scenario exist:
// 1. if the remote Entity doesn't exist locally, it's created
// --> emit entity.MergeStatusNew
// 2. if the remote and local Entity have the same state, nothing is changed
// --> emit entity.MergeStatusNothing
// 3. if the local Entity has new commits but the remote don't, nothing is changed
// --> emit entity.MergeStatusNothing
// 4. if the remote has new commit, the local bug is updated to match the same history
// (fast-forward update)
// --> emit entity.MergeStatusUpdated
// 5. if both local and remote Entity have new commits (that is, we have a concurrent edition),
// a merge commit with an empty operationPack is created to join both branch and form a DAG.
// --> emit entity.MergeStatusUpdated
func MergeAll(def Definition, repo repository.ClockedRepo, remote string) <-chan entity.MergeResult {
out := make(chan entity.MergeResult)
// no caching for the merge, we load everything from git even if that means multiple
// copy of the same entity in memory. The cache layer will intercept the results to
// invalidate entities if necessary.
go func() {
defer close(out)
remoteRefSpec := fmt.Sprintf("refs/remotes/%s/%s/", remote, def.namespace)
remoteRefs, err := repo.ListRefs(remoteRefSpec)
if err != nil {
out <- entity.MergeResult{Err: err}
return
}
for _, remoteRef := range remoteRefs {
out <- merge(def, repo, remoteRef)
}
}()
return out
}
// merge perform a merge to make sure a local Entity is up to date.
// See MergeAll for more details.
func merge(def Definition, repo repository.ClockedRepo, remoteRef string) entity.MergeResult {
id := entity.RefToId(remoteRef)
if err := id.Validate(); err != nil {
return entity.NewMergeInvalidStatus(id, errors.Wrap(err, "invalid ref").Error())
}
remoteEntity, err := read(def, repo, remoteRef)
if err != nil {
return entity.NewMergeInvalidStatus(id,
errors.Wrapf(err, "remote %s is not readable", def.typename).Error())
}
// Check for error in remote data
if err := remoteEntity.Validate(); err != nil {
return entity.NewMergeInvalidStatus(id,
errors.Wrapf(err, "remote %s data is invalid", def.typename).Error())
}
localRef := fmt.Sprintf("refs/%s/%s", def.namespace, id.String())
// SCENARIO 1
// if the remote Entity doesn't exist locally, it's created
localExist, err := repo.RefExist(localRef)
if err != nil {
return entity.NewMergeError(err, id)
}
if !localExist {
// the bug is not local yet, simply create the reference
err := repo.CopyRef(remoteRef, localRef)
if err != nil {
return entity.NewMergeError(err, id)
}
return entity.NewMergeNewStatus(id, remoteEntity)
}
localCommit, err := repo.ResolveRef(localRef)
if err != nil {
return entity.NewMergeError(err, id)
}
remoteCommit, err := repo.ResolveRef(remoteRef)
if err != nil {
return entity.NewMergeError(err, id)
}
// SCENARIO 2
// if the remote and local Entity have the same state, nothing is changed
if localCommit == remoteCommit {
// nothing to merge
return entity.NewMergeNothingStatus(id)
}
// SCENARIO 3
// if the local Entity has new commits but the remote don't, nothing is changed
localCommits, err := repo.ListCommits(localRef)
if err != nil {
return entity.NewMergeError(err, id)
}
for _, hash := range localCommits {
if hash == localCommit {
return entity.NewMergeNothingStatus(id)
}
}
// SCENARIO 4
// if the remote has new commit, the local bug is updated to match the same history
// (fast-forward update)
remoteCommits, err := repo.ListCommits(remoteRef)
if err != nil {
return entity.NewMergeError(err, id)
}
// fast-forward is possible if otherRef include ref
fastForwardPossible := false
for _, hash := range remoteCommits {
if hash == localCommit {
fastForwardPossible = true
break
}
}
if fastForwardPossible {
err = repo.UpdateRef(localRef, remoteCommit)
if err != nil {
return entity.NewMergeError(err, id)
}
return entity.NewMergeUpdatedStatus(id, remoteEntity)
}
// SCENARIO 5
// if both local and remote Entity have new commits (that is, we have a concurrent edition),
// a merge commit with an empty operationPack is created to join both branch and form a DAG.
// fast-forward is not possible, we need to create a merge commit
// For simplicity when reading and to have clocks that record this change, we store
// an empty operationPack.
// First step is to collect those clocks.
localEntity, err := read(def, repo, localRef)
if err != nil {
return entity.NewMergeError(err, id)
}
// TODO: pack clock
// err = localEntity.packClock.Witness(remoteEntity.packClock.Time())
// if err != nil {
// return entity.NewMergeError(err, id)
// }
//
// packTime, err := localEntity.packClock.Increment()
// if err != nil {
// return entity.NewMergeError(err, id)
// }
editTime, err := repo.Increment(fmt.Sprintf(editClockPattern, def.namespace))
if err != nil {
return entity.NewMergeError(err, id)
}
opp := &operationPack{
Operations: nil,
CreateTime: 0,
EditTime: editTime,
// TODO: pack clock
// PackTime: packTime,
}
commitHash, err := opp.Write(def, repo, localCommit, remoteCommit)
if err != nil {
return entity.NewMergeError(err, id)
}
// finally update the ref
err = repo.UpdateRef(localRef, commitHash)
if err != nil {
return entity.NewMergeError(err, id)
}
// Note: we don't need to update localEntity state (lastCommit, operations...) as we
// discard it entirely anyway.
return entity.NewMergeUpdatedStatus(id, localEntity)
}
func Remove() error {
panic("")
}
|