Skip to content

Commit bb1005b

Browse files
committed
Fixed gh pr checkout on detached HEAD
1 parent e3f21a5 commit bb1005b

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

pkg/cmd/pr/checkout/checkout.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package checkout
33
import (
44
"errors"
55
"fmt"
6+
"net/http"
7+
"os"
8+
"os/exec"
9+
"strings"
10+
611
"github.com/cli/cli/api"
712
"github.com/cli/cli/context"
813
"github.com/cli/cli/git"
@@ -13,10 +18,6 @@ import (
1318
"github.com/cli/cli/pkg/cmdutil"
1419
"github.com/cli/cli/pkg/iostreams"
1520
"github.com/spf13/cobra"
16-
"net/http"
17-
"os"
18-
"os/exec"
19-
"strings"
2021
)
2122

2223
type CheckoutOptions struct {
@@ -70,11 +71,6 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr
7071
}
7172

7273
func checkoutRun(opts *CheckoutOptions) error {
73-
currentBranch, err := opts.Branch()
74-
if err != nil {
75-
return err
76-
}
77-
7874
remotes, err := opts.Remotes()
7975
if err != nil {
8076
return err
@@ -133,6 +129,7 @@ func checkoutRun(opts *CheckoutOptions) error {
133129
}
134130
} else {
135131
// no git remote for PR head
132+
currentBranch, _ := opts.Branch()
136133

137134
defaultBranchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
138135
if err != nil {

pkg/cmd/pr/checkout/checkout_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,46 @@ func TestPRCheckout_differentRepo_existingBranch(t *testing.T) {
448448
eq(t, strings.Join(ranCommands[1], " "), "git checkout feature")
449449
}
450450

451+
func TestPRCheckout_detachedHead(t *testing.T) {
452+
http := &httpmock.Registry{}
453+
defer http.Verify(t)
454+
455+
http.Register(httpmock.GraphQL(`query PullRequestByNumber\b`), httpmock.StringResponse(`
456+
{ "data": { "repository": { "pullRequest": {
457+
"number": 123,
458+
"headRefName": "feature",
459+
"headRepositoryOwner": {
460+
"login": "hubot"
461+
},
462+
"headRepository": {
463+
"name": "REPO"
464+
},
465+
"isCrossRepository": true,
466+
"maintainerCanModify": true
467+
} } } }
468+
`))
469+
470+
ranCommands := [][]string{}
471+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
472+
switch strings.Join(cmd.Args, " ") {
473+
case "git config branch.feature.merge":
474+
return &test.OutputStub{Out: []byte("refs/heads/feature\n")}
475+
default:
476+
ranCommands = append(ranCommands, cmd.Args)
477+
return &test.OutputStub{}
478+
}
479+
})
480+
defer restoreCmd()
481+
482+
output, err := runCommand(http, nil, "", `123`)
483+
eq(t, err, nil)
484+
eq(t, output.String(), "")
485+
486+
eq(t, len(ranCommands), 2)
487+
eq(t, strings.Join(ranCommands[0], " "), "git fetch origin refs/pull/123/head:feature")
488+
eq(t, strings.Join(ranCommands[1], " "), "git checkout feature")
489+
}
490+
451491
func TestPRCheckout_differentRepo_currentBranch(t *testing.T) {
452492
http := &httpmock.Registry{}
453493
defer http.Verify(t)

0 commit comments

Comments
 (0)