Specify branch to force exit code

This commit is contained in:
Heath Stewart 2023-03-29 07:43:21 -07:00
parent 36436cb8b8
commit 3085c39a73
No known key found for this signature in database
2 changed files with 25 additions and 6 deletions

View file

@ -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)

View file

@ -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",
},