From e51b854b86a87209f417fc47b6d917b3bd0a5933 Mon Sep 17 00:00:00 2001 From: Javiera Tapia Date: Tue, 28 Nov 2023 19:18:44 -0300 Subject: [PATCH 1/2] Add done pktline when git clone --depth option is given --- internal/command/githttp/pull.go | 17 ++++++++++++----- internal/command/githttp/pull_test.go | 21 ++++++++++++++++++++- internal/gitlabnet/accessverifier/client.go | 15 ++++++++------- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/internal/command/githttp/pull.go b/internal/command/githttp/pull.go index 907b0766..6e6fa6c6 100644 --- a/internal/command/githttp/pull.go +++ b/internal/command/githttp/pull.go @@ -4,12 +4,13 @@ import ( "bytes" "context" "fmt" + "io" + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/readwriter" "gitlab.com/gitlab-org/gitlab-shell/v14/internal/config" "gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/accessverifier" "gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/git" "gitlab.com/gitlab-org/gitlab-shell/v14/internal/pktline" - "io" ) const pullService = "git-upload-pack" @@ -40,7 +41,7 @@ func (c *PullCommand) Execute(ctx context.Context) error { return err } - return c.requestUploadPack(ctx, client) + return c.requestUploadPack(ctx, client, data.GeoProxyFetchDirectToPrimaryWithOptions) } func (c *PullCommand) requestInfoRefs(ctx context.Context, client *git.Client) error { @@ -63,9 +64,9 @@ func (c *PullCommand) requestInfoRefs(ctx context.Context, client *git.Client) e return err } -func (c *PullCommand) requestUploadPack(ctx context.Context, client *git.Client) error { +func (c *PullCommand) requestUploadPack(ctx context.Context, client *git.Client, geoProxyFetchDirectToPrimaryWithOptions bool) error { pipeReader, pipeWriter := io.Pipe() - go c.readFromStdin(pipeWriter) + go c.readFromStdin(pipeWriter, geoProxyFetchDirectToPrimaryWithOptions) response, err := client.UploadPack(ctx, pipeReader) if err != nil { @@ -78,7 +79,7 @@ func (c *PullCommand) requestUploadPack(ctx context.Context, client *git.Client) return err } -func (c *PullCommand) readFromStdin(pw *io.PipeWriter) { +func (c *PullCommand) readFromStdin(pw *io.PipeWriter, geoProxyFetchDirectToPrimaryWithOptions bool) { scanner := pktline.NewScanner(c.ReadWriter.In) for scanner.Scan() { @@ -90,6 +91,12 @@ func (c *PullCommand) readFromStdin(pw *io.PipeWriter) { break } + if pktline.IsFlush(line) && geoProxyFetchDirectToPrimaryWithOptions { + pw.Write([]byte(scanner.Text() + "0009done\n")) + + break + } + pw.Write(line) } diff --git a/internal/command/githttp/pull_test.go b/internal/command/githttp/pull_test.go index fdf23932..02ea3552 100644 --- a/internal/command/githttp/pull_test.go +++ b/internal/command/githttp/pull_test.go @@ -31,7 +31,26 @@ func TestPullExecute(t *testing.T) { ReadWriter: &readwriter.ReadWriter{Out: output, In: input}, Response: &accessverifier.Response{ Payload: accessverifier.CustomPayload{ - Data: accessverifier.CustomPayloadData{PrimaryRepo: url}, + Data: accessverifier.CustomPayloadData{PrimaryRepo: url, GeoProxyFetchDirectToPrimaryWithOptions: false}, + }, + }, + } + + require.NoError(t, cmd.Execute(context.Background())) + require.Equal(t, infoRefsWithoutPrefix, output.String()) +} + +func TestPullExecuteWithDepth(t *testing.T) { + url := setupPull(t, http.StatusOK) + output := &bytes.Buffer{} + input := strings.NewReader(strings.Replace(cloneResponse, "00000009done", "0000", 1)) + + cmd := &PullCommand{ + Config: &config.Config{GitlabUrl: url}, + ReadWriter: &readwriter.ReadWriter{Out: output, In: input}, + Response: &accessverifier.Response{ + Payload: accessverifier.CustomPayload{ + Data: accessverifier.CustomPayloadData{PrimaryRepo: url, GeoProxyFetchDirectToPrimaryWithOptions: true}, }, }, } diff --git a/internal/gitlabnet/accessverifier/client.go b/internal/gitlabnet/accessverifier/client.go index d566f07c..2cd14b88 100644 --- a/internal/gitlabnet/accessverifier/client.go +++ b/internal/gitlabnet/accessverifier/client.go @@ -43,13 +43,14 @@ type Gitaly struct { } type CustomPayloadData struct { - ApiEndpoints []string `json:"api_endpoints"` - Username string `json:"gl_username"` - PrimaryRepo string `json:"primary_repo"` - UserId string `json:"gl_id,omitempty"` - RequestHeaders map[string]string `json:"request_headers"` - GeoProxyDirectToPrimary bool `json:"geo_proxy_direct_to_primary"` - GeoProxyFetchDirectToPrimary bool `json:"geo_proxy_fetch_direct_to_primary"` + ApiEndpoints []string `json:"api_endpoints"` + Username string `json:"gl_username"` + PrimaryRepo string `json:"primary_repo"` + UserId string `json:"gl_id,omitempty"` + RequestHeaders map[string]string `json:"request_headers"` + GeoProxyDirectToPrimary bool `json:"geo_proxy_direct_to_primary"` + GeoProxyFetchDirectToPrimary bool `json:"geo_proxy_fetch_direct_to_primary"` + GeoProxyFetchDirectToPrimaryWithOptions bool `json:"geo_proxy_fetch_direct_to_primary_with_options"` } type CustomPayload struct { -- GitLab From 7244bb9e956cf6a7ba6dca559247b90a2780d8b3 Mon Sep 17 00:00:00 2001 From: Javiera Tapia Date: Sun, 10 Dec 2023 22:29:36 -0300 Subject: [PATCH 2/2] Apply maintainer suggestions --- internal/command/githttp/pull.go | 8 +++----- internal/command/githttp/pull_test.go | 9 ++++----- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/internal/command/githttp/pull.go b/internal/command/githttp/pull.go index 6e6fa6c6..f31f7fc5 100644 --- a/internal/command/githttp/pull.go +++ b/internal/command/githttp/pull.go @@ -85,19 +85,17 @@ func (c *PullCommand) readFromStdin(pw *io.PipeWriter, geoProxyFetchDirectToPrim for scanner.Scan() { line := scanner.Bytes() - if pktline.IsDone(line) { - pw.Write(line) + pw.Write(line) + if pktline.IsDone(line) { break } if pktline.IsFlush(line) && geoProxyFetchDirectToPrimaryWithOptions { - pw.Write([]byte(scanner.Text() + "0009done\n")) + pw.Write(pktline.PktDone()) break } - - pw.Write(line) } pw.Close() diff --git a/internal/command/githttp/pull_test.go b/internal/command/githttp/pull_test.go index 02ea3552..b3584d26 100644 --- a/internal/command/githttp/pull_test.go +++ b/internal/command/githttp/pull_test.go @@ -18,20 +18,19 @@ import ( var cloneResponse = `0090want 11d731b83788cd556abea7b465c6bee52d89923c multi_ack_detailed side-band-64k thin-pack ofs-delta deepen-since deepen-not agent=git/2.41.0 0032want e56497bb5f03a90a51293fc6d516788730953899 -00000009done ` func TestPullExecute(t *testing.T) { url := setupPull(t, http.StatusOK) output := &bytes.Buffer{} - input := strings.NewReader(cloneResponse) + input := strings.NewReader(cloneResponse + "00000009done\n") cmd := &PullCommand{ Config: &config.Config{GitlabUrl: url}, ReadWriter: &readwriter.ReadWriter{Out: output, In: input}, Response: &accessverifier.Response{ Payload: accessverifier.CustomPayload{ - Data: accessverifier.CustomPayloadData{PrimaryRepo: url, GeoProxyFetchDirectToPrimaryWithOptions: false}, + Data: accessverifier.CustomPayloadData{PrimaryRepo: url}, }, }, } @@ -43,7 +42,7 @@ func TestPullExecute(t *testing.T) { func TestPullExecuteWithDepth(t *testing.T) { url := setupPull(t, http.StatusOK) output := &bytes.Buffer{} - input := strings.NewReader(strings.Replace(cloneResponse, "00000009done", "0000", 1)) + input := strings.NewReader(cloneResponse + "0000\n") cmd := &PullCommand{ Config: &config.Config{GitlabUrl: url}, @@ -113,7 +112,7 @@ func TestPullExecuteWithFailedInfoRefs(t *testing.T) { func TestExecuteWithFailedUploadPack(t *testing.T) { url := setupPull(t, http.StatusForbidden) output := &bytes.Buffer{} - input := strings.NewReader(cloneResponse) + input := strings.NewReader(cloneResponse + "00000009done\n") cmd := &PullCommand{ Config: &config.Config{GitlabUrl: url}, -- GitLab