Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion command/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Error() != "git: not on any branch" {
if err != nil && repoOverride == "" && !errors.Is(err, git.ErrNotOnAnyBranch) {
return fmt.Errorf("could not query for pull request for current branch: %w", err)
}

Expand Down
32 changes: 32 additions & 0 deletions command/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion context/blank_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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
}
Expand Down
5 changes: 4 additions & 1 deletion git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"github.com/cli/cli/internal/run"
)

// ErrNotOnAnyBranch indicates that the users is in detached HEAD state
var ErrNotOnAnyBranch = errors.New("git: not on any branch")

// Ref represents a git commit reference
type Ref struct {
Hash string
Expand Down Expand Up @@ -64,7 +67,7 @@ func CurrentBranch() (string, error) {
if errors.As(err, &cmdErr) {
if cmdErr.Stderr.Len() == 0 {
// Detached head
return "", errors.New("git: not on any branch")
return "", ErrNotOnAnyBranch
}
}

Expand Down
5 changes: 2 additions & 3 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ func Test_CurrentBranch_detached_head(t *testing.T) {
if err == nil {
t.Errorf("expected an error")
}
expectedError := "git: not on any branch"
if err.Error() != expectedError {
t.Errorf("got unexpected error: %s instead of %s", err.Error(), expectedError)
if err != ErrNotOnAnyBranch {
t.Errorf("got unexpected error: %s instead of %s", err, ErrNotOnAnyBranch)
}
if len(cs.Calls) != 1 {
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
Expand Down