-
Notifications
You must be signed in to change notification settings - Fork 352
/
diff.go
73 lines (64 loc) · 1.79 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
package diff
import (
"context"
"fmt"
"net/http"
"github.com/go-openapi/swag"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/treeverse/lakefs/pkg/api/apigen"
"github.com/treeverse/lakefs/pkg/api/apiutil"
"github.com/treeverse/lakefs/pkg/local"
"github.com/treeverse/lakefs/pkg/uri"
)
const diffTypeTwoDot = "two_dot"
// StreamRepositoryDiffs asynchronously fetches differences between 'left' and 'right' references, assumes both are in the same repository
func StreamRepositoryDiffs(ctx context.Context, client apigen.ClientWithResponsesInterface, left, right *uri.URI, prefix string, diffs chan<- apigen.Diff, twoDot bool) error {
defer func() {
close(diffs)
}()
var diffType *string
if twoDot {
diffType = apiutil.Ptr(diffTypeTwoDot)
}
hasMore := true
var after string
for hasMore {
diffResp, err := client.DiffRefsWithResponse(ctx, left.Repository, left.Ref, right.Ref, &apigen.DiffRefsParams{
After: (*apigen.PaginationAfter)(swag.String(after)),
Prefix: (*apigen.PaginationPrefix)(&prefix),
Type: diffType,
})
if err != nil {
return err
}
if diffResp.HTTPResponse.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP %d: %w", diffResp.StatusCode(), local.ErrRemoteDiffFailed)
}
for _, d := range diffResp.JSON200.Results {
diffs <- d
}
hasMore = diffResp.JSON200.Pagination.HasMore
after = diffResp.JSON200.Pagination.NextOffset
}
return nil
}
func Fmt(change string) (string, text.Color) {
var color text.Color
var action string
switch change {
case "added":
color = text.FgGreen
action = "+ added"
case "removed":
color = text.FgRed
action = "- removed"
case "changed", "modified":
color = text.FgYellow
action = "~ modified"
case "conflict":
color = text.FgHiYellow
action = "* conflict"
default:
}
return action, color
}