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

find_remote_repository.go « remote « service « gitaly « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c4b018a64819b54729df62937a3d082483e99594 (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
package remote

import (
	"bytes"
	"context"
	"io"

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

func (s *server) FindRemoteRepository(ctx context.Context, req *gitalypb.FindRemoteRepositoryRequest) (*gitalypb.FindRemoteRepositoryResponse, error) {
	if req.GetRemote() == "" {
		return nil, structerr.NewInvalidArgument("empty remote can't be checked.")
	}

	cmd, err := s.gitCmdFactory.NewWithoutRepo(ctx,
		git.Command{
			Name: "ls-remote",
			Args: []string{
				req.GetRemote(),
				"HEAD",
			},
		},
	)
	if err != nil {
		return nil, structerr.NewInternal("error executing git command: %w", err)
	}

	output, err := io.ReadAll(cmd)
	if err != nil {
		return nil, structerr.NewInternal("unable to read stdout: %w", err)
	}
	if err := cmd.Wait(); err != nil {
		return &gitalypb.FindRemoteRepositoryResponse{Exists: false}, nil
	}

	// The output of git-ls-remote is expected to be of the format:
	//
	//	58fbff2e0d3b620f591a748c158799ead87b51cd	HEAD\n
	objectID, refname, ok := bytes.Cut(output, []byte("\t"))
	if !ok {
		return &gitalypb.FindRemoteRepositoryResponse{Exists: false}, nil
	}

	// We've asked for HEAD, so the refname should match that.
	if !bytes.Equal(refname, []byte("HEAD\n")) {
		return &gitalypb.FindRemoteRepositoryResponse{Exists: false}, nil
	}

	// We have no way to ask the remote's object format via git-ls-remote(1), so all we can do
	// is to verify that the object hash matches something we know.
	switch len(objectID) {
	case git.ObjectHashSHA1.EncodedLen():
		return &gitalypb.FindRemoteRepositoryResponse{Exists: true}, nil
	case git.ObjectHashSHA256.EncodedLen():
		return &gitalypb.FindRemoteRepositoryResponse{Exists: true}, nil
	default:
		return &gitalypb.FindRemoteRepositoryResponse{Exists: false}, nil
	}
}