diff options
author | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-12-02 15:35:24 +0300 |
---|---|---|
committer | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-12-02 15:35:24 +0300 |
commit | fd1cdcabd258341e7a6a480b941316fa995400c1 (patch) | |
tree | faaac00ccb4000bb9304516413abd89391c099f8 | |
parent | e2148d83e953bef15bb274a4c4f3327356296877 (diff) | |
parent | fe2c75b7bb4225f8bad21c143b226bb04c6815cb (diff) |
Merge branch 'fix_keep_around_ref' into 'master'
Allow internal fetches to see all hidden references
See merge request gitlab-org/gitaly!1640
-rw-r--r-- | changelogs/unreleased/fix_keep_around_ref.yml | 5 | ||||
-rw-r--r-- | cmd/gitaly-ssh/upload_pack.go | 9 | ||||
-rw-r--r-- | cmd/gitaly-ssh/upload_pack_test.go | 104 |
3 files changed, 118 insertions, 0 deletions
diff --git a/changelogs/unreleased/fix_keep_around_ref.yml b/changelogs/unreleased/fix_keep_around_ref.yml new file mode 100644 index 000000000..3b8fab1a7 --- /dev/null +++ b/changelogs/unreleased/fix_keep_around_ref.yml @@ -0,0 +1,5 @@ +--- +title: Allow internal fetches to see all hidden references +merge_request: 1640 +author: +type: fixed diff --git a/cmd/gitaly-ssh/upload_pack.go b/cmd/gitaly-ssh/upload_pack.go index c6fbf4b89..954e47a6b 100644 --- a/cmd/gitaly-ssh/upload_pack.go +++ b/cmd/gitaly-ssh/upload_pack.go @@ -11,12 +11,21 @@ import ( "google.golang.org/grpc" ) +const ( + // GitConfigShowAllRefs is a git-config option. + // We have to use a negative transfer.hideRefs since this is the only way + // to undo an already set parameter: https://www.spinics.net/lists/git/msg256772.html + GitConfigShowAllRefs = "transfer.hideRefs=!refs" +) + func uploadPack(ctx context.Context, conn *grpc.ClientConn, req string) (int32, error) { var request gitalypb.SSHUploadPackRequest if err := jsonpb.UnmarshalString(req, &request); err != nil { return 0, fmt.Errorf("json unmarshal: %v", err) } + request.GitConfigOptions = append([]string{GitConfigShowAllRefs}, request.GitConfigOptions...) + ctx, cancel := context.WithCancel(ctx) defer cancel() diff --git a/cmd/gitaly-ssh/upload_pack_test.go b/cmd/gitaly-ssh/upload_pack_test.go new file mode 100644 index 000000000..80c12e9e9 --- /dev/null +++ b/cmd/gitaly-ssh/upload_pack_test.go @@ -0,0 +1,104 @@ +package main + +import ( + "bytes" + "fmt" + "os" + "testing" + + "github.com/golang/protobuf/jsonpb" + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/internal/git" + "gitlab.com/gitlab-org/gitaly/internal/git/updateref" + "gitlab.com/gitlab-org/gitaly/internal/server" + "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" +) + +const keepAroundNamespace = "refs/keep-around" + +func TestVisibilityOfHiddenRefs(t *testing.T) { + ctx, cancel := testhelper.Context() + defer cancel() + + testRepo, testRepoPath, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + // Create a keep-around ref + existingSha := "1e292f8fedd741b75372e19097c76d327140c312" + keepAroundRef := fmt.Sprintf("%s/%s", keepAroundNamespace, existingSha) + + updater, err := updateref.New(ctx, testRepo) + + require.NoError(t, err) + require.NoError(t, updater.Create(keepAroundRef, existingSha)) + require.NoError(t, updater.Wait()) + + testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "config", "transfer.hideRefs", keepAroundNamespace) + + output := testhelper.MustRunCommand(t, nil, "git", "ls-remote", testRepoPath, keepAroundNamespace) + require.Empty(t, output, "there should be no keep-around refs in normal ls-remote output") + + socketPath := testhelper.GetTemporaryGitalySocketFileName() + + unixServer, _ := runServer(t, server.NewInsecure, "unix", socketPath) + defer unixServer.Stop() + + wd, err := os.Getwd() + require.NoError(t, err) + + tests := []struct { + name string + GitConfigOptions []string + HiddenRefFound bool + }{ + { + name: "With no custom GitConfigOptions passed", + GitConfigOptions: []string{}, + HiddenRefFound: true, + }, + { + name: "With custom GitConfigOptions passed", + GitConfigOptions: []string{fmt.Sprintf("transfer.hideRefs=%s", keepAroundRef)}, + HiddenRefFound: false, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + pbMarshaler := &jsonpb.Marshaler{} + payload, err := pbMarshaler.MarshalToString(&gitalypb.SSHUploadPackRequest{ + Repository: testRepo, + GitConfigOptions: test.GitConfigOptions, + }) + + require.NoError(t, err) + + env := []string{ + fmt.Sprintf("GITALY_PAYLOAD=%s", payload), + fmt.Sprintf("GITALY_ADDRESS=unix:%s", socketPath), + fmt.Sprintf("GITALY_WD=%s", wd), + fmt.Sprintf("PATH=.:%s", os.Getenv("PATH")), + fmt.Sprintf("GIT_SSH_COMMAND=%s upload-pack", gitalySSHPath), + } + + stdout := &bytes.Buffer{} + cmd, err := git.SafeBareCmd(ctx, nil, stdout, nil, env, nil, git.SubCmd{ + Name: "ls-remote", + Args: []string{ + fmt.Sprintf("%s:%s", "git@localhost", testRepoPath), + keepAroundRef, + }, + }) + require.NoError(t, err) + + err = cmd.Wait() + require.NoError(t, err) + + if test.HiddenRefFound { + require.Equal(t, fmt.Sprintf("%s\t%s\n", existingSha, keepAroundRef), stdout.String()) + } else { + require.NotEqual(t, fmt.Sprintf("%s\t%s\n", existingSha, keepAroundRef), stdout.String()) + } + }) + } +} |