-
Notifications
You must be signed in to change notification settings - Fork 351
/
resolve_ref.go
161 lines (146 loc) · 5.13 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
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,
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,
StagingToken: rr.StagingToken,
CommitID: rr.CommitID,
}, 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,
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,
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,
BranchID: branchID,
StagingToken: branch.StagingToken,
CommitID: branch.CommitID,
}, 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,
CommitID: *commitID,
}, nil
}