Merge pull request #1155 from metalogical/FIX-detached-head

fix regression in support for detached HEAD state
This commit is contained in:
Mislav Marohnić 2020-06-24 17:01:22 +02:00 committed by GitHub
commit ab903bdfc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 6 deletions

View file

@ -131,7 +131,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)
}

View file

@ -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()

View file

@ -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
}

View file

@ -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
@ -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
}
}

View file

@ -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))