diff --git a/command/pr.go b/command/pr.go index f1c1eb101..10a41246d 100644 --- a/command/pr.go +++ b/command/pr.go @@ -116,7 +116,7 @@ func prStatus(cmd *cobra.Command, args []string) error { repoOverride, _ := cmd.Flags().GetString("repo") currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx, baseRepo) - if err != nil && repoOverride == "" && err != git.ErrNotOnAnyBranch { + if err != nil && repoOverride == "" && !errors.Is(err, git.ErrNotOnAnyBranch) { return fmt.Errorf("could not query for pull request for current branch: %w", err) } diff --git a/command/pr_test.go b/command/pr_test.go index a11c60820..ee90a66b9 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -299,6 +299,38 @@ Requesting a code review from you } } +func TestPRStatus_detachedHead(t *testing.T) { + initBlankContext("", "OWNER/REPO", "") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + http.StubResponse(200, bytes.NewBufferString(` + { "data": {} } + `)) + + output, err := RunCommand("pr status") + if err != nil { + t.Errorf("error running command `pr status`: %v", err) + } + + expected := ` +Relevant pull requests in OWNER/REPO + +Current branch + There is no current branch + +Created by you + You have no open pull requests + +Requesting a code review from you + You have no pull requests to review + +` + if output.String() != expected { + t.Errorf("expected %q, got %q", expected, output.String()) + } +} + func TestPRList(t *testing.T) { initBlankContext("", "OWNER/REPO", "master") http := initFakeHTTP() diff --git a/context/blank_context.go b/context/blank_context.go index 3ea657abe..7bb5282ec 100644 --- a/context/blank_context.go +++ b/context/blank_context.go @@ -18,6 +18,7 @@ func NewBlank() *blankContext { type blankContext struct { authToken string branch string + branchErr error baseRepo ghrepo.Interface remotes Remotes } @@ -40,7 +41,7 @@ func (c *blankContext) SetAuthToken(t string) { func (c *blankContext) Branch() (string, error) { if c.branch == "" { - return "", fmt.Errorf("branch was not initialized") + return "", fmt.Errorf("branch was not initialized: %w", git.ErrNotOnAnyBranch) } return c.branch, nil } diff --git a/context/context.go b/context/context.go index 4f1aa10ba..236f9e722 100644 --- a/context/context.go +++ b/context/context.go @@ -207,11 +207,7 @@ func (c *fsContext) Branch() (string, error) { } currentBranch, err := git.CurrentBranch() - switch err { - case nil: - case git.ErrNotOnAnyBranch: - return "", err - default: + if err != nil { return "", fmt.Errorf("could not determine current branch: %w", err) }