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
|
package ref
import (
"bufio"
"context"
"io"
"strings"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/git/log"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *server) FindBranch(ctx context.Context, req *gitalypb.FindBranchRequest) (*gitalypb.FindBranchResponse, error) {
refName := string(req.GetName())
if len(refName) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "Branch name cannot be empty")
}
repo := req.GetRepository()
if strings.HasPrefix(refName, "refs/heads/") {
refName = strings.TrimPrefix(refName, "refs/heads/")
} else if strings.HasPrefix(refName, "heads/") {
refName = strings.TrimPrefix(refName, "heads/")
}
cmd, err := git.SafeCmd(ctx, repo, nil, git.SubCmd{
Name: "for-each-ref",
Flags: []git.Option{git.Flag{"--format=%(objectname)"}},
Args: []string{"refs/heads/" + refName},
})
if err != nil {
return nil, err
}
reader := bufio.NewReader(cmd)
revision, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
return &gitalypb.FindBranchResponse{}, nil
}
return nil, err
}
commit, err := log.GetCommit(ctx, repo, string(revision))
if err != nil {
return nil, err
}
if err := cmd.Wait(); err != nil {
return nil, err
}
return &gitalypb.FindBranchResponse{
Branch: &gitalypb.Branch{
Name: []byte(refName),
TargetCommit: commit,
},
}, nil
}
|