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
|
package ref
import (
"bytes"
"context"
"fmt"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/catfile"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper/lines"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
)
var localBranchFormatFields = []string{"%(refname)", "%(objectname)"}
func parseRef(ref []byte) ([][]byte, error) {
elements := bytes.Split(ref, []byte("\x00"))
if len(elements) != len(localBranchFormatFields) {
return nil, fmt.Errorf("error parsing ref %q", ref)
}
return elements, nil
}
func buildLocalBranch(name []byte, target *gitalypb.GitCommit) *gitalypb.FindLocalBranchResponse {
response := &gitalypb.FindLocalBranchResponse{
Name: name,
}
if target == nil {
return response
}
response.Commit = target
// all code below exists for compatibility and must be removed after
// https://gitlab.com/gitlab-org/gitlab/issues/205685
response.CommitId = target.Id
response.CommitSubject = target.Subject
if author := target.Author; author != nil {
response.CommitAuthor = &gitalypb.FindLocalBranchCommitAuthor{
Name: author.Name,
Email: author.Email,
Date: author.Date,
Timezone: author.Timezone,
}
}
if committer := target.Committer; committer != nil {
response.CommitCommitter = &gitalypb.FindLocalBranchCommitAuthor{
Name: committer.Name,
Email: committer.Email,
Date: committer.Date,
Timezone: committer.Timezone,
}
}
return response
}
func buildAllBranchesBranch(ctx context.Context, c catfile.Batch, elements [][]byte) (*gitalypb.FindAllBranchesResponse_Branch, error) {
target, err := catfile.GetCommit(ctx, c, git.Revision(elements[1]))
if err != nil {
return nil, err
}
return &gitalypb.FindAllBranchesResponse_Branch{
Name: elements[0],
Target: target,
}, nil
}
func buildBranch(ctx context.Context, c catfile.Batch, elements [][]byte) (*gitalypb.Branch, error) {
target, err := catfile.GetCommit(ctx, c, git.Revision(elements[1]))
if err != nil {
return nil, err
}
return &gitalypb.Branch{
Name: elements[0],
TargetCommit: target,
}, nil
}
func newFindLocalBranchesWriter(stream gitalypb.RefService_FindLocalBranchesServer, c catfile.Batch) lines.Sender {
return func(refs [][]byte) error {
var branches []*gitalypb.FindLocalBranchResponse
ctx := stream.Context()
for _, ref := range refs {
elements, err := parseRef(ref)
if err != nil {
return err
}
target, err := catfile.GetCommit(ctx, c, git.Revision(elements[1]))
if err != nil {
return err
}
branches = append(branches, buildLocalBranch(elements[0], target))
}
return stream.Send(&gitalypb.FindLocalBranchesResponse{Branches: branches})
}
}
func newFindAllBranchesWriter(stream gitalypb.RefService_FindAllBranchesServer, c catfile.Batch) lines.Sender {
return func(refs [][]byte) error {
var branches []*gitalypb.FindAllBranchesResponse_Branch
ctx := stream.Context()
for _, ref := range refs {
elements, err := parseRef(ref)
if err != nil {
return err
}
branch, err := buildAllBranchesBranch(ctx, c, elements)
if err != nil {
return err
}
branches = append(branches, branch)
}
return stream.Send(&gitalypb.FindAllBranchesResponse{Branches: branches})
}
}
func newFindAllRemoteBranchesWriter(stream gitalypb.RefService_FindAllRemoteBranchesServer, c catfile.Batch) lines.Sender {
return func(refs [][]byte) error {
var branches []*gitalypb.Branch
ctx := stream.Context()
for _, ref := range refs {
elements, err := parseRef(ref)
if err != nil {
return err
}
branch, err := buildBranch(ctx, c, elements)
if err != nil {
return err
}
branches = append(branches, branch)
}
return stream.Send(&gitalypb.FindAllRemoteBranchesResponse{Branches: branches})
}
}
|