diff options
Diffstat (limited to 'internal/gitaly/service/remote/find_remote_root_ref.go')
-rw-r--r-- | internal/gitaly/service/remote/find_remote_root_ref.go | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/internal/gitaly/service/remote/find_remote_root_ref.go b/internal/gitaly/service/remote/find_remote_root_ref.go index 374c483d9..7474751d4 100644 --- a/internal/gitaly/service/remote/find_remote_root_ref.go +++ b/internal/gitaly/service/remote/find_remote_root_ref.go @@ -3,9 +3,11 @@ package remote import ( "bufio" "context" + "fmt" "strings" "gitlab.com/gitlab-org/gitaly/internal/git" + "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -13,14 +15,32 @@ import ( const headPrefix = "HEAD branch: " -func (s *server) findRemoteRootRef(ctx context.Context, repo *gitalypb.Repository, remote string) (string, error) { - cmd, err := s.gitCmdFactory.New(ctx, repo, +func (s *server) findRemoteRootRef(ctx context.Context, request *gitalypb.FindRemoteRootRefRequest) (string, error) { + remoteName := request.Remote + var config []git.ConfigPair + + if request.RemoteUrl != "" { + remoteName = "inmemory" + config = []git.ConfigPair{ + {Key: "remote.inmemory.url", Value: request.RemoteUrl}, + } + + if authHeader := request.GetHttpAuthorizationHeader(); authHeader != "" { + config = append(config, git.ConfigPair{ + Key: fmt.Sprintf("http.%s.extraHeader", request.RemoteUrl), + Value: "Authorization: " + authHeader, + }) + } + } + + cmd, err := s.gitCmdFactory.New(ctx, request.Repository, git.SubSubCmd{ Name: "remote", Action: "show", - Args: []string{remote}, + Args: []string{remoteName}, }, - git.WithRefTxHook(ctx, repo, s.cfg), + git.WithRefTxHook(ctx, request.Repository, s.cfg), + git.WithConfigEnv(config...), ) if err != nil { return "", err @@ -31,7 +51,11 @@ func (s *server) findRemoteRootRef(ctx context.Context, repo *gitalypb.Repositor line := strings.TrimSpace(scanner.Text()) if strings.HasPrefix(line, headPrefix) { - return strings.TrimPrefix(line, headPrefix), nil + rootRef := strings.TrimPrefix(line, headPrefix) + if rootRef == "(unknown)" { + return "", status.Error(codes.NotFound, "no remote HEAD found") + } + return rootRef, nil } } @@ -48,18 +72,21 @@ func (s *server) findRemoteRootRef(ctx context.Context, repo *gitalypb.Repositor // FindRemoteRootRef queries the remote to determine its HEAD func (s *server) FindRemoteRootRef(ctx context.Context, in *gitalypb.FindRemoteRootRefRequest) (*gitalypb.FindRemoteRootRefResponse, error) { - remote := in.GetRemote() - if remote == "" { - return nil, status.Error(codes.InvalidArgument, "empty remote can't be queried") + //nolint:staticcheck // GetRemote() is deprecated + if in.GetRemote() == "" && in.GetRemoteUrl() == "" { + return nil, status.Error(codes.InvalidArgument, "got neither remote name nor URL") + } + //nolint:staticcheck // GetRemote() is deprecated + if in.GetRemote() != "" && in.GetRemoteUrl() != "" { + return nil, status.Error(codes.InvalidArgument, "got remote name and URL") + } + if in.Repository == nil { + return nil, status.Error(codes.InvalidArgument, "missing repository") } - ref, err := s.findRemoteRootRef(ctx, in.GetRepository(), remote) + ref, err := s.findRemoteRootRef(ctx, in) if err != nil { - if _, ok := status.FromError(err); ok { - return nil, err - } - - return nil, status.Errorf(codes.Internal, err.Error()) + return nil, helper.ErrInternal(err) } return &gitalypb.FindRemoteRootRefResponse{Ref: ref}, nil |