Welcome to mirror list, hosted at ThFree Co, Russian Federation.

find_refs_by_oid.go « ref « service « gitaly « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e493f90b9b8c9ea06b082b2dd3e5327a897d026 (plain)
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
package ref

import (
	"context"
	"errors"
	"strings"

	"gitlab.com/gitlab-org/gitaly/proto/v15/go/gitalypb"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git/gitpipe"
	"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service"
)

func (s *server) FindRefsByOID(ctx context.Context, in *gitalypb.FindRefsByOIDRequest) (*gitalypb.FindRefsByOIDResponse, error) {
	if err := validateFindRefsReq(in); err != nil {
		return nil, structerr.NewInvalidArgument("%w", err)
	}

	repo := s.localrepo(in.GetRepository())

	patterns := in.GetRefPatterns()
	if len(patterns) == 0 {
		patterns = []string{"refs/tags/", "refs/heads/"}
	}

	forEachRefIter := gitpipe.ForEachRef(
		ctx,
		repo,
		patterns,
		gitpipe.WithSortField(in.GetSortField()),
		gitpipe.WithPointsAt(in.GetOid()),
		gitpipe.WithCount(int(in.GetLimit())),
	)

	var refs []string
	for forEachRefIter.Next() {
		refs = append(refs, string(forEachRefIter.Result().ObjectName))
	}

	if err := forEachRefIter.Err(); err != nil {
		// git uses exit status 129 to indicate errors in command line usage
		// https://www.git-scm.com/docs/api-error-handling
		if strings.Contains(err.Error(), "exit status 129") {
			return nil, structerr.NewInvalidArgument("%w", err)
		}
		return nil, structerr.NewInternal("%w", err)
	}

	return &gitalypb.FindRefsByOIDResponse{
		Refs: refs,
	}, nil
}

func validateFindRefsReq(in *gitalypb.FindRefsByOIDRequest) error {
	if err := service.ValidateRepository(in.GetRepository()); err != nil {
		return err
	}

	if in.GetOid() == "" {
		return errors.New("empty Oid")
	}

	return nil
}