-
Notifications
You must be signed in to change notification settings - Fork 351
/
diff.go
321 lines (296 loc) · 8.35 KB
/
diff.go
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package local
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/go-openapi/swag"
"github.com/treeverse/lakefs/pkg/api/apigen"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/block/local"
"github.com/treeverse/lakefs/pkg/block/params"
"github.com/treeverse/lakefs/pkg/uri"
)
type ChangeSource int
const (
ChangeSourceRemote ChangeSource = iota
ChangeSourceLocal
)
type ChangeType int
const (
ChangeTypeAdded ChangeType = iota
ChangeTypeModified
ChangeTypeRemoved
ChangeTypeConflict
)
type Change struct {
Source ChangeSource
Path string
Type ChangeType
}
func (c *Change) String() string {
return fmt.Sprintf("%s\t%s\t%s", ChangeSourceString(c.Source), ChangeTypeString(c.Type), c.Path)
}
func ChangeTypeFromString(changeType string) ChangeType {
switch changeType {
case "added":
return ChangeTypeAdded
case "removed":
return ChangeTypeRemoved
case "modified", "changed":
return ChangeTypeModified
case "conflict":
return ChangeTypeConflict
default:
panic("invalid change type")
}
}
func ChangeTypeString(changeType ChangeType) string {
switch changeType {
case ChangeTypeAdded:
return "added"
case ChangeTypeRemoved:
return "removed"
case ChangeTypeModified:
return "modified"
case ChangeTypeConflict:
return "conflict"
default:
panic("invalid change type")
}
}
func ChangeSourceString(changeSource ChangeSource) string {
switch changeSource {
case ChangeSourceLocal:
return "local"
case ChangeSourceRemote:
return "remote"
default:
panic("invalid change source")
}
}
type Changes []*Change
func (c Changes) String() string {
strs := make([]string, len(c))
for i, cc := range c {
strs[i] = cc.String()
}
return strings.Join(strs, "\n")
}
type MergeStrategy int
const (
MergeStrategyNone MergeStrategy = iota
MergeStrategyThis
MergeStrategyOther
)
// MergeWith combines changes from two diffs, sorting by lexicographic order.
// If the same path appears in both diffs, it's marked as a conflict.
func (c Changes) MergeWith(other Changes, strategy MergeStrategy) Changes {
cIdx := 0
oIdx := 0
result := make(Changes, 0)
for cIdx < len(c) && oIdx < len(other) {
switch {
case c[cIdx].Path > other[oIdx].Path:
// other is first
result = append(result, other[oIdx])
oIdx++
case c[cIdx].Path < other[oIdx].Path:
result = append(result, c[cIdx])
cIdx++
default: // both modified the same path!!
switch strategy {
case MergeStrategyNone:
result = append(result, &Change{
Source: c[cIdx].Source,
Path: c[cIdx].Path,
Type: ChangeTypeConflict,
})
case MergeStrategyOther:
result = append(result, other[oIdx])
case MergeStrategyThis:
result = append(result, c[cIdx])
default:
panic("invalid merge strategy")
}
cIdx++
oIdx++
}
}
if cIdx < len(c) {
result = append(result, c[cIdx:]...)
}
if oIdx < len(other) {
result = append(result, other[oIdx:]...)
}
return result
}
func switchSource(source ChangeSource) ChangeSource {
switch source {
case ChangeSourceRemote:
return ChangeSourceLocal
case ChangeSourceLocal:
return ChangeSourceRemote
default:
panic("invalid change source")
}
}
// Undo Creates a new list of changes that reverses the given changes list.
func Undo(c Changes) Changes {
reversed := make(Changes, len(c))
for i, op := range c {
switch op.Type {
case ChangeTypeAdded:
reversed[i] = &Change{
Source: switchSource(op.Source),
Path: op.Path,
Type: ChangeTypeRemoved,
}
case ChangeTypeModified:
reversed[i] = &Change{
Source: switchSource(op.Source),
Path: op.Path,
Type: ChangeTypeModified,
}
case ChangeTypeRemoved:
reversed[i] = &Change{
Source: switchSource(op.Source),
Path: op.Path,
Type: ChangeTypeModified, // mark as modified so it will trigger download
}
case ChangeTypeConflict:
default:
// Should never reach
panic(fmt.Sprintf("got unsupported change type %d in undo", op.Type))
}
}
return reversed
}
// DiffLocalWithHead Checks changes between a local directory and the head it is pointing to. The diff check assumes the remote
// is an immutable set so any changes found resulted from changes in the local directory
// left is an object channel which contains results from a remote source. rightPath is the local directory to diff with
func DiffLocalWithHead(left <-chan apigen.ObjectStats, rightPath string) (Changes, error) {
// left should be the base commit
changes := make([]*Change, 0)
var (
currentRemoteFile apigen.ObjectStats
hasMore bool
)
absPath, err := filepath.Abs(rightPath)
if err != nil {
return nil, err
}
uri := url.URL{Scheme: "local", Path: absPath}
reader := local.NewLocalWalker(params.Local{
ImportEnabled: false,
ImportHidden: true,
AllowedExternalPrefixes: []string{absPath},
})
err = reader.Walk(context.Background(), &uri, block.WalkOptions{}, func(e block.ObjectStoreEntry) error {
info, err := os.Stat(e.FullKey)
if err != nil {
return err
}
if info.IsDir() || diffShouldIgnore(info.Name()) {
return nil
}
localPath := e.RelativeKey
localPath = strings.TrimPrefix(localPath, string(filepath.Separator))
localPath = filepath.ToSlash(localPath) // normalize to use "/" always
localBytes := info.Size()
localMtime := info.ModTime().Unix()
for {
if currentRemoteFile.Path == "" {
if currentRemoteFile, hasMore = <-left; !hasMore {
// nothing left on the left side, we definitely added stuff!
changes = append(changes, &Change{ChangeSourceLocal, localPath, ChangeTypeAdded})
break
}
}
switch {
case currentRemoteFile.Path < localPath: // We removed a file locally
changes = append(changes, &Change{ChangeSourceLocal, currentRemoteFile.Path, ChangeTypeRemoved})
currentRemoteFile.Path = ""
case currentRemoteFile.Path == localPath:
remoteMtime, err := getMtimeFromStats(currentRemoteFile)
if err != nil {
return err
}
if localBytes != swag.Int64Value(currentRemoteFile.SizeBytes) || localMtime != remoteMtime {
// we made a change!
changes = append(changes, &Change{ChangeSourceLocal, localPath, ChangeTypeModified})
}
currentRemoteFile.Path = ""
return nil
default: // currentRemoteFile.Path > localPath - we added a new file locally
changes = append(changes, &Change{ChangeSourceLocal, localPath, ChangeTypeAdded})
return nil
}
}
return nil
})
if err != nil {
return nil, err
}
// remaining remote files
if currentRemoteFile.Path != "" {
changes = append(changes, &Change{ChangeSourceLocal, currentRemoteFile.Path, ChangeTypeRemoved})
}
for currentRemoteFile = range left {
changes = append(changes, &Change{ChangeSourceLocal, currentRemoteFile.Path, ChangeTypeRemoved})
}
return changes, nil
}
// ListRemote - Lists objects from a remote uri and inserts them into the objects channel
func ListRemote(ctx context.Context, client apigen.ClientWithResponsesInterface, loc *uri.URI, objects chan<- apigen.ObjectStats) error {
hasMore := true
var after string
defer func() {
close(objects)
}()
for hasMore {
listResp, err := client.ListObjectsWithResponse(ctx, loc.Repository, loc.Ref, &apigen.ListObjectsParams{
After: (*apigen.PaginationAfter)(swag.String(after)),
Prefix: (*apigen.PaginationPrefix)(loc.Path),
UserMetadata: swag.Bool(true),
})
if err != nil {
return err
}
if listResp.HTTPResponse.StatusCode != http.StatusOK {
return fmt.Errorf("list remote failed. HTTP %d: %w", listResp.StatusCode(), ErrRemoteFailure)
}
for _, o := range listResp.JSON200.Results {
path := strings.TrimPrefix(o.Path, loc.GetPath())
// skip directory markers
if path == "" || (strings.HasSuffix(path, uri.PathSeparator) && swag.Int64Value(o.SizeBytes) == 0) {
continue
}
path = strings.TrimPrefix(path, uri.PathSeparator)
objects <- apigen.ObjectStats{
Checksum: o.Checksum,
ContentType: o.ContentType,
Metadata: o.Metadata,
Mtime: o.Mtime,
Path: path,
PathType: o.PathType,
PhysicalAddress: o.PhysicalAddress,
SizeBytes: o.SizeBytes,
}
}
hasMore = listResp.JSON200.Pagination.HasMore
after = listResp.JSON200.Pagination.NextOffset
}
return nil
}
func diffShouldIgnore(name string) bool {
switch name {
case IndexFileName, ".DS_Store":
return true
default:
return false
}
}