diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-09-07 10:29:19 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-09-07 11:16:11 +0300 |
commit | 86c5d708df567f605fe44f48389c504b9f170b3d (patch) | |
tree | 33274ea1d9cc584cce18df4763294c2582ab0960 /internal/gitaly/service/repository/fetch.go | |
parent | 8a0d83b496eaff459d55bc6825428f648059e8d9 (diff) |
gitaly: Move Gitaly-specific code into `internal/gitaly`
Since the introduction of Praefect, our code layout started to become
confusing: while Praefect code lives in `internal/praefect`,
Gitaly-specific code is all over the place and not neatly singled out.
This makes it hard at times to tell apart Praefect- and Gitaly-specific
from generic code.
To improve the situation, this commit thus moves most of the server
specific code into a new `internal/gitaly` package. Currently, this is
the `internal/config`, `internal/server`, `internal/service` and
`internal/rubyserver` packages, which are all main components of Gitaly.
The move was realized with the following script:
#!/bin/sh
mkdir -p internal/gitaly
git mv internal/{config,server,service,rubyserver} internal/gitaly/
find . -name '*.go' -exec sed -i \
-e 's|gitlab-org/gitaly/internal/rubyserver|gitlab-org/gitaly/internal/gitaly/rubyserver|' \
-e 's|gitlab-org/gitaly/internal/server|gitlab-org/gitaly/internal/gitaly/server|' \
-e 's|gitlab-org/gitaly/internal/service|gitlab-org/gitaly/internal/gitaly/service|' \
-e 's|gitlab-org/gitaly/internal/config|gitlab-org/gitaly/internal/gitaly/config|' {} \;
In addition to that, some minor adjustments were needed for tests which
used relative paths.
Diffstat (limited to 'internal/gitaly/service/repository/fetch.go')
-rw-r--r-- | internal/gitaly/service/repository/fetch.go | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/internal/gitaly/service/repository/fetch.go b/internal/gitaly/service/repository/fetch.go new file mode 100644 index 000000000..cb27a9bc7 --- /dev/null +++ b/internal/gitaly/service/repository/fetch.go @@ -0,0 +1,84 @@ +package repository + +import ( + "context" + "fmt" + + "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" + "gitlab.com/gitlab-org/gitaly/internal/git" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/rubyserver" + "gitlab.com/gitlab-org/gitaly/internal/gitalyssh" + "gitlab.com/gitlab-org/gitaly/internal/helper" + "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" + "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" +) + +func (s *server) FetchSourceBranch(ctx context.Context, req *gitalypb.FetchSourceBranchRequest) (*gitalypb.FetchSourceBranchResponse, error) { + if featureflag.IsDisabled(ctx, featureflag.GoFetchSourceBranch) { + return s.rubyFetchSourceBranch(ctx, req) + } + + if err := git.ValidateRevision(req.GetSourceBranch()); err != nil { + return nil, helper.ErrInvalidArgument(err) + } + + if err := git.ValidateRevision(req.GetTargetRef()); err != nil { + return nil, helper.ErrInvalidArgument(err) + } + + repoPath, err := helper.GetRepoPath(req.Repository) + if err != nil { + return nil, err + } + + refspec := fmt.Sprintf("%s:%s", req.GetSourceBranch(), req.GetTargetRef()) + + var remote string + var env []string + if helper.RepoPathEqual(req.GetRepository(), req.GetSourceRepository()) { + remote = "file://" + repoPath + } else { + remote = gitalyInternalURL + env, err = gitalyssh.UploadPackEnv(ctx, &gitalypb.SSHUploadPackRequest{Repository: req.SourceRepository}) + if err != nil { + return nil, err + } + } + + cmd, err := git.SafeBareCmd(ctx, git.CmdStream{}, env, + []git.Option{git.ValueFlag{"--git-dir", repoPath}}, + git.SubCmd{ + Name: "fetch", + Flags: []git.Option{git.Flag{Name: "--prune"}}, + Args: []string{remote, refspec}, + }, + ) + if err != nil { + return nil, err + } + if err := cmd.Wait(); err != nil { + // Design quirk: if the fetch fails, this RPC returns Result: false, but no error. + ctxlogrus.Extract(ctx). + WithField("repo_path", repoPath). + WithField("remote", remote). + WithField("refspec", refspec). + WithError(err).Warn("git fetch failed") + return &gitalypb.FetchSourceBranchResponse{Result: false}, nil + } + + return &gitalypb.FetchSourceBranchResponse{Result: true}, nil +} + +func (s *server) rubyFetchSourceBranch(ctx context.Context, req *gitalypb.FetchSourceBranchRequest) (*gitalypb.FetchSourceBranchResponse, error) { + client, err := s.ruby.RepositoryServiceClient(ctx) + if err != nil { + return nil, err + } + + clientCtx, err := rubyserver.SetHeaders(ctx, req.GetRepository()) + if err != nil { + return nil, err + } + + return client.FetchSourceBranch(clientCtx, req) +} |