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
|
package operations
import (
"bytes"
"context"
"errors"
"fmt"
"regexp"
"time"
"github.com/golang/protobuf/ptypes"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/catfile"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/localrepo"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *Server) UserDeleteTag(ctx context.Context, req *gitalypb.UserDeleteTagRequest) (*gitalypb.UserDeleteTagResponse, error) {
if len(req.TagName) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "empty tag name")
}
if req.User == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty user")
}
referenceName := git.ReferenceName(fmt.Sprintf("refs/tags/%s", req.TagName))
revision, err := s.localrepo(req.GetRepository()).ResolveRevision(ctx, referenceName.Revision())
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "tag not found: %s", req.TagName)
}
if err := s.updateReferenceWithHooks(ctx, req.Repository, req.User, referenceName, git.ZeroOID, revision); err != nil {
var preReceiveError preReceiveError
if errors.As(err, &preReceiveError) {
return &gitalypb.UserDeleteTagResponse{
PreReceiveError: preReceiveError.message,
}, nil
}
var updateRefError updateRefError
if errors.As(err, &updateRefError) {
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
return nil, err
}
return &gitalypb.UserDeleteTagResponse{}, nil
}
func validateUserCreateTag(req *gitalypb.UserCreateTagRequest) error {
// Emulate validations done by Ruby. A lot of these (e.g. the
// upper-case error messages) can be simplified once we're not
// doing bug-for-bug Ruby emulation anymore)
if len(req.TagName) == 0 {
return status.Errorf(codes.InvalidArgument, "empty tag name")
}
if bytes.Contains(req.TagName, []byte(" ")) {
return status.Errorf(codes.Unknown, "Gitlab::Git::CommitError: Could not update refs/tags/%s. Please refresh and try again.", req.TagName)
}
if req.User == nil {
return status.Errorf(codes.InvalidArgument, "empty user")
}
if len(req.TargetRevision) == 0 {
return status.Error(codes.InvalidArgument, "empty target revision")
}
if bytes.Contains(req.Message, []byte("\000")) {
return status.Errorf(codes.Unknown, "ArgumentError: string contains null byte")
}
// Our own Go-specific validation
if req.GetRepository() == nil {
return status.Errorf(codes.Internal, "empty repository")
}
return nil
}
func (s *Server) UserCreateTag(ctx context.Context, req *gitalypb.UserCreateTagRequest) (*gitalypb.UserCreateTagResponse, error) {
// Validate the request
if err := validateUserCreateTag(req); err != nil {
return nil, err
}
// Setup
repo := s.localrepo(req.GetRepository())
catFile, err := s.catfileCache.BatchProcess(ctx, repo)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
// We allow all ways to name a revision that cat-file
// supports, not just OID. Resolve it.
targetRevision := git.Revision(req.TargetRevision)
targetInfo, err := catFile.Info(ctx, targetRevision)
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "revspec '%s' not found", targetRevision)
}
targetObjectID, targetObjectType := targetInfo.Oid, targetInfo.Type
// Whether we have a "message" parameter tells us if we're
// making a lightweight tag or an annotated tag. Maybe we can
// use strings.TrimSpace() eventually, but as the tests show
// the Ruby idea of Unicode whitespace is different than its
// idea.
makingTag := regexp.MustCompile(`\S`).Match(req.Message)
// If we're creating a tag to another "tag" we'll need to peel
// (possibly recursively) all the way down to the
// non-tag. That'll be our returned TargetCommit if it's a
// commit (nil if tree or blob).
//
// If we're not pointing to a tag we pretend our "peeled" is
// the commit/tree/blob object itself in subsequent logic.
peeledTargetObjectID, peeledTargetObjectType := targetObjectID, targetObjectType
if targetObjectType == "tag" {
peeledTargetObjectInfo, err := catFile.Info(ctx, targetRevision+"^{}")
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
peeledTargetObjectID, peeledTargetObjectType = peeledTargetObjectInfo.Oid, peeledTargetObjectInfo.Type
// If we're not making an annotated tag ourselves and
// the user pointed to a "tag" we'll ignore what they
// asked for and point to what we found when peeling
// that tag.
if !makingTag {
targetObjectID = peeledTargetObjectID
}
}
// At this point we'll either be pointing to an object we were
// provided with, or creating a new tag object and pointing to
// that.
refObjectID := targetObjectID
var tagObject *gitalypb.Tag
if makingTag {
localRepo := s.localrepo(repo)
committerTime := time.Now()
if req.Timestamp != nil {
committerTime, err = ptypes.Timestamp(req.Timestamp)
if err != nil {
return nil, helper.ErrInvalidArgument(err)
}
}
tagObjectID, err := localRepo.WriteTag(ctx, targetObjectID, targetObjectType, req.TagName, req.User.Name, req.User.Email, req.Message, committerTime)
if err != nil {
var FormatTagError localrepo.FormatTagError
if errors.As(err, &FormatTagError) {
return nil, status.Errorf(codes.Unknown, "Rugged::InvalidError: failed to parse signature - expected prefix doesn't match actual")
}
var MktagError localrepo.MktagError
if errors.As(err, &MktagError) {
return nil, status.Errorf(codes.NotFound, "Gitlab::Git::CommitError: %s", err.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}
createdTag, err := catfile.GetTag(ctx, catFile, tagObjectID.Revision(), string(req.TagName), false, false)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
tagObject = &gitalypb.Tag{
Name: req.TagName,
Id: tagObjectID.String(),
Message: createdTag.Message,
MessageSize: createdTag.MessageSize,
//TargetCommit: is filled in below if needed
}
refObjectID = tagObjectID
} else {
tagObject = &gitalypb.Tag{
Name: req.TagName,
Id: peeledTargetObjectID.String(),
//TargetCommit: is filled in below if needed
}
}
referenceName := git.ReferenceName(fmt.Sprintf("refs/tags/%s", req.TagName))
if err := s.updateReferenceWithHooks(ctx, req.Repository, req.User, referenceName, refObjectID, git.ZeroOID); err != nil {
var preReceiveError preReceiveError
if errors.As(err, &preReceiveError) {
return &gitalypb.UserCreateTagResponse{
PreReceiveError: preReceiveError.message,
}, nil
}
var updateRefError updateRefError
if errors.As(err, &updateRefError) {
refNameOK, err := git.CheckRefFormat(ctx, s.gitCmdFactory, referenceName.String())
if refNameOK {
// The tag might not actually exist,
// perhaps update-ref died for some
// other reason. But saying that it
// does is what the Ruby code used to
// do, so let's follow it off that
// cliff.
return &gitalypb.UserCreateTagResponse{
Tag: nil,
Exists: true,
}, nil
}
var CheckRefFormatError git.CheckRefFormatError
if errors.As(err, &CheckRefFormatError) {
// It doesn't make sense either to
// tell the user to retry with an
// invalid ref name, but ditto on the
// Ruby bug-for-bug emulation.
return &gitalypb.UserCreateTagResponse{
Tag: nil,
Exists: true,
}, status.Errorf(codes.Unknown, "Gitlab::Git::CommitError: Could not update refs/tags/%s. Please refresh and try again.", req.TagName)
}
// Should only be reachable if "git
// check-ref-format"'s invocation is
// incorrect, or if it segfaults on startup
// etc.
return nil, status.Error(codes.Internal, err.Error())
}
// The Ruby code did not return this, but always an
// "Exists: true" on update-ref failure without any
// meaningful error. This is our "PANIC" response, if
// we've got an unknown error (it should all be
// updateRefError above) let's relay it to the
// caller. This should not happen.
return &gitalypb.UserCreateTagResponse{
Tag: nil,
Exists: true,
}, status.Error(codes.Internal, err.Error())
}
// Save ourselves looking this up earlier in case update-ref
// died
if peeledTargetObjectType == "commit" {
peeledTargetCommit, err := catfile.GetCommit(ctx, catFile, peeledTargetObjectID.Revision())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
tagObject.TargetCommit = peeledTargetCommit
}
return &gitalypb.UserCreateTagResponse{
Tag: tagObject,
}, nil
}
|