From 3085c39a730782dcc4292e76ba24b0449167152b Mon Sep 17 00:00:00 2001 From: Heath Stewart Date: Wed, 29 Mar 2023 07:43:21 -0700 Subject: [PATCH] Specify branch to force exit code --- pkg/cmd/repo/create/create.go | 18 +++++++++++++++--- pkg/cmd/repo/create/create_test.go | 13 ++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/repo/create/create.go b/pkg/cmd/repo/create/create.go index 105c7f130..0aae16aac 100644 --- a/pkg/cmd/repo/create/create.go +++ b/pkg/cmd/repo/create/create.go @@ -1,9 +1,11 @@ package create import ( + "bytes" "context" "errors" "fmt" + "io" "net/http" "os/exec" "path/filepath" @@ -335,6 +337,7 @@ func createFromScratch(opts *CreateOptions) error { InitReadme: opts.AddReadme, } + var templateRepoMainBranch string if opts.Template != "" { var templateRepo ghrepo.Interface apiClient := api.NewClientFromHTTP(httpClient) @@ -358,6 +361,7 @@ func createFromScratch(opts *CreateOptions) error { } input.TemplateRepositoryID = repo.ID + templateRepoMainBranch = repo.DefaultBranchRef.Name } repo, err := repoCreate(httpClient, repoToCreate.RepoHost(), input) @@ -397,7 +401,7 @@ func createFromScratch(opts *CreateOptions) error { if err := localInit(opts.GitClient, remoteURL, repo.RepoName()); err != nil { return err } - } else if err := cloneWithRetry(opts, remoteURL); err != nil { + } else if err := cloneWithRetry(opts, remoteURL, templateRepoMainBranch); err != nil { return err } } @@ -563,19 +567,27 @@ func createFromLocal(opts *CreateOptions) error { return nil } -func cloneWithRetry(opts *CreateOptions, remoteURL string) error { +func cloneWithRetry(opts *CreateOptions, remoteURL, branch string) error { // Allow injecting alternative BackOff in tests. if opts.BackOff == nil { opts.BackOff = backoff.NewConstantBackOff(3 * time.Second) } + var args []string + if branch != "" { + args = append(args, "--branch", branch) + } + ctx := context.Background() return backoff.Retry(func() error { - _, err := opts.GitClient.Clone(ctx, remoteURL, []string{}) + stderr := &bytes.Buffer{} + _, err := opts.GitClient.Clone(ctx, remoteURL, args, git.WithStderr(stderr)) var execError errWithExitCode if errors.As(err, &execError) && execError.ExitCode() == 128 { return err + } else { + io.Copy(opts.IO.ErrOut, stderr) } return backoff.Permanent(err) diff --git a/pkg/cmd/repo/create/create_test.go b/pkg/cmd/repo/create/create_test.go index d773b06f5..5b5fd72bb 100644 --- a/pkg/cmd/repo/create/create_test.go +++ b/pkg/cmd/repo/create/create_test.go @@ -6,6 +6,7 @@ import ( "net/http" "testing" + "github.com/cenkalti/backoff/v4" "github.com/cli/cli/v2/git" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/prompter" @@ -602,13 +603,14 @@ func Test_createRun(t *testing.T) { wantStdout: "https://github.com/OWNER/REPO\n", }, { - name: "noninteractive create from template", + name: "noninteractive create from template with retry", opts: &CreateOptions{ Interactive: false, Name: "REPO", Visibility: "PRIVATE", Clone: true, Template: "mytemplate", + BackOff: &backoff.ZeroBackOff{}, }, tty: false, httpStubs: func(reg *httpmock.Registry) { @@ -621,7 +623,10 @@ func Test_createRun(t *testing.T) { httpmock.GraphQLQuery(`{ "data": { "repository": { - "id": "REPOID" + "id": "REPOID", + "defaultBranchRef": { + "name": "main" + } } } }`, func(s string, m map[string]interface{}) { @@ -651,7 +656,9 @@ func Test_createRun(t *testing.T) { })) }, execStubs: func(cs *run.CommandStubber) { - cs.Register(`git clone https://github.com/OWNER/REPO`, 0, "") + // fatal: Remote branch main not found in upstream origin + cs.Register(`git clone --branch main https://github.com/OWNER/REPO`, 128, "") + cs.Register(`git clone --branch main https://github.com/OWNER/REPO`, 0, "") }, wantStdout: "https://github.com/OWNER/REPO\n", },