Skip to content

Commit 7555a4c

Browse files
author
Nate Smith
authored
Merge pull request cli#696 from cli/pr-status-no-commits
fix branch detection on empty repo
2 parents 1fb0eef + c966f0d commit 7555a4c

File tree

16 files changed

+210
-182
lines changed

16 files changed

+210
-182
lines changed

command/issue_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"github.com/cli/cli/internal/run"
1314
"github.com/cli/cli/test"
14-
"github.com/cli/cli/utils"
1515
)
1616

1717
func TestIssueStatus(t *testing.T) {
@@ -229,7 +229,7 @@ func TestIssueView_web(t *testing.T) {
229229
`))
230230

231231
var seenCmd *exec.Cmd
232-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
232+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
233233
seenCmd = cmd
234234
return &test.OutputStub{}
235235
})
@@ -263,7 +263,7 @@ func TestIssueView_web_numberArgWithHash(t *testing.T) {
263263
`))
264264

265265
var seenCmd *exec.Cmd
266-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
266+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
267267
seenCmd = cmd
268268
return &test.OutputStub{}
269269
})
@@ -372,7 +372,7 @@ func TestIssueView_web_notFound(t *testing.T) {
372372
`))
373373

374374
var seenCmd *exec.Cmd
375-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
375+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
376376
seenCmd = cmd
377377
return &test.OutputStub{}
378378
})
@@ -419,7 +419,7 @@ func TestIssueView_web_urlArg(t *testing.T) {
419419
`))
420420

421421
var seenCmd *exec.Cmd
422-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
422+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
423423
seenCmd = cmd
424424
return &test.OutputStub{}
425425
})
@@ -504,7 +504,7 @@ func TestIssueCreate_web(t *testing.T) {
504504
http.StubRepoResponse("OWNER", "REPO")
505505

506506
var seenCmd *exec.Cmd
507-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
507+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
508508
seenCmd = cmd
509509
return &test.OutputStub{}
510510
})
@@ -530,7 +530,7 @@ func TestIssueCreate_webTitleBody(t *testing.T) {
530530
http.StubRepoResponse("OWNER", "REPO")
531531

532532
var seenCmd *exec.Cmd
533-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
533+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
534534
seenCmd = cmd
535535
return &test.OutputStub{}
536536
})

command/pr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func prStatus(cmd *cobra.Command, args []string) error {
8484
repoOverride, _ := cmd.Flags().GetString("repo")
8585
currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx, baseRepo)
8686
if err != nil && repoOverride == "" && err.Error() != "git: not on any branch" {
87-
return err
87+
return fmt.Errorf("could not query for pull request for current branch: %w", err)
8888
}
8989

9090
prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser)

command/pr_checkout.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"os"
77
"os/exec"
88

9+
"github.com/spf13/cobra"
10+
911
"github.com/cli/cli/git"
1012
"github.com/cli/cli/internal/ghrepo"
11-
"github.com/cli/cli/utils"
12-
"github.com/spf13/cobra"
13+
"github.com/cli/cli/internal/run"
1314
)
1415

1516
func prCheckout(cmd *cobra.Command, args []string) error {
@@ -109,7 +110,7 @@ func prCheckout(cmd *cobra.Command, args []string) error {
109110
cmd := exec.Command(args[0], args[1:]...)
110111
cmd.Stdout = os.Stdout
111112
cmd.Stderr = os.Stderr
112-
if err := utils.PrepareCmd(cmd).Run(); err != nil {
113+
if err := run.PrepareCmd(cmd).Run(); err != nil {
113114
return err
114115
}
115116
}

command/pr_checkout_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"testing"
1010

1111
"github.com/cli/cli/context"
12+
"github.com/cli/cli/internal/run"
1213
"github.com/cli/cli/test"
13-
"github.com/cli/cli/utils"
1414
)
1515

1616
func TestPRCheckout_sameRepo(t *testing.T) {
@@ -44,7 +44,7 @@ func TestPRCheckout_sameRepo(t *testing.T) {
4444
`))
4545

4646
ranCommands := [][]string{}
47-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
47+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
4848
switch strings.Join(cmd.Args, " ") {
4949
case "git show-ref --verify -- refs/heads/feature":
5050
return &errorStub{"exit status: 1"}
@@ -96,7 +96,7 @@ func TestPRCheckout_urlArg(t *testing.T) {
9696
`))
9797

9898
ranCommands := [][]string{}
99-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
99+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
100100
switch strings.Join(cmd.Args, " ") {
101101
case "git show-ref --verify -- refs/heads/feature":
102102
return &errorStub{"exit status: 1"}
@@ -145,7 +145,7 @@ func TestPRCheckout_urlArg_differentBase(t *testing.T) {
145145
`))
146146

147147
ranCommands := [][]string{}
148-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
148+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
149149
switch strings.Join(cmd.Args, " ") {
150150
case "git show-ref --verify -- refs/heads/feature":
151151
return &errorStub{"exit status: 1"}
@@ -208,7 +208,7 @@ func TestPRCheckout_branchArg(t *testing.T) {
208208
`))
209209

210210
ranCommands := [][]string{}
211-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
211+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
212212
switch strings.Join(cmd.Args, " ") {
213213
case "git show-ref --verify -- refs/heads/feature":
214214
return &errorStub{"exit status: 1"}
@@ -258,7 +258,7 @@ func TestPRCheckout_existingBranch(t *testing.T) {
258258
`))
259259

260260
ranCommands := [][]string{}
261-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
261+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
262262
switch strings.Join(cmd.Args, " ") {
263263
case "git show-ref --verify -- refs/heads/feature":
264264
return &test.OutputStub{}
@@ -311,7 +311,7 @@ func TestPRCheckout_differentRepo_remoteExists(t *testing.T) {
311311
`))
312312

313313
ranCommands := [][]string{}
314-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
314+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
315315
switch strings.Join(cmd.Args, " ") {
316316
case "git show-ref --verify -- refs/heads/feature":
317317
return &errorStub{"exit status: 1"}
@@ -364,7 +364,7 @@ func TestPRCheckout_differentRepo(t *testing.T) {
364364
`))
365365

366366
ranCommands := [][]string{}
367-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
367+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
368368
switch strings.Join(cmd.Args, " ") {
369369
case "git config branch.feature.merge":
370370
return &errorStub{"exit status 1"}
@@ -417,10 +417,10 @@ func TestPRCheckout_differentRepo_existingBranch(t *testing.T) {
417417
`))
418418

419419
ranCommands := [][]string{}
420-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
420+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
421421
switch strings.Join(cmd.Args, " ") {
422422
case "git config branch.feature.merge":
423-
return &test.OutputStub{[]byte("refs/heads/feature\n")}
423+
return &test.OutputStub{[]byte("refs/heads/feature\n"), nil}
424424
default:
425425
ranCommands = append(ranCommands, cmd.Args)
426426
return &test.OutputStub{}
@@ -468,10 +468,10 @@ func TestPRCheckout_differentRepo_currentBranch(t *testing.T) {
468468
`))
469469

470470
ranCommands := [][]string{}
471-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
471+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
472472
switch strings.Join(cmd.Args, " ") {
473473
case "git config branch.feature.merge":
474-
return &test.OutputStub{[]byte("refs/heads/feature\n")}
474+
return &test.OutputStub{[]byte("refs/heads/feature\n"), nil}
475475
default:
476476
ranCommands = append(ranCommands, cmd.Args)
477477
return &test.OutputStub{}
@@ -519,7 +519,7 @@ func TestPRCheckout_maintainerCanModify(t *testing.T) {
519519
`))
520520

521521
ranCommands := [][]string{}
522-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
522+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
523523
switch strings.Join(cmd.Args, " ") {
524524
case "git config branch.feature.merge":
525525
return &errorStub{"exit status 1"}

command/pr_create_test.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/cli/cli/context"
1111
"github.com/cli/cli/git"
12+
"github.com/cli/cli/test"
1213
)
1314

1415
func TestPRCreate(t *testing.T) {
@@ -29,7 +30,7 @@ func TestPRCreate(t *testing.T) {
2930
} } } }
3031
`))
3132

32-
cs, cmdTeardown := initCmdStubber()
33+
cs, cmdTeardown := test.InitCmdStubber()
3334
defer cmdTeardown()
3435

3536
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -80,7 +81,7 @@ func TestPRCreate_alreadyExists(t *testing.T) {
8081
] } } } }
8182
`))
8283

83-
cs, cmdTeardown := initCmdStubber()
84+
cs, cmdTeardown := test.InitCmdStubber()
8485
defer cmdTeardown()
8586

8687
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -114,7 +115,7 @@ func TestPRCreate_alreadyExistsDifferentBase(t *testing.T) {
114115
`))
115116
http.StubResponse(200, bytes.NewBufferString("{}"))
116117

117-
cs, cmdTeardown := initCmdStubber()
118+
cs, cmdTeardown := test.InitCmdStubber()
118119
defer cmdTeardown()
119120

120121
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -138,7 +139,7 @@ func TestPRCreate_web(t *testing.T) {
138139
] } } } }
139140
`))
140141

141-
cs, cmdTeardown := initCmdStubber()
142+
cs, cmdTeardown := test.InitCmdStubber()
142143
defer cmdTeardown()
143144

144145
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -179,7 +180,7 @@ func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
179180
} } } }
180181
`))
181182

182-
cs, cmdTeardown := initCmdStubber()
183+
cs, cmdTeardown := test.InitCmdStubber()
183184
defer cmdTeardown()
184185

185186
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -251,7 +252,7 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) {
251252
} } } }
252253
`))
253254

254-
cs, cmdTeardown := initCmdStubber()
255+
cs, cmdTeardown := test.InitCmdStubber()
255256
defer cmdTeardown()
256257

257258
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -306,7 +307,7 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) {
306307
} } } }
307308
`))
308309

309-
cs, cmdTeardown := initCmdStubber()
310+
cs, cmdTeardown := test.InitCmdStubber()
310311
defer cmdTeardown()
311312

312313
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -382,7 +383,7 @@ func TestPRCreate_survey_defaults_monocommit(t *testing.T) {
382383
} } } }
383384
`))
384385

385-
cs, cmdTeardown := initCmdStubber()
386+
cs, cmdTeardown := test.InitCmdStubber()
386387
defer cmdTeardown()
387388

388389
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -459,7 +460,7 @@ func TestPRCreate_survey_autofill(t *testing.T) {
459460
} } } }
460461
`))
461462

462-
cs, cmdTeardown := initCmdStubber()
463+
cs, cmdTeardown := test.InitCmdStubber()
463464
defer cmdTeardown()
464465

465466
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -504,7 +505,7 @@ func TestPRCreate_defaults_error_autofill(t *testing.T) {
504505
http := initFakeHTTP()
505506
http.StubRepoResponse("OWNER", "REPO")
506507

507-
cs, cmdTeardown := initCmdStubber()
508+
cs, cmdTeardown := test.InitCmdStubber()
508509
defer cmdTeardown()
509510

510511
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -522,7 +523,7 @@ func TestPRCreate_defaults_error_web(t *testing.T) {
522523
http := initFakeHTTP()
523524
http.StubRepoResponse("OWNER", "REPO")
524525

525-
cs, cmdTeardown := initCmdStubber()
526+
cs, cmdTeardown := test.InitCmdStubber()
526527
defer cmdTeardown()
527528

528529
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -549,7 +550,7 @@ func TestPRCreate_defaults_error_interactive(t *testing.T) {
549550
} } } }
550551
`))
551552

552-
cs, cmdTeardown := initCmdStubber()
553+
cs, cmdTeardown := test.InitCmdStubber()
553554
defer cmdTeardown()
554555

555556
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
@@ -588,7 +589,7 @@ func TestPRCreate_defaults_error_interactive(t *testing.T) {
588589
}
589590

590591
func Test_determineTrackingBranch_empty(t *testing.T) {
591-
cs, cmdTeardown := initCmdStubber()
592+
cs, cmdTeardown := test.InitCmdStubber()
592593
defer cmdTeardown()
593594

594595
remotes := context.Remotes{}
@@ -603,7 +604,7 @@ func Test_determineTrackingBranch_empty(t *testing.T) {
603604
}
604605

605606
func Test_determineTrackingBranch_noMatch(t *testing.T) {
606-
cs, cmdTeardown := initCmdStubber()
607+
cs, cmdTeardown := test.InitCmdStubber()
607608
defer cmdTeardown()
608609

609610
remotes := context.Remotes{
@@ -630,7 +631,7 @@ deadb00f refs/remotes/origin/feature`) // git show-ref --verify (ShowRefs)
630631
}
631632

632633
func Test_determineTrackingBranch_hasMatch(t *testing.T) {
633-
cs, cmdTeardown := initCmdStubber()
634+
cs, cmdTeardown := test.InitCmdStubber()
634635
defer cmdTeardown()
635636

636637
remotes := context.Remotes{
@@ -663,7 +664,7 @@ deadbeef refs/remotes/upstream/feature`) // git show-ref --verify (ShowRefs)
663664
}
664665

665666
func Test_determineTrackingBranch_respectTrackingConfig(t *testing.T) {
666-
cs, cmdTeardown := initCmdStubber()
667+
cs, cmdTeardown := test.InitCmdStubber()
667668
defer cmdTeardown()
668669

669670
remotes := context.Remotes{

0 commit comments

Comments
 (0)