diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2021-02-02 11:04:16 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2021-02-05 17:39:16 +0300 |
commit | df12b1eda9561e17cee62786fbd1d58ac7f92f77 (patch) | |
tree | d4b848f545de82a735d7cab016caa9b2326c7fdd | |
parent | 3c11252567490bc5aef10e11509d5ac6e455f6bc (diff) |
Injection of the git.CommandFactory into ref package
The set of changes expands on other packages as well
because functionality of the ref package is used in them.
Part of: https://gitlab.com/gitlab-org/gitaly/-/issues/2699
19 files changed, 87 insertions, 77 deletions
diff --git a/internal/gitaly/service/commit/commits_by_message.go b/internal/gitaly/service/commit/commits_by_message.go index ef04e9527..7b1d3e6d8 100644 --- a/internal/gitaly/service/commit/commits_by_message.go +++ b/internal/gitaly/service/commit/commits_by_message.go @@ -6,6 +6,7 @@ import ( "github.com/golang/protobuf/proto" "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/helper" + "gitlab.com/gitlab-org/gitaly/internal/storage" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" ) @@ -28,14 +29,14 @@ func (s *server) CommitsByMessage(in *gitalypb.CommitsByMessageRequest, stream g return helper.ErrInvalidArgument(err) } - if err := s.commitsByMessage(in, stream); err != nil { + if err := s.commitsByMessage(s.locator, in, stream); err != nil { return helper.ErrInternal(err) } return nil } -func (s *server) commitsByMessage(in *gitalypb.CommitsByMessageRequest, stream gitalypb.CommitService_CommitsByMessageServer) error { +func (s *server) commitsByMessage(locator storage.Locator, in *gitalypb.CommitsByMessageRequest, stream gitalypb.CommitService_CommitsByMessageServer) error { ctx := stream.Context() sender := &commitsByMessageSender{stream: stream} @@ -54,7 +55,7 @@ func (s *server) commitsByMessage(in *gitalypb.CommitsByMessageRequest, stream g if len(revision) == 0 { var err error - revision, err = defaultBranchName(ctx, in.Repository) + revision, err = defaultBranchName(ctx, s.gitCmdFactory, in.Repository) if err != nil { return err } @@ -65,7 +66,7 @@ func (s *server) commitsByMessage(in *gitalypb.CommitsByMessageRequest, stream g paths = append(paths, string(path)) } - return sendCommits(stream.Context(), sender, s.gitCmdFactory, in.GetRepository(), []string{string(revision)}, paths, in.GetGlobalOptions(), gitLogExtraOptions...) + return sendCommits(stream.Context(), sender, locator, s.gitCmdFactory, in.GetRepository(), []string{string(revision)}, paths, in.GetGlobalOptions(), gitLogExtraOptions...) } func validateCommitsByMessageRequest(in *gitalypb.CommitsByMessageRequest) error { diff --git a/internal/gitaly/service/commit/find_all_commits.go b/internal/gitaly/service/commit/find_all_commits.go index 43dd4d803..9a7eb79c9 100644 --- a/internal/gitaly/service/commit/find_all_commits.go +++ b/internal/gitaly/service/commit/find_all_commits.go @@ -34,7 +34,7 @@ func (s *server) FindAllCommits(in *gitalypb.FindAllCommitsRequest, stream gital var revisions []string if len(in.GetRevision()) == 0 { - branchNames, err := _findBranchNamesFunc(stream.Context(), in.Repository) + branchNames, err := _findBranchNamesFunc(stream.Context(), git.NewExecCommandFactory(s.cfg), in.Repository) if err != nil { return helper.ErrInvalidArgument(err) } diff --git a/internal/gitaly/service/commit/find_all_commits_test.go b/internal/gitaly/service/commit/find_all_commits_test.go index 2ce379b96..e92653c6e 100644 --- a/internal/gitaly/service/commit/find_all_commits_test.go +++ b/internal/gitaly/service/commit/find_all_commits_test.go @@ -7,6 +7,7 @@ import ( "github.com/golang/protobuf/ptypes/timestamp" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/ref" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -18,7 +19,7 @@ func TestSuccessfulFindAllCommitsRequest(t *testing.T) { _findBranchNamesFunc = ref.FindBranchNames }() - _findBranchNamesFunc = func(ctx context.Context, repo *gitalypb.Repository) ([][]byte, error) { + _findBranchNamesFunc = func(ctx context.Context, gitCmdFactory git.CommandFactory, repo *gitalypb.Repository) ([][]byte, error) { return [][]byte{ []byte("few-commits"), []byte("two-commits"), diff --git a/internal/gitaly/service/commit/find_commits.go b/internal/gitaly/service/commit/find_commits.go index ebcbed34b..7a1400bcf 100644 --- a/internal/gitaly/service/commit/find_commits.go +++ b/internal/gitaly/service/commit/find_commits.go @@ -29,7 +29,7 @@ func (s *server) FindCommits(req *gitalypb.FindCommitsRequest, stream gitalypb.C // migrated. if revision := req.Revision; len(revision) == 0 && !req.GetAll() { var err error - req.Revision, err = defaultBranchName(ctx, req.Repository) + req.Revision, err = defaultBranchName(ctx, git.NewExecCommandFactory(s.cfg), req.Repository) if err != nil { return helper.ErrInternal(fmt.Errorf("defaultBranchName: %v", err)) } diff --git a/internal/gitaly/service/commit/languages.go b/internal/gitaly/service/commit/languages.go index 4f867bc28..2a14556f5 100644 --- a/internal/gitaly/service/commit/languages.go +++ b/internal/gitaly/service/commit/languages.go @@ -30,7 +30,7 @@ func (s *server) CommitLanguages(ctx context.Context, req *gitalypb.CommitLangua revision := string(req.Revision) if revision == "" { - defaultBranch, err := ref.DefaultBranchName(ctx, req.Repository) + defaultBranch, err := ref.DefaultBranchName(ctx, git.NewExecCommandFactory(s.cfg), req.Repository) if err != nil { return nil, err } diff --git a/internal/gitaly/service/commit/list_files.go b/internal/gitaly/service/commit/list_files.go index 7a4d811ec..cb73c2311 100644 --- a/internal/gitaly/service/commit/list_files.go +++ b/internal/gitaly/service/commit/list_files.go @@ -33,7 +33,7 @@ func (s *server) ListFiles(in *gitalypb.ListFilesRequest, stream gitalypb.Commit revision := string(in.GetRevision()) if len(revision) == 0 { - defaultBranch, err := defaultBranchName(stream.Context(), repo) + defaultBranch, err := defaultBranchName(stream.Context(), git.NewExecCommandFactory(s.cfg), repo) if err != nil { return helper.DecorateError(codes.NotFound, fmt.Errorf("revision not found %q", revision)) } diff --git a/internal/gitaly/service/commit/list_files_test.go b/internal/gitaly/service/commit/list_files_test.go index cfe3ce88c..59d0a94fa 100644 --- a/internal/gitaly/service/commit/list_files_test.go +++ b/internal/gitaly/service/commit/list_files_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/ref" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -40,7 +41,7 @@ var ( ) func TestListFiles_success(t *testing.T) { - defaultBranchName = func(ctx context.Context, _ *gitalypb.Repository) ([]byte, error) { + defaultBranchName = func(context.Context, git.CommandFactory, *gitalypb.Repository) ([]byte, error) { return []byte("test-do-not-touch"), nil } defer func() { diff --git a/internal/gitaly/service/ref/list_new_blobs.go b/internal/gitaly/service/ref/list_new_blobs.go index ea8dda4c0..44c546295 100644 --- a/internal/gitaly/service/ref/list_new_blobs.go +++ b/internal/gitaly/service/ref/list_new_blobs.go @@ -33,7 +33,7 @@ func (s *server) listNewBlobs(in *gitalypb.ListNewBlobsRequest, stream gitalypb. } // the added ^ is to negate the oid since there is a --not option that comes earlier in the arg list - revList, err := git.NewCommand(ctx, in.GetRepository(), nil, git.SubCmd{Name: "rev-list", Flags: cmdFlags, Args: []string{"^" + oid}}) + revList, err := s.gitCmdFactory.New(ctx, in.GetRepository(), nil, git.SubCmd{Name: "rev-list", Flags: cmdFlags, Args: []string{"^" + oid}}) if err != nil { return err } diff --git a/internal/gitaly/service/ref/list_new_commits.go b/internal/gitaly/service/ref/list_new_commits.go index d84e586f4..1b0333c84 100644 --- a/internal/gitaly/service/ref/list_new_commits.go +++ b/internal/gitaly/service/ref/list_new_commits.go @@ -26,7 +26,7 @@ func (s *server) ListNewCommits(in *gitalypb.ListNewCommitsRequest, stream gital func (s *server) listNewCommits(in *gitalypb.ListNewCommitsRequest, stream gitalypb.RefService_ListNewCommitsServer, oid string) error { ctx := stream.Context() - revList, err := git.NewCommand(ctx, in.GetRepository(), nil, git.SubCmd{ + revList, err := s.gitCmdFactory.New(ctx, in.GetRepository(), nil, git.SubCmd{ Name: "rev-list", Flags: []git.Option{git.Flag{Name: "--not"}, git.Flag{Name: "--all"}}, Args: []string{"^" + oid}, // the added ^ is to negate the oid since there is a --not option that comes earlier in the arg list diff --git a/internal/gitaly/service/ref/pack_refs.go b/internal/gitaly/service/ref/pack_refs.go index 9e8cbe9e6..c360c428d 100644 --- a/internal/gitaly/service/ref/pack_refs.go +++ b/internal/gitaly/service/ref/pack_refs.go @@ -11,12 +11,12 @@ import ( "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" ) -func (server) PackRefs(ctx context.Context, in *gitalypb.PackRefsRequest) (*gitalypb.PackRefsResponse, error) { +func (s *server) PackRefs(ctx context.Context, in *gitalypb.PackRefsRequest) (*gitalypb.PackRefsResponse, error) { if err := validatePackRefsRequest(in); err != nil { return nil, helper.ErrInvalidArgument(err) } - if err := packRefs(ctx, in.GetRepository(), in.GetAllRefs()); err != nil { + if err := s.packRefs(ctx, in.GetRepository(), in.GetAllRefs()); err != nil { return nil, helper.ErrInternal(err) } @@ -30,8 +30,8 @@ func validatePackRefsRequest(in *gitalypb.PackRefsRequest) error { return nil } -func packRefs(ctx context.Context, repository repository.GitRepo, all bool) error { - cmd, err := git.NewCommand(ctx, repository, nil, git.SubCmd{ +func (s *server) packRefs(ctx context.Context, repository repository.GitRepo, all bool) error { + cmd, err := s.gitCmdFactory.New(ctx, repository, nil, git.SubCmd{ Name: "pack-refs", Flags: []git.Option{git.Flag{Name: "--all"}}, }) diff --git a/internal/gitaly/service/ref/refexists.go b/internal/gitaly/service/ref/refexists.go index 437f1177d..4bcd54a12 100644 --- a/internal/gitaly/service/ref/refexists.go +++ b/internal/gitaly/service/ref/refexists.go @@ -12,14 +12,14 @@ import ( ) // RefExists returns true if the given reference exists. The ref must start with the string `ref/` -func (server) RefExists(ctx context.Context, in *gitalypb.RefExistsRequest) (*gitalypb.RefExistsResponse, error) { +func (s *server) RefExists(ctx context.Context, in *gitalypb.RefExistsRequest) (*gitalypb.RefExistsResponse, error) { ref := string(in.Ref) if !isValidRefName(ref) { return nil, helper.ErrInvalidArgument(fmt.Errorf("invalid refname")) } - exists, err := refExists(ctx, in.Repository, ref) + exists, err := s.refExists(ctx, in.Repository, ref) if err != nil { return nil, helper.ErrInternal(err) } @@ -27,8 +27,8 @@ func (server) RefExists(ctx context.Context, in *gitalypb.RefExistsRequest) (*gi return &gitalypb.RefExistsResponse{Value: exists}, nil } -func refExists(ctx context.Context, repo *gitalypb.Repository, ref string) (bool, error) { - cmd, err := git.NewCommand(ctx, repo, nil, git.SubCmd{ +func (s *server) refExists(ctx context.Context, repo *gitalypb.Repository, ref string) (bool, error) { + cmd, err := s.gitCmdFactory.New(ctx, repo, nil, git.SubCmd{ Name: "show-ref", Flags: []git.Option{git.Flag{Name: "--verify"}, git.Flag{Name: "--quiet"}}, Args: []string{ref}, diff --git a/internal/gitaly/service/ref/refname.go b/internal/gitaly/service/ref/refname.go index 199a07f23..6c191e50f 100644 --- a/internal/gitaly/service/ref/refname.go +++ b/internal/gitaly/service/ref/refname.go @@ -20,7 +20,7 @@ func (s *server) FindRefName(ctx context.Context, in *gitalypb.FindRefNameReques return nil, helper.ErrInvalidArgument(fmt.Errorf("empty commit sha")) } - ref, err := findRefName(ctx, in.Repository, in.CommitId, string(in.Prefix)) + ref, err := s.findRefName(ctx, in.Repository, in.CommitId, string(in.Prefix)) if err != nil { return nil, helper.ErrInternal(err) } @@ -29,7 +29,7 @@ func (s *server) FindRefName(ctx context.Context, in *gitalypb.FindRefNameReques } // We assume `repo` and `commitID` and `prefix` are non-empty -func findRefName(ctx context.Context, repo *gitalypb.Repository, commitID, prefix string) (string, error) { +func (s *server) findRefName(ctx context.Context, repo *gitalypb.Repository, commitID, prefix string) (string, error) { flags := []git.Option{ git.Flag{Name: "--format=%(refname)"}, git.Flag{Name: "--count=1"}, @@ -43,7 +43,7 @@ func findRefName(ctx context.Context, repo *gitalypb.Repository, commitID, prefi subCmd.Flags = flags subCmd.Args = []string{prefix} - cmd, err := git.NewCommand(ctx, repo, nil, subCmd) + cmd, err := s.gitCmdFactory.New(ctx, repo, nil, subCmd) if err != nil { return "", err } diff --git a/internal/gitaly/service/ref/refnames.go b/internal/gitaly/service/ref/refnames.go index 6e9f2f0a4..cda557116 100644 --- a/internal/gitaly/service/ref/refnames.go +++ b/internal/gitaly/service/ref/refnames.go @@ -15,7 +15,7 @@ import ( func (s *server) FindAllBranchNames(in *gitalypb.FindAllBranchNamesRequest, stream gitalypb.RefService_FindAllBranchNamesServer) error { chunker := chunk.New(&findAllBranchNamesSender{stream: stream}) - return listRefNames(stream.Context(), chunker, "refs/heads", in.Repository, nil) + return s.listRefNames(stream.Context(), chunker, "refs/heads", in.Repository, nil) } type findAllBranchNamesSender struct { @@ -36,7 +36,7 @@ func (ts *findAllBranchNamesSender) Send() error { func (s *server) FindAllTagNames(in *gitalypb.FindAllTagNamesRequest, stream gitalypb.RefService_FindAllTagNamesServer) error { chunker := chunk.New(&findAllTagNamesSender{stream: stream}) - return listRefNames(stream.Context(), chunker, "refs/tags", in.Repository, nil) + return s.listRefNames(stream.Context(), chunker, "refs/tags", in.Repository, nil) } type findAllTagNamesSender struct { @@ -53,7 +53,7 @@ func (ts *findAllTagNamesSender) Send() error { return ts.stream.Send(&gitalypb.FindAllTagNamesResponse{Names: ts.tagNames}) } -func listRefNames(ctx context.Context, chunker *chunk.Chunker, prefix string, repo *gitalypb.Repository, extraArgs []string) error { +func (s *server) listRefNames(ctx context.Context, chunker *chunk.Chunker, prefix string, repo *gitalypb.Repository, extraArgs []string) error { flags := []git.Option{ git.Flag{Name: "--format=%(refname)"}, } @@ -62,7 +62,7 @@ func listRefNames(ctx context.Context, chunker *chunk.Chunker, prefix string, re flags = append(flags, git.Flag{arg}) } - cmd, err := git.NewCommand(ctx, repo, nil, git.SubCmd{ + cmd, err := s.gitCmdFactory.New(ctx, repo, nil, git.SubCmd{ Name: "for-each-ref", Flags: flags, Args: []string{prefix}, diff --git a/internal/gitaly/service/ref/refnames_containing.go b/internal/gitaly/service/ref/refnames_containing.go index b30159e47..1c3678812 100644 --- a/internal/gitaly/service/ref/refnames_containing.go +++ b/internal/gitaly/service/ref/refnames_containing.go @@ -14,14 +14,14 @@ import ( // ListBranchNamesContainingCommit returns a maximum of in.GetLimit() Branch names // which contain the SHA1 passed as argument -func (*server) ListBranchNamesContainingCommit(in *gitalypb.ListBranchNamesContainingCommitRequest, stream gitalypb.RefService_ListBranchNamesContainingCommitServer) error { +func (s *server) ListBranchNamesContainingCommit(in *gitalypb.ListBranchNamesContainingCommitRequest, stream gitalypb.RefService_ListBranchNamesContainingCommitServer) error { if err := git.ValidateObjectID(in.GetCommitId()); err != nil { return helper.ErrInvalidArgument(err) } chunker := chunk.New(&branchNamesContainingCommitSender{stream: stream}) ctx := stream.Context() - if err := listRefNames(ctx, chunker, "refs/heads", in.Repository, containingArgs(in)); err != nil { + if err := s.listRefNames(ctx, chunker, "refs/heads", in.Repository, containingArgs(in)); err != nil { return helper.ErrInternal(err) } @@ -57,14 +57,14 @@ func (bs *branchNamesContainingCommitSender) Send() error { // ListTagNamesContainingCommit returns a maximum of in.GetLimit() Tag names // which contain the SHA1 passed as argument -func (*server) ListTagNamesContainingCommit(in *gitalypb.ListTagNamesContainingCommitRequest, stream gitalypb.RefService_ListTagNamesContainingCommitServer) error { +func (s *server) ListTagNamesContainingCommit(in *gitalypb.ListTagNamesContainingCommitRequest, stream gitalypb.RefService_ListTagNamesContainingCommitServer) error { if err := git.ValidateObjectID(in.GetCommitId()); err != nil { return helper.ErrInvalidArgument(err) } chunker := chunk.New(&tagNamesContainingCommitSender{stream: stream}) ctx := stream.Context() - if err := listRefNames(ctx, chunker, "refs/tags", in.Repository, containingArgs(in)); err != nil { + if err := s.listRefNames(ctx, chunker, "refs/tags", in.Repository, containingArgs(in)); err != nil { return helper.ErrInternal(err) } diff --git a/internal/gitaly/service/ref/refs.go b/internal/gitaly/service/ref/refs.go index 2d580dd03..3849895c0 100644 --- a/internal/gitaly/service/ref/refs.go +++ b/internal/gitaly/service/ref/refs.go @@ -39,7 +39,7 @@ type findRefsOpts struct { lines.SenderOpts } -func findRefs(ctx context.Context, writer lines.Sender, repo *gitalypb.Repository, patterns []string, opts *findRefsOpts) error { +func (s *server) findRefs(ctx context.Context, writer lines.Sender, repo *gitalypb.Repository, patterns []string, opts *findRefsOpts) error { var options []git.Option if len(opts.cmdArgs) == 0 { @@ -48,7 +48,7 @@ func findRefs(ctx context.Context, writer lines.Sender, repo *gitalypb.Repositor options = append(options, opts.cmdArgs...) } - cmd, err := git.NewCommand(ctx, repo, nil, git.SubCmd{ + cmd, err := s.gitCmdFactory.New(ctx, repo, nil, git.SubCmd{ Name: "for-each-ref", Flags: options, Args: patterns, @@ -88,7 +88,7 @@ func (t *tagSender) Send() error { } func (s *server) parseAndReturnTags(ctx context.Context, repo *gitalypb.Repository, stream gitalypb.RefService_FindAllTagsServer) error { - tagsCmd, err := git.NewCommand(ctx, repo, nil, git.SubCmd{ + tagsCmd, err := s.gitCmdFactory.New(ctx, repo, nil, git.SubCmd{ Name: "for-each-ref", Flags: []git.Option{ git.ValueFlag{"--format", tagFormat}, @@ -154,10 +154,10 @@ func (s *server) validateFindAllTagsRequest(request *gitalypb.FindAllTagsRequest return nil } -func _findBranchNames(ctx context.Context, repo *gitalypb.Repository) ([][]byte, error) { +func _findBranchNames(ctx context.Context, gitCmdFactory git.CommandFactory, repo *gitalypb.Repository) ([][]byte, error) { var names [][]byte - cmd, err := git.NewCommand(ctx, repo, nil, git.SubCmd{ + cmd, err := gitCmdFactory.New(ctx, repo, nil, git.SubCmd{ Name: "for-each-ref", Flags: []git.Option{git.Flag{Name: "--format=%(refname)"}}, Args: []string{"refs/heads"}}, @@ -181,10 +181,10 @@ func _findBranchNames(ctx context.Context, repo *gitalypb.Repository) ([][]byte, return names, nil } -func _headReference(ctx context.Context, repo *gitalypb.Repository) ([]byte, error) { +func _headReference(ctx context.Context, gitCmdFactory git.CommandFactory, repo *gitalypb.Repository) ([]byte, error) { var headRef []byte - cmd, err := git.NewCommand(ctx, repo, nil, git.SubCmd{ + cmd, err := gitCmdFactory.New(ctx, repo, nil, git.SubCmd{ Name: "rev-parse", Flags: []git.Option{git.Flag{Name: "--symbolic-full-name"}}, Args: []string{"HEAD"}, @@ -214,8 +214,8 @@ func _headReference(ctx context.Context, repo *gitalypb.Repository) ([]byte, err } // SetDefaultBranchRef overwrites the default branch ref for the repository -func SetDefaultBranchRef(ctx context.Context, repo *gitalypb.Repository, ref string, cfg config.Cfg) error { - cmd, err := git.NewCommand(ctx, repo, nil, git.SubCmd{ +func SetDefaultBranchRef(ctx context.Context, gitCmdFactory git.CommandFactory, repo *gitalypb.Repository, ref string, cfg config.Cfg) error { + cmd, err := gitCmdFactory.New(ctx, repo, nil, git.SubCmd{ Name: "symbolic-ref", Args: []string{"HEAD", ref}, }, git.WithRefTxHook(ctx, repo, cfg)) @@ -226,8 +226,8 @@ func SetDefaultBranchRef(ctx context.Context, repo *gitalypb.Repository, ref str } // DefaultBranchName looks up the name of the default branch given a repoPath -func DefaultBranchName(ctx context.Context, repo *gitalypb.Repository) ([]byte, error) { - branches, err := FindBranchNames(ctx, repo) +func DefaultBranchName(ctx context.Context, gitCmdFactory git.CommandFactory, repo *gitalypb.Repository) ([]byte, error) { + branches, err := FindBranchNames(ctx, gitCmdFactory, repo) if err != nil { return nil, err @@ -244,7 +244,7 @@ func DefaultBranchName(ctx context.Context, repo *gitalypb.Repository) ([]byte, } hasMaster := false - headRef, err := headReference(ctx, repo) + headRef, err := headReference(ctx, gitCmdFactory, repo) if err != nil { return nil, err } @@ -268,7 +268,7 @@ func DefaultBranchName(ctx context.Context, repo *gitalypb.Repository) ([]byte, // FindDefaultBranchName returns the default branch name for the given repository func (s *server) FindDefaultBranchName(ctx context.Context, in *gitalypb.FindDefaultBranchNameRequest) (*gitalypb.FindDefaultBranchNameResponse, error) { - defaultBranchName, err := DefaultBranchName(ctx, in.Repository) + defaultBranchName, err := DefaultBranchName(ctx, s.gitCmdFactory, in.Repository) if err != nil { return nil, helper.ErrInternal(err) } @@ -313,7 +313,7 @@ func (s *server) findLocalBranches(in *gitalypb.FindLocalBranchesRequest, stream git.Flag{Name: "--sort=" + parseSortKey(in.GetSortBy())}, } - return findRefs(ctx, writer, in.Repository, []string{"refs/heads"}, opts) + return s.findRefs(ctx, writer, in.Repository, []string{"refs/heads"}, opts) } func (s *server) FindAllBranches(in *gitalypb.FindAllBranchesRequest, stream gitalypb.RefService_FindAllBranchesServer) error { @@ -333,7 +333,7 @@ func (s *server) findAllBranches(in *gitalypb.FindAllBranchesRequest, stream git patterns := []string{"refs/heads", "refs/remotes"} if in.MergedOnly { - defaultBranchName, err := DefaultBranchName(stream.Context(), in.Repository) + defaultBranchName, err := DefaultBranchName(stream.Context(), s.gitCmdFactory, in.Repository) if err != nil { return err } @@ -360,7 +360,7 @@ func (s *server) findAllBranches(in *gitalypb.FindAllBranchesRequest, stream git writer := newFindAllBranchesWriter(stream, c) - return findRefs(ctx, writer, in.Repository, patterns, opts) + return s.findRefs(ctx, writer, in.Repository, patterns, opts) } func (s *server) FindTag(ctx context.Context, in *gitalypb.FindTagRequest) (*gitalypb.FindTagResponse, error) { @@ -413,7 +413,7 @@ func parseTagLine(ctx context.Context, c catfile.Batch, tagLine string) (*gitaly } func (s *server) findTag(ctx context.Context, repository *gitalypb.Repository, tagName []byte) (*gitalypb.Tag, error) { - tagCmd, err := git.NewCommand(ctx, repository, nil, + tagCmd, err := s.gitCmdFactory.New(ctx, repository, nil, git.SubCmd{ Name: "tag", Flags: []git.Option{ diff --git a/internal/gitaly/service/ref/refs_test.go b/internal/gitaly/service/ref/refs_test.go index 7dccb28a5..f7d64c317 100644 --- a/internal/gitaly/service/ref/refs_test.go +++ b/internal/gitaly/service/ref/refs_test.go @@ -272,7 +272,7 @@ func TestHeadReference(t *testing.T) { testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - headRef, err := headReference(ctx, testRepo) + headRef, err := headReference(ctx, git.NewExecCommandFactory(config.Config), testRepo) if err != nil { t.Fatal(err) } @@ -294,7 +294,7 @@ func TestHeadReferenceWithNonExistingHead(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - headRef, err := headReference(ctx, testRepo) + headRef, err := headReference(ctx, git.NewExecCommandFactory(config.Config), testRepo) if err != nil { t.Fatal(err) } @@ -329,10 +329,11 @@ func TestSetDefaultBranchRef(t *testing.T) { testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - err := SetDefaultBranchRef(ctx, testRepo, tc.ref, config.Config) + gitCmdFactory := git.NewExecCommandFactory(config.Config) + err := SetDefaultBranchRef(ctx, gitCmdFactory, testRepo, tc.ref, config.Config) require.NoError(t, err) - newRef, err := DefaultBranchName(ctx, testRepo) + newRef, err := DefaultBranchName(ctx, gitCmdFactory, testRepo) require.NoError(t, err) require.Equal(t, tc.expectedRef, string(newRef)) @@ -352,47 +353,51 @@ func TestDefaultBranchName(t *testing.T) { testCases := []struct { desc string - findBranchNames func(context.Context, *gitalypb.Repository) ([][]byte, error) - headReference func(context.Context, *gitalypb.Repository) ([]byte, error) + findBranchNames func(context.Context, git.CommandFactory, *gitalypb.Repository) ([][]byte, error) + headReference func(context.Context, git.CommandFactory, *gitalypb.Repository) ([]byte, error) expected []byte }{ { desc: "Get first branch when only one branch exists", expected: []byte("refs/heads/foo"), - findBranchNames: func(context.Context, *gitalypb.Repository) ([][]byte, error) { + findBranchNames: func(context.Context, git.CommandFactory, *gitalypb.Repository) ([][]byte, error) { return [][]byte{[]byte("refs/heads/foo")}, nil }, - headReference: func(context.Context, *gitalypb.Repository) ([]byte, error) { return nil, nil }, + headReference: func(context.Context, git.CommandFactory, *gitalypb.Repository) ([]byte, error) { return nil, nil }, }, { - desc: "Get empy ref if no branches exists", - expected: nil, - findBranchNames: func(context.Context, *gitalypb.Repository) ([][]byte, error) { return [][]byte{}, nil }, - headReference: func(context.Context, *gitalypb.Repository) ([]byte, error) { return nil, nil }, + desc: "Get empy ref if no branches exists", + expected: nil, + findBranchNames: func(context.Context, git.CommandFactory, *gitalypb.Repository) ([][]byte, error) { + return [][]byte{}, nil + }, + headReference: func(context.Context, git.CommandFactory, *gitalypb.Repository) ([]byte, error) { return nil, nil }, }, { desc: "Get the name of the head reference when more than one branch exists", expected: []byte("refs/heads/bar"), - findBranchNames: func(context.Context, *gitalypb.Repository) ([][]byte, error) { + findBranchNames: func(context.Context, git.CommandFactory, *gitalypb.Repository) ([][]byte, error) { return [][]byte{[]byte("refs/heads/foo"), []byte("refs/heads/bar")}, nil }, - headReference: func(context.Context, *gitalypb.Repository) ([]byte, error) { return []byte("refs/heads/bar"), nil }, + headReference: func(context.Context, git.CommandFactory, *gitalypb.Repository) ([]byte, error) { + return []byte("refs/heads/bar"), nil + }, }, { desc: "Get `ref/heads/master` when several branches exist", expected: []byte("refs/heads/master"), - findBranchNames: func(context.Context, *gitalypb.Repository) ([][]byte, error) { + findBranchNames: func(context.Context, git.CommandFactory, *gitalypb.Repository) ([][]byte, error) { return [][]byte{[]byte("refs/heads/foo"), []byte("refs/heads/master"), []byte("refs/heads/bar")}, nil }, - headReference: func(context.Context, *gitalypb.Repository) ([]byte, error) { return nil, nil }, + headReference: func(context.Context, git.CommandFactory, *gitalypb.Repository) ([]byte, error) { return nil, nil }, }, { desc: "Get the name of the first branch when several branches exists and no other conditions are met", expected: []byte("refs/heads/foo"), - findBranchNames: func(context.Context, *gitalypb.Repository) ([][]byte, error) { + findBranchNames: func(context.Context, git.CommandFactory, *gitalypb.Repository) ([][]byte, error) { return [][]byte{[]byte("refs/heads/foo"), []byte("refs/heads/bar"), []byte("refs/heads/baz")}, nil }, - headReference: func(context.Context, *gitalypb.Repository) ([]byte, error) { return nil, nil }, + headReference: func(context.Context, git.CommandFactory, *gitalypb.Repository) ([]byte, error) { return nil, nil }, }, } @@ -402,7 +407,7 @@ func TestDefaultBranchName(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - defaultBranch, err := DefaultBranchName(ctx, testRepo) + defaultBranch, err := DefaultBranchName(ctx, git.NewExecCommandFactory(config.Config), testRepo) if err != nil { t.Fatal(err) } diff --git a/internal/gitaly/service/ref/remote_branches.go b/internal/gitaly/service/ref/remote_branches.go index 01776f221..f753c4c9a 100644 --- a/internal/gitaly/service/ref/remote_branches.go +++ b/internal/gitaly/service/ref/remote_branches.go @@ -39,7 +39,7 @@ func (s *server) findAllRemoteBranches(req *gitalypb.FindAllRemoteBranchesReques opts.cmdArgs = args writer := newFindAllRemoteBranchesWriter(stream, c) - return findRefs(ctx, writer, req.GetRepository(), patterns, opts) + return s.findRefs(ctx, writer, req.GetRepository(), patterns, opts) } func validateFindAllRemoteBranchesRequest(req *gitalypb.FindAllRemoteBranchesRequest) error { diff --git a/internal/gitaly/service/ref/testhelper_test.go b/internal/gitaly/service/ref/testhelper_test.go index b402dffe4..5055e7b21 100644 --- a/internal/gitaly/service/ref/testhelper_test.go +++ b/internal/gitaly/service/ref/testhelper_test.go @@ -44,13 +44,15 @@ func testMain(m *testing.M) int { } func runRefServiceServer(t *testing.T) (func(), string) { - locator := config.NewLocator(config.Config) - txManager := transaction.NewManager(config.Config) - hookManager := hook.NewManager(locator, txManager, hook.GitlabAPIStub, config.Config) - - srv := testhelper.NewServer(t, nil, nil, testhelper.WithInternalSocket(config.Config)) - gitalypb.RegisterRefServiceServer(srv.GrpcServer(), NewServer(config.Config, locator, git.NewExecCommandFactory(config.Config))) - gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hookservice.NewServer(config.Config, hookManager)) + cfg := config.Config + locator := config.NewLocator(cfg) + txManager := transaction.NewManager(cfg) + hookManager := hook.NewManager(locator, txManager, hook.GitlabAPIStub, cfg) + gitCmdFactory := git.NewExecCommandFactory(cfg) + + srv := testhelper.NewServer(t, nil, nil, testhelper.WithInternalSocket(cfg)) + gitalypb.RegisterRefServiceServer(srv.GrpcServer(), NewServer(cfg, locator, gitCmdFactory)) + gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hookservice.NewServer(cfg, hookManager)) srv.Start(t) return srv.Stop, "unix://" + srv.Socket() diff --git a/internal/gitaly/service/remote/fetch_internal_remote.go b/internal/gitaly/service/remote/fetch_internal_remote.go index 84bfc20e8..2023b8c9f 100644 --- a/internal/gitaly/service/remote/fetch_internal_remote.go +++ b/internal/gitaly/service/remote/fetch_internal_remote.go @@ -57,13 +57,13 @@ func (s *server) FetchInternalRemote(ctx context.Context, req *gitalypb.FetchInt return nil, status.Errorf(codes.Internal, "FetchInternalRemote: remote default branch: %v", err) } - defaultBranch, err := ref.DefaultBranchName(ctx, req.Repository) + defaultBranch, err := ref.DefaultBranchName(ctx, s.gitCmdFactory, req.Repository) if err != nil { return nil, status.Errorf(codes.Internal, "FetchInternalRemote: default branch: %v", err) } if !bytes.Equal(defaultBranch, remoteDefaultBranch) { - if err := ref.SetDefaultBranchRef(ctx, req.Repository, string(remoteDefaultBranch), config.Config); err != nil { + if err := ref.SetDefaultBranchRef(ctx, s.gitCmdFactory, req.Repository, string(remoteDefaultBranch), config.Config); err != nil { return nil, status.Errorf(codes.Internal, "FetchInternalRemote: set default branch: %v", err) } } |