forked from treeverse/lakeFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcataloger_diff_uncommitted.go
62 lines (54 loc) · 1.68 KB
/
cataloger_diff_uncommitted.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
package catalog
import (
"context"
"fmt"
sq "github.com/Masterminds/squirrel"
"github.com/treeverse/lakefs/db"
)
func (c *cataloger) DiffUncommitted(ctx context.Context, repository, branch string, limit int, after string) (Differences, bool, error) {
if err := Validate(ValidateFields{
{Name: "repository", IsValid: ValidateRepositoryName(repository)},
{Name: "branch", IsValid: ValidateBranchName(branch)},
}); err != nil {
return nil, false, err
}
if limit < 0 || limit > DiffMaxLimit {
limit = DiffMaxLimit
}
res, err := c.db.Transact(func(tx db.Tx) (interface{}, error) {
branchID, err := c.getBranchIDCache(tx, repository, branch)
if err != nil {
return nil, err
}
lineage, err := getLineage(tx, branchID, CommittedID)
if err != nil {
return nil, fmt.Errorf("get lineage: %w", err)
}
q := psql.Select("CASE WHEN e.max_commit=0 THEN 1 WHEN v.path IS NOT NULL THEN 2 ELSE 0 END AS diff_type", "e.path").
FromSelect(sqEntriesV(UncommittedID), "e").
JoinClause(
sqEntriesLineageV(branchID, CommittedID, lineage).
Prefix("LEFT JOIN (").Suffix(") AS v ON v.path=e.path")).
Where(sq.And{
sq.Eq{"e.branch_id": branchID, "e.is_committed": false},
sq.Gt{"e.path": after},
}).
Limit(uint64(limit + 1)).
OrderBy("path")
sql, args, err := q.ToSql()
if err != nil {
return nil, fmt.Errorf("build sql: %w", err)
}
var result Differences
if err := tx.Select(&result, sql, args...); err != nil {
return nil, err
}
return result, nil
}, c.txOpts(ctx, db.ReadOnly())...)
if err != nil {
return nil, false, err
}
differences := res.(Differences)
hasMore := paginateSlice(&differences, limit)
return differences, hasMore, nil
}