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
|
package ref
import (
"context"
"errors"
"fmt"
"strings"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/git/localrepo"
"gitlab.com/gitlab-org/gitaly/internal/git/updateref"
"gitlab.com/gitlab-org/gitaly/internal/helper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *server) DeleteRefs(ctx context.Context, in *gitalypb.DeleteRefsRequest) (*gitalypb.DeleteRefsResponse, error) {
if err := validateDeleteRefRequest(in); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "DeleteRefs: %v", err)
}
updater, err := updateref.New(ctx, s.cfg, s.gitCmdFactory, in.GetRepository())
if err != nil {
if errors.Is(err, git.ErrInvalidArg) {
return nil, helper.ErrInvalidArgument(err)
}
return nil, helper.ErrInternal(err)
}
refnames, err := s.refsToRemove(ctx, in)
if err != nil {
return nil, helper.ErrInternal(err)
}
for _, ref := range refnames {
if err := updater.Delete(ref); err != nil {
return &gitalypb.DeleteRefsResponse{GitError: err.Error()}, nil
}
}
if err := updater.Wait(); err != nil {
return &gitalypb.DeleteRefsResponse{GitError: fmt.Sprintf("unable to delete refs: %s", err.Error())}, nil
}
return &gitalypb.DeleteRefsResponse{}, nil
}
func (s *server) refsToRemove(ctx context.Context, req *gitalypb.DeleteRefsRequest) ([]git.ReferenceName, error) {
if len(req.Refs) > 0 {
refs := make([]git.ReferenceName, len(req.Refs))
for i, ref := range req.Refs {
refs[i] = git.ReferenceName(ref)
}
return refs, nil
}
prefixes := make([]string, len(req.ExceptWithPrefix))
for i, prefix := range req.ExceptWithPrefix {
prefixes[i] = string(prefix)
}
existingRefs, err := localrepo.New(req.GetRepository(), s.cfg).GetReferences(ctx, "")
if err != nil {
return nil, err
}
var refs []git.ReferenceName
for _, existingRef := range existingRefs {
if hasAnyPrefix(existingRef.Name.String(), prefixes) {
continue
}
refs = append(refs, existingRef.Name)
}
return refs, nil
}
func hasAnyPrefix(s string, prefixes []string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(s, prefix) {
return true
}
}
return false
}
func validateDeleteRefRequest(req *gitalypb.DeleteRefsRequest) error {
if len(req.ExceptWithPrefix) > 0 && len(req.Refs) > 0 {
return fmt.Errorf("ExceptWithPrefix and Refs are mutually exclusive")
}
if len(req.ExceptWithPrefix) == 0 && len(req.Refs) == 0 { // You can't delete all refs
return fmt.Errorf("empty ExceptWithPrefix and Refs")
}
for _, prefix := range req.ExceptWithPrefix {
if len(prefix) == 0 {
return fmt.Errorf("empty prefix for exclusion")
}
}
for _, ref := range req.Refs {
if len(ref) == 0 {
return fmt.Errorf("empty ref")
}
}
return nil
}
|