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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package remote
import (
"bufio"
"context"
"fmt"
"gitlab.com/gitlab-org/gitaly/v15/structerr"
"strings"
"gitlab.com/gitlab-org/gitaly/proto/v15/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/v15/internal/command"
"gitlab.com/gitlab-org/gitaly/v15/internal/git"
"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service"
)
const headPrefix = "HEAD branch: "
func (s *server) findRemoteRootRefCmd(ctx context.Context, request *gitalypb.FindRemoteRootRefRequest) (*command.Command, error) {
remoteURL := request.GetRemoteUrl()
var config []git.ConfigPair
if resolvedAddress := request.GetResolvedAddress(); resolvedAddress != "" {
modifiedURL, resolveConfig, err := git.GetURLAndResolveConfig(remoteURL, resolvedAddress)
if err != nil {
return nil, structerr.NewInvalidArgument("couldn't get curloptResolve config: %w", err)
}
remoteURL = modifiedURL
config = append(config, resolveConfig...)
}
config = append(config, git.ConfigPair{Key: "remote.inmemory.url", Value: remoteURL})
if authHeader := request.GetHttpAuthorizationHeader(); authHeader != "" {
config = append(config, git.ConfigPair{
Key: fmt.Sprintf("http.%s.extraHeader", request.RemoteUrl),
Value: "Authorization: " + authHeader,
})
}
//nolint:staticcheck
if host := request.GetHttpHost(); host != "" {
config = append(config, git.ConfigPair{
Key: fmt.Sprintf("http.%s.extraHeader", request.RemoteUrl),
Value: "Host: " + host,
})
}
return s.gitCmdFactory.New(ctx, request.Repository,
git.Command{
Name: "remote",
Action: "show",
Args: []string{"inmemory"},
},
git.WithRefTxHook(request.Repository),
git.WithConfigEnv(config...),
)
}
func (s *server) findRemoteRootRef(ctx context.Context, request *gitalypb.FindRemoteRootRefRequest) (string, error) {
cmd, err := s.findRemoteRootRefCmd(ctx, request)
if err != nil {
return "", err
}
scanner := bufio.NewScanner(cmd)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, headPrefix) {
rootRef := strings.TrimPrefix(line, headPrefix)
if rootRef == "(unknown)" {
return "", structerr.NewNotFound("no remote HEAD found")
}
return rootRef, nil
}
}
if err := scanner.Err(); err != nil {
return "", err
}
if err := cmd.Wait(); err != nil {
return "", err
}
return "", structerr.NewNotFound("couldn't query the remote HEAD")
}
// FindRemoteRootRef queries the remote to determine its HEAD
func (s *server) FindRemoteRootRef(ctx context.Context, in *gitalypb.FindRemoteRootRefRequest) (*gitalypb.FindRemoteRootRefResponse, error) {
if in.GetRemoteUrl() == "" {
return nil, structerr.NewInvalidArgument("missing remote URL")
}
if err := service.ValidateRepository(in.GetRepository()); err != nil {
return nil, structerr.NewInvalidArgument("%w", err)
}
ref, err := s.findRemoteRootRef(ctx, in)
if err != nil {
return nil, structerr.NewInternal("%w", err)
}
return &gitalypb.FindRemoteRootRefResponse{Ref: ref}, nil
}
|