diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-06-18 10:57:35 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-06-18 10:57:35 +0300 |
commit | b619cf6f55e989ecf24528c5a9aa9a0536d787c4 (patch) | |
tree | de1486d4064f7dfb69082ee445813105c7a66e2b | |
parent | ceb84269c65974da7353c35c3abc96ea7830c85a (diff) | |
parent | ae52e8f136169b153b41014d713aea883bf92263 (diff) |
Merge branch 'fetch_remote_default_branch' into 'master'
Set default branch to match remote for FetchInternalRemote
Closes #2837
See merge request gitlab-org/gitaly!2265
-rw-r--r-- | changelogs/unreleased/fetch_remote_default_branch.yml | 5 | ||||
-rw-r--r-- | internal/service/ref/refs.go | 12 | ||||
-rw-r--r-- | internal/service/ref/refs_test.go | 37 | ||||
-rw-r--r-- | internal/service/remote/fetch_internal_remote.go | 18 | ||||
-rw-r--r-- | internal/service/remote/fetch_internal_remote_test.go | 9 |
5 files changed, 78 insertions, 3 deletions
diff --git a/changelogs/unreleased/fetch_remote_default_branch.yml b/changelogs/unreleased/fetch_remote_default_branch.yml new file mode 100644 index 000000000..3da75c753 --- /dev/null +++ b/changelogs/unreleased/fetch_remote_default_branch.yml @@ -0,0 +1,5 @@ +--- +title: Set default branch to match remote for FetchInternalRemote +merge_request: 2265 +author: +type: fixed diff --git a/internal/service/ref/refs.go b/internal/service/ref/refs.go index 0618a7ac3..c4a8e831f 100644 --- a/internal/service/ref/refs.go +++ b/internal/service/ref/refs.go @@ -206,6 +206,18 @@ func _headReference(ctx context.Context, repo *gitalypb.Repository) ([]byte, err return headRef, nil } +// SetDefaultBranchRef overwrites the default branch ref for the repository +func SetDefaultBranchRef(ctx context.Context, repo *gitalypb.Repository, ref string) error { + cmd, err := git.SafeCmd(ctx, repo, nil, git.SubCmd{ + Name: "symbolic-ref", + Args: []string{"HEAD", ref}, + }) + if err != nil { + return err + } + return cmd.Wait() +} + // 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) diff --git a/internal/service/ref/refs_test.go b/internal/service/ref/refs_test.go index 476f98285..a026fbd41 100644 --- a/internal/service/ref/refs_test.go +++ b/internal/service/ref/refs_test.go @@ -301,6 +301,43 @@ func TestHeadReferenceWithNonExistingHead(t *testing.T) { } } +func TestSetDefaultBranchRef(t *testing.T) { + testCases := []struct { + desc string + ref string + expectedRef string + }{ + { + desc: "update the branch ref", + ref: "refs/heads/feature", + expectedRef: "refs/heads/feature", + }, + { + desc: "unknown ref", + ref: "refs/heads/non_existent_ref", + expectedRef: "refs/heads/master", + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + testRepo, _, cleanupFn := testhelper.NewTestRepo(t) + defer cleanupFn() + + err := SetDefaultBranchRef(ctx, testRepo, tc.ref) + require.NoError(t, err) + + newRef, err := DefaultBranchName(ctx, testRepo) + require.NoError(t, err) + + require.Equal(t, tc.expectedRef, string(newRef)) + }) + } +} + func TestDefaultBranchName(t *testing.T) { // We are going to override these functions during this test. Restore them after we're done defer func() { diff --git a/internal/service/remote/fetch_internal_remote.go b/internal/service/remote/fetch_internal_remote.go index 6365e3cec..08863b24e 100644 --- a/internal/service/remote/fetch_internal_remote.go +++ b/internal/service/remote/fetch_internal_remote.go @@ -1,6 +1,7 @@ package remote import ( + "bytes" "context" "fmt" @@ -8,6 +9,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/gitalyssh" "gitlab.com/gitlab-org/gitaly/internal/helper" + "gitlab.com/gitlab-org/gitaly/internal/service/ref" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -51,6 +53,22 @@ func (s *server) FetchInternalRemote(ctx context.Context, req *gitalypb.FetchInt return &gitalypb.FetchInternalRemoteResponse{Result: false}, nil } + remoteDefaultBranch, err := ref.DefaultBranchName(ctx, req.RemoteRepository) + if err != nil { + return nil, status.Errorf(codes.Internal, "FetchInternalRemote: remote default branch: %v", err) + } + + defaultBranch, err := ref.DefaultBranchName(ctx, 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)); err != nil { + return nil, status.Errorf(codes.Internal, "FetchInternalRemote: set default branch: %v", err) + } + } + return &gitalypb.FetchInternalRemoteResponse{Result: true}, nil } diff --git a/internal/service/remote/fetch_internal_remote_test.go b/internal/service/remote/fetch_internal_remote_test.go index 3dc4079ee..a5b42c455 100644 --- a/internal/service/remote/fetch_internal_remote_test.go +++ b/internal/service/remote/fetch_internal_remote_test.go @@ -26,6 +26,8 @@ func TestSuccessfulFetchInternalRemote(t *testing.T) { remoteRepo, remoteRepoPath, remoteCleanupFn := testhelper.NewTestRepo(t) defer remoteCleanupFn() + testhelper.MustRunCommand(t, nil, "git", "-C", remoteRepoPath, "symbolic-ref", "HEAD", "refs/heads/feature") + repo, repoPath, cleanupFn := testhelper.InitBareRepo(t) defer cleanupFn() @@ -44,9 +46,10 @@ func TestSuccessfulFetchInternalRemote(t *testing.T) { require.NoError(t, err) require.True(t, c.GetResult()) - remoteRefs := testhelper.GetRepositoryRefs(t, remoteRepoPath) - refs := testhelper.GetRepositoryRefs(t, repoPath) - require.Equal(t, remoteRefs, refs) + require.Equal(t, + string(testhelper.MustRunCommand(t, nil, "git", "-C", remoteRepoPath, "show-ref", "--head")), + string(testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "show-ref", "--head")), + ) } func TestFailedFetchInternalRemote(t *testing.T) { |