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
|
package ref
import (
"fmt"
"strings"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/internal/command"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/helper"
"golang.org/x/net/context"
)
// RefExists returns true if the given reference exists. The ref must start with the string `ref/`
func (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 := refExists(ctx, in.Repository, ref)
if err != nil {
return nil, helper.ErrInternal(err)
}
return &gitalypb.RefExistsResponse{Value: exists}, nil
}
func refExists(ctx context.Context, repo *gitalypb.Repository, ref string) (bool, error) {
cmd, err := git.Command(ctx, repo, "show-ref", "--verify", "--quiet", 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/")
}
|