Skip to content

Commit 07e6d60

Browse files
authored
Merge pull request cli#2991 from cli/repo-create-prompt-change
Repo create tweaks
2 parents f93674b + 9e63199 commit 07e6d60

File tree

2 files changed

+86
-53
lines changed

2 files changed

+86
-53
lines changed

pkg/cmd/repo/create/create.go

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func createRun(opts *CreateOptions) error {
132132
isNameAnArg := false
133133
isDescEmpty := opts.Description == ""
134134
isVisibilityPassed := false
135+
inLocalRepo := projectDirErr == nil
135136

136137
if opts.Name != "" {
137138
isNameAnArg = true
@@ -201,6 +202,7 @@ func createRun(opts *CreateOptions) error {
201202
repoToCreate = ghrepo.New("", opts.Name)
202203
}
203204

205+
var templateRepoMainBranch string
204206
// Find template repo ID
205207
if opts.Template != "" {
206208
httpClient, err := opts.HttpClient()
@@ -230,6 +232,7 @@ func createRun(opts *CreateOptions) error {
230232
}
231233

232234
opts.Template = repo.ID
235+
templateRepoMainBranch = repo.DefaultBranchRef.Name
233236
}
234237

235238
input := repoCreateInput{
@@ -250,7 +253,7 @@ func createRun(opts *CreateOptions) error {
250253

251254
createLocalDirectory := opts.ConfirmSubmit
252255
if !opts.ConfirmSubmit {
253-
opts.ConfirmSubmit, err = confirmSubmission(input.Name, input.OwnerID, projectDirErr)
256+
opts.ConfirmSubmit, err = confirmSubmission(input.Name, input.OwnerID, inLocalRepo)
254257
if err != nil {
255258
return err
256259
}
@@ -284,7 +287,7 @@ func createRun(opts *CreateOptions) error {
284287
}
285288
remoteURL := ghrepo.FormatRemoteURL(repo, protocol)
286289

287-
if projectDirErr == nil {
290+
if inLocalRepo {
288291
_, err = git.AddRemote("origin", remoteURL)
289292
if err != nil {
290293
return err
@@ -295,40 +298,26 @@ func createRun(opts *CreateOptions) error {
295298
} else {
296299
if opts.IO.CanPrompt() {
297300
if !createLocalDirectory {
298-
err := prompt.Confirm(fmt.Sprintf("Create a local project directory for %s?", ghrepo.FullName(repo)), &createLocalDirectory)
301+
err := prompt.Confirm(fmt.Sprintf(`Create a local project directory for "%s"?`, ghrepo.FullName(repo)), &createLocalDirectory)
299302
if err != nil {
300303
return err
301304
}
302305
}
303306
}
304307
if createLocalDirectory {
305308
path := repo.Name
306-
307-
gitInit, err := git.GitCommand("init", path)
308-
if err != nil {
309-
return err
310-
}
311-
isTTY := opts.IO.IsStdoutTTY()
312-
if isTTY {
313-
gitInit.Stdout = stdout
314-
}
315-
gitInit.Stderr = stderr
316-
err = run.PrepareCmd(gitInit).Run()
317-
if err != nil {
318-
return err
319-
}
320-
gitRemoteAdd, err := git.GitCommand("-C", path, "remote", "add", "origin", remoteURL)
321-
if err != nil {
322-
return err
309+
checkoutBranch := ""
310+
if opts.Template != "" {
311+
// NOTE: we cannot read `defaultBranchRef` from the newly created repository as it will
312+
// be null at this time. Instead, we assume that the main branch name of the new
313+
// repository will be the same as that of the template repository.
314+
checkoutBranch = templateRepoMainBranch
323315
}
324-
gitRemoteAdd.Stdout = stdout
325-
gitRemoteAdd.Stderr = stderr
326-
err = run.PrepareCmd(gitRemoteAdd).Run()
327-
if err != nil {
316+
if err := localInit(opts.IO, remoteURL, path, checkoutBranch); err != nil {
328317
return err
329318
}
330319
if isTTY {
331-
fmt.Fprintf(stderr, "%s Initialized repository in './%s/'\n", cs.SuccessIcon(), path)
320+
fmt.Fprintf(stderr, "%s Initialized repository in \"%s\"\n", cs.SuccessIcon(), path)
332321
}
333322
}
334323
}
@@ -339,6 +328,56 @@ func createRun(opts *CreateOptions) error {
339328
return nil
340329
}
341330

331+
func localInit(io *iostreams.IOStreams, remoteURL, path, checkoutBranch string) error {
332+
gitInit, err := git.GitCommand("init", path)
333+
if err != nil {
334+
return err
335+
}
336+
isTTY := io.IsStdoutTTY()
337+
if isTTY {
338+
gitInit.Stdout = io.Out
339+
}
340+
gitInit.Stderr = io.ErrOut
341+
err = run.PrepareCmd(gitInit).Run()
342+
if err != nil {
343+
return err
344+
}
345+
346+
gitRemoteAdd, err := git.GitCommand("-C", path, "remote", "add", "origin", remoteURL)
347+
if err != nil {
348+
return err
349+
}
350+
gitRemoteAdd.Stdout = io.Out
351+
gitRemoteAdd.Stderr = io.ErrOut
352+
err = run.PrepareCmd(gitRemoteAdd).Run()
353+
if err != nil {
354+
return err
355+
}
356+
357+
if checkoutBranch == "" {
358+
return nil
359+
}
360+
361+
gitFetch, err := git.GitCommand("-C", path, "fetch", "origin", fmt.Sprintf("+refs/heads/%[1]s:refs/remotes/origin/%[1]s", checkoutBranch))
362+
if err != nil {
363+
return err
364+
}
365+
gitFetch.Stdout = io.Out
366+
gitFetch.Stderr = io.ErrOut
367+
err = run.PrepareCmd(gitFetch).Run()
368+
if err != nil {
369+
return err
370+
}
371+
372+
gitCheckout, err := git.GitCommand("-C", path, "checkout", checkoutBranch)
373+
if err != nil {
374+
return err
375+
}
376+
gitCheckout.Stdout = io.Out
377+
gitCheckout.Stderr = io.ErrOut
378+
return run.PrepareCmd(gitCheckout).Run()
379+
}
380+
342381
func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName string) (string, string, string, error) {
343382
qs := []*survey.Question{}
344383

@@ -388,16 +427,18 @@ func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName s
388427
return answers.RepoName, answers.RepoDescription, strings.ToUpper(answers.RepoVisibility), nil
389428
}
390429

391-
func confirmSubmission(repoName string, repoOwner string, projectDirErr error) (bool, error) {
430+
func confirmSubmission(repoName string, repoOwner string, inLocalRepo bool) (bool, error) {
392431
qs := []*survey.Question{}
393432

394433
promptString := ""
395-
if projectDirErr == nil {
396-
promptString = "This will add remote origin to your current directory. Continue? "
397-
} else if repoOwner != "" {
398-
promptString = fmt.Sprintf("This will create '%s/%s' in your current directory. Continue? ", repoOwner, repoName)
434+
if inLocalRepo {
435+
promptString = `This will add an "origin" git remote to your local repository. Continue?`
399436
} else {
400-
promptString = fmt.Sprintf("This will create '%s' in your current directory. Continue? ", repoName)
437+
targetRepo := repoName
438+
if repoOwner != "" {
439+
targetRepo = fmt.Sprintf("%s/%s", repoOwner, repoName)
440+
}
441+
promptString = fmt.Sprintf(`This will create the "%s" repository on GitHub. Continue?`, targetRepo)
401442
}
402443

403444
confirmSubmitQuestion := &survey.Question{

pkg/cmd/repo/create/create_test.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"testing"
99

10+
"github.com/MakeNowJust/heredoc"
1011
"github.com/cli/cli/internal/config"
1112
"github.com/cli/cli/internal/run"
1213
"github.com/cli/cli/pkg/cmdutil"
@@ -345,6 +346,7 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
345346

346347
func TestRepoCreate_template(t *testing.T) {
347348
reg := &httpmock.Registry{}
349+
defer reg.Verify(t)
348350
reg.Register(
349351
httpmock.GraphQL(`mutation CloneTemplateRepository\b`),
350352
httpmock.StringResponse(`
@@ -370,32 +372,26 @@ func TestRepoCreate_template(t *testing.T) {
370372
cs, cmdTeardown := run.Stub()
371373
defer cmdTeardown(t)
372374

373-
cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "")
374-
cs.Register(`git rev-parse --show-toplevel`, 0, "")
375+
cs.Register(`git rev-parse --show-toplevel`, 1, "")
376+
cs.Register(`git init REPO`, 0, "")
377+
cs.Register(`git -C REPO remote add`, 0, "")
378+
cs.Register(`git -C REPO fetch origin \+refs/heads/main:refs/remotes/origin/main`, 0, "")
379+
cs.Register(`git -C REPO checkout main`, 0, "")
375380

376-
as, surveyTearDown := prompt.InitAskStubber()
381+
_, surveyTearDown := prompt.InitAskStubber()
377382
defer surveyTearDown()
378383

379-
as.Stub([]*prompt.QuestionStub{
380-
{
381-
Name: "repoVisibility",
382-
Value: "PRIVATE",
383-
},
384-
})
385-
as.Stub([]*prompt.QuestionStub{
386-
{
387-
Name: "confirmSubmit",
388-
Value: true,
389-
},
390-
})
391-
392-
output, err := runCommand(httpClient, "REPO --template='OWNER/REPO'", true)
384+
output, err := runCommand(httpClient, "REPO -y --private --template='OWNER/REPO'", true)
393385
if err != nil {
394386
t.Errorf("error running command `repo create`: %v", err)
387+
return
395388
}
396389

397390
assert.Equal(t, "", output.String())
398-
assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr())
391+
assert.Equal(t, heredoc.Doc(`
392+
✓ Created repository OWNER/REPO on GitHub
393+
✓ Initialized repository in "REPO"
394+
`), output.Stderr())
399395

400396
var reqBody struct {
401397
Query string
@@ -404,10 +400,6 @@ func TestRepoCreate_template(t *testing.T) {
404400
}
405401
}
406402

407-
if len(reg.Requests) != 3 {
408-
t.Fatalf("expected 3 HTTP requests, got %d", len(reg.Requests))
409-
}
410-
411403
bodyBytes, _ := ioutil.ReadAll(reg.Requests[2].Body)
412404
_ = json.Unmarshal(bodyBytes, &reqBody)
413405
if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" {

0 commit comments

Comments
 (0)