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
|
package ref
import (
"context"
"fmt"
"strings"
"gitlab.com/gitlab-org/gitaly/v14/internal/command"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
)
// RefExists returns true if the given reference exists. The ref must start with the string `ref/`
func (s *server) RefExists(ctx context.Context, in *gitalypb.RefExistsRequest) (*gitalypb.RefExistsResponse, error) {
ref := string(in.Ref)
if !isValidRefName(ref) {
return nil, helper.ErrInvalidArgument(fmt.Errorf("invalid refname"))
}
exists, err := s.refExists(ctx, in.Repository, ref)
if err != nil {
return nil, helper.ErrInternal(err)
}
return &gitalypb.RefExistsResponse{Value: exists}, nil
}
func (s *server) refExists(ctx context.Context, repo *gitalypb.Repository, ref string) (bool, error) {
cmd, err := s.gitCmdFactory.New(ctx, repo, git.SubCmd{
Name: "show-ref",
Flags: []git.Option{git.Flag{Name: "--verify"}, git.Flag{Name: "--quiet"}},
Args: []string{ref},
})
if err != nil {
return false, err
}
err = cmd.Wait()
if err == nil {
// Exit code 0: the ref exists
return true, nil
}
if code, ok := command.ExitStatus(err); ok && code == 1 {
// Exit code 1: the ref does not exist
return false, nil
}
// This will normally occur when exit code > 1
return false, err
}
func isValidRefName(refName string) bool {
return strings.HasPrefix(refName, "refs/")
}
|