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
|
package operations
import (
"context"
"errors"
"fmt"
"gitlab.com/gitlab-org/gitaly/v15/internal/git"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/localrepo"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/remoterepo"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/updateref"
"gitlab.com/gitlab-org/gitaly/v15/internal/git2go"
"gitlab.com/gitlab-org/gitaly/v15/internal/structerr"
"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
)
//nolint:revive // This is unintentionally missing documentation.
func (s *Server) UserRevert(ctx context.Context, req *gitalypb.UserRevertRequest) (*gitalypb.UserRevertResponse, error) {
if err := validateCherryPickOrRevertRequest(req); err != nil {
return nil, structerr.NewInvalidArgument("%w", err)
}
quarantineDir, quarantineRepo, err := s.quarantinedRepo(ctx, req.GetRepository())
if err != nil {
return nil, err
}
startRevision, err := s.fetchStartRevision(ctx, quarantineRepo, req)
if err != nil {
return nil, err
}
repoHadBranches, err := quarantineRepo.HasBranches(ctx)
if err != nil {
return nil, structerr.NewInternal("has branches: %w", err)
}
repoPath, err := quarantineRepo.Path()
if err != nil {
return nil, structerr.NewInternal("get path: %w", err)
}
var mainline uint
if len(req.Commit.ParentIds) > 1 {
mainline = 1
}
authorDate, err := dateFromProto(req)
if err != nil {
return nil, structerr.NewInvalidArgument("%w", err)
}
newrev, err := s.git2goExecutor.Revert(ctx, quarantineRepo, git2go.RevertCommand{
Repository: repoPath,
AuthorName: string(req.User.Name),
AuthorMail: string(req.User.Email),
AuthorDate: authorDate,
Message: string(req.Message),
Ours: startRevision.String(),
Revert: req.Commit.Id,
Mainline: mainline,
})
if err != nil {
if errors.As(err, &git2go.HasConflictsError{}) {
return &gitalypb.UserRevertResponse{
CreateTreeError: err.Error(),
CreateTreeErrorCode: gitalypb.UserRevertResponse_CONFLICT,
}, nil
} else if errors.As(err, &git2go.EmptyError{}) {
return &gitalypb.UserRevertResponse{
CreateTreeError: err.Error(),
CreateTreeErrorCode: gitalypb.UserRevertResponse_EMPTY,
}, nil
} else if errors.Is(err, git2go.ErrInvalidArgument) {
return nil, structerr.NewInvalidArgument("%w", err)
} else {
return nil, structerr.NewInternal("revert command: %w", err)
}
}
referenceName := git.NewReferenceNameFromBranchName(string(req.BranchName))
branchCreated := false
var oldrev git.ObjectID
if expectedOldOID := req.GetExpectedOldOid(); expectedOldOID != "" {
oldrev, err = git.ObjectHashSHA1.FromHex(expectedOldOID)
if err != nil {
return nil, structerr.NewInvalidArgument("invalid expected old object ID: %w", err).WithMetadata("old_object_id", expectedOldOID)
}
oldrev, err = quarantineRepo.ResolveRevision(
ctx, git.Revision(fmt.Sprintf("%s^{object}", oldrev)),
)
if err != nil {
return nil, structerr.NewInvalidArgument("cannot resolve expected old object ID: %w", err).
WithMetadata("old_object_id", expectedOldOID)
}
} else {
oldrev, err = quarantineRepo.ResolveRevision(ctx, referenceName.Revision()+"^{commit}")
if errors.Is(err, git.ErrReferenceNotFound) {
branchCreated = true
oldrev = git.ObjectHashSHA1.ZeroOID
} else if err != nil {
return nil, structerr.NewInvalidArgument("resolve ref: %w", err)
}
}
if req.DryRun {
newrev = startRevision
}
if !branchCreated {
ancestor, err := quarantineRepo.IsAncestor(ctx, oldrev.Revision(), newrev.Revision())
if err != nil {
return nil, structerr.NewInternal("checking for ancestry: %w", err)
}
if !ancestor {
return &gitalypb.UserRevertResponse{
CommitError: "Branch diverged",
}, nil
}
}
if err := s.updateReferenceWithHooks(ctx, req.GetRepository(), req.User, quarantineDir, referenceName, newrev, oldrev); err != nil {
var customHookErr updateref.CustomHookError
if errors.As(err, &customHookErr) {
return &gitalypb.UserRevertResponse{
PreReceiveError: customHookErr.Error(),
}, nil
}
return nil, fmt.Errorf("update reference with hooks: %w", err)
}
return &gitalypb.UserRevertResponse{
BranchUpdate: &gitalypb.OperationBranchUpdate{
CommitId: newrev.String(),
BranchCreated: branchCreated,
RepoCreated: !repoHadBranches,
},
}, nil
}
type requestFetchingStartRevision interface {
GetBranchName() []byte
GetStartRepository() *gitalypb.Repository
GetStartBranchName() []byte
}
func (s *Server) fetchStartRevision(
ctx context.Context,
localRepo *localrepo.Repo,
req requestFetchingStartRevision,
) (git.ObjectID, error) {
startBranchName := req.GetStartBranchName()
if len(startBranchName) == 0 {
startBranchName = req.GetBranchName()
}
var remoteRepo git.Repository = localRepo
if startRepository := req.GetStartRepository(); startRepository != nil {
var err error
remoteRepo, err = remoterepo.New(ctx, startRepository, s.conns)
if err != nil {
return "", structerr.NewInternal("%w", err)
}
}
startRevision, err := remoteRepo.ResolveRevision(ctx, git.Revision(fmt.Sprintf("%s^{commit}", startBranchName)))
if err != nil {
return "", structerr.NewInvalidArgument("resolve start ref: %w", err)
}
if req.GetStartRepository() == nil {
return startRevision, nil
}
_, err = localRepo.ResolveRevision(ctx, startRevision.Revision()+"^{commit}")
if errors.Is(err, git.ErrReferenceNotFound) {
if err := localRepo.FetchInternal(
ctx,
req.GetStartRepository(),
[]string{startRevision.String()},
localrepo.FetchOpts{Tags: localrepo.FetchOptsTagsNone},
); err != nil {
return "", structerr.NewInternal("fetch start: %w", err)
}
} else if err != nil {
return "", structerr.NewInvalidArgument("resolve start: %w", err)
}
return startRevision, nil
}
|