-
Notifications
You must be signed in to change notification settings - Fork 342
/
resolve_ref.go
183 lines (168 loc) · 5.67 KB
/
resolve_ref.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
package ref
import (
"context"
"errors"
"regexp"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/ident"
)
type Store interface {
GetBranch(ctx context.Context, repositoryID graveler.RepositoryID, branchID graveler.BranchID) (*graveler.Branch, error)
GetTag(ctx context.Context, repositoryID graveler.RepositoryID, tagID graveler.TagID) (*graveler.CommitID, error)
GetCommitByPrefix(ctx context.Context, repositoryID graveler.RepositoryID, prefix graveler.CommitID) (*graveler.Commit, error)
GetCommit(ctx context.Context, repositoryID graveler.RepositoryID, prefix graveler.CommitID) (*graveler.Commit, error)
}
type revResolverFunc func(context.Context, Store, ident.AddressProvider, graveler.RepositoryID, string) (*graveler.ResolvedRef, error)
var hashRegexp = regexp.MustCompile("^[a-fA-F0-9]{1,64}$")
func isAHash(part string) bool {
return hashRegexp.MatchString(part)
}
// revResolve return the first resolve of 'rev' - by hash, branch or tag
func revResolve(ctx context.Context, store Store, addressProvider ident.AddressProvider, repositoryID graveler.RepositoryID, rev string) (*graveler.ResolvedRef, error) {
resolvers := []revResolverFunc{revResolveAHash, revResolveBranch, revResolveTag}
for _, resolveHelper := range resolvers {
r, err := resolveHelper(ctx, store, addressProvider, repositoryID, rev)
if err != nil {
return nil, err
}
if r != nil {
return r, nil
}
}
return nil, graveler.ErrNotFound
}
func ResolveRawRef(ctx context.Context, store Store, addressProvider ident.AddressProvider, repositoryID graveler.RepositoryID, rawRef graveler.RawRef) (*graveler.ResolvedRef, error) {
rr, err := revResolve(ctx, store, addressProvider, repositoryID, rawRef.BaseRef)
if err != nil {
return nil, err
}
// return the matched reference, when no modifiers on ref or use the commit id as base
if len(rawRef.Modifiers) == 0 {
return rr, nil
}
baseCommit := rr.CommitID
for _, mod := range rawRef.Modifiers {
// lastly, apply modifier
switch mod.Type {
case graveler.RefModTypeAt:
if rr.Type != graveler.ReferenceTypeBranch || len(rawRef.Modifiers) != 1 {
return nil, graveler.ErrInvalidRef
}
return &graveler.ResolvedRef{
Type: graveler.ReferenceTypeBranch,
ResolvedBranchModifier: graveler.ResolvedBranchModifierCommitted,
BranchRecord: graveler.BranchRecord{
Branch: &graveler.Branch{
CommitID: rr.CommitID,
}},
}, nil
case graveler.RefModTypeDollar:
if rr.Type != graveler.ReferenceTypeBranch || len(rawRef.Modifiers) != 1 {
return nil, graveler.ErrInvalidRef
}
return &graveler.ResolvedRef{
Type: graveler.ReferenceTypeBranch,
ResolvedBranchModifier: graveler.ResolvedBranchModifierStaging,
BranchRecord: graveler.BranchRecord{
BranchID: rr.BranchID,
Branch: &graveler.Branch{
CommitID: rr.CommitID,
StagingToken: rr.StagingToken,
SealedTokens: rr.SealedTokens,
}},
}, nil
case graveler.RefModTypeTilde:
// skip mod.ValueNumeric iterations
for i := 0; i < mod.Value; i++ {
commit, err := store.GetCommit(ctx, repositoryID, baseCommit)
if err != nil {
return nil, err
}
if len(commit.Parents) == 0 {
return nil, graveler.ErrNotFound
}
baseCommit = commit.Parents[0]
}
case graveler.RefModTypeCaret:
if mod.Value == 0 {
// ^0 = the commit itself
continue
}
// get the commit and extract parents
c, err := store.GetCommitByPrefix(ctx, repositoryID, baseCommit)
if err != nil {
return nil, err
}
if mod.Value > len(c.Parents) {
return nil, graveler.ErrInvalidRef
}
baseCommit = c.Parents[mod.Value-1]
default:
return nil, graveler.ErrInvalidRef
}
}
return &graveler.ResolvedRef{
Type: graveler.ReferenceTypeCommit,
BranchRecord: graveler.BranchRecord{
BranchID: rr.BranchID,
Branch: &graveler.Branch{
CommitID: baseCommit,
}},
}, nil
}
func revResolveAHash(ctx context.Context, store Store, addressProvider ident.AddressProvider, repositoryID graveler.RepositoryID, rev string) (*graveler.ResolvedRef, error) {
if !isAHash(rev) {
return nil, nil
}
commit, err := store.GetCommitByPrefix(ctx, repositoryID, graveler.CommitID(rev))
if errors.Is(err, graveler.ErrNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
commitID := graveler.CommitID(addressProvider.ContentAddress(commit))
return &graveler.ResolvedRef{
Type: graveler.ReferenceTypeCommit,
BranchRecord: graveler.BranchRecord{
Branch: &graveler.Branch{
CommitID: commitID,
}},
}, nil
}
func revResolveBranch(ctx context.Context, store Store, _ ident.AddressProvider, repositoryID graveler.RepositoryID, rev string) (*graveler.ResolvedRef, error) {
branchID := graveler.BranchID(rev)
branch, err := store.GetBranch(ctx, repositoryID, branchID)
if errors.Is(err, graveler.ErrNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &graveler.ResolvedRef{
Type: graveler.ReferenceTypeBranch,
BranchRecord: graveler.BranchRecord{
BranchID: branchID,
Branch: &graveler.Branch{
CommitID: branch.CommitID,
StagingToken: branch.StagingToken,
SealedTokens: branch.SealedTokens,
}},
}, nil
}
func revResolveTag(ctx context.Context, store Store, _ ident.AddressProvider, repositoryID graveler.RepositoryID, rev string) (*graveler.ResolvedRef, error) {
commitID, err := store.GetTag(ctx, repositoryID, graveler.TagID(rev))
if errors.Is(err, graveler.ErrNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &graveler.ResolvedRef{
Type: graveler.ReferenceTypeTag,
BranchRecord: graveler.BranchRecord{
Branch: &graveler.Branch{
CommitID: *commitID,
}},
}, nil
}