fix regression in support for detached HEAD state

for gh pr status
This commit is contained in:
naman 2020-06-10 11:41:44 -07:00
parent 28f91cbed8
commit 3bb6983b35
4 changed files with 12 additions and 6 deletions

View file

@ -116,7 +116,7 @@ func prStatus(cmd *cobra.Command, args []string) error {
repoOverride, _ := cmd.Flags().GetString("repo") repoOverride, _ := cmd.Flags().GetString("repo")
currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx, baseRepo) currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx, baseRepo)
if err != nil && repoOverride == "" && err.Error() != "git: not on any branch" { if err != nil && repoOverride == "" && err != git.ErrNotOnAnyBranch {
return fmt.Errorf("could not query for pull request for current branch: %w", err) return fmt.Errorf("could not query for pull request for current branch: %w", err)
} }

View file

@ -207,7 +207,11 @@ func (c *fsContext) Branch() (string, error) {
} }
currentBranch, err := git.CurrentBranch() currentBranch, err := git.CurrentBranch()
if err != nil { switch err {
case nil:
case git.ErrNotOnAnyBranch:
return "", err
default:
return "", fmt.Errorf("could not determine current branch: %w", err) return "", fmt.Errorf("could not determine current branch: %w", err)
} }

View file

@ -13,6 +13,9 @@ import (
"github.com/cli/cli/internal/run" "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 // Ref represents a git commit reference
type Ref struct { type Ref struct {
Hash string Hash string
@ -64,7 +67,7 @@ func CurrentBranch() (string, error) {
if errors.As(err, &cmdErr) { if errors.As(err, &cmdErr) {
if cmdErr.Stderr.Len() == 0 { if cmdErr.Stderr.Len() == 0 {
// Detached head // 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 { if err == nil {
t.Errorf("expected an error") t.Errorf("expected an error")
} }
expectedError := "git: not on any branch" if err != ErrNotOnAnyBranch {
if err.Error() != expectedError { t.Errorf("got unexpected error: %s instead of %s", err, ErrNotOnAnyBranch)
t.Errorf("got unexpected error: %s instead of %s", err.Error(), expectedError)
} }
if len(cs.Calls) != 1 { if len(cs.Calls) != 1 {
t.Errorf("expected 1 git call, saw %d", len(cs.Calls)) t.Errorf("expected 1 git call, saw %d", len(cs.Calls))