Skip to content

Commit 3bb6983

Browse files
committed
fix regression in support for detached HEAD state
for gh pr status
1 parent 28f91cb commit 3bb6983

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

command/pr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func prStatus(cmd *cobra.Command, args []string) error {
116116
repoOverride, _ := cmd.Flags().GetString("repo")
117117
currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx, baseRepo)
118118

119-
if err != nil && repoOverride == "" && err.Error() != "git: not on any branch" {
119+
if err != nil && repoOverride == "" && err != git.ErrNotOnAnyBranch {
120120
return fmt.Errorf("could not query for pull request for current branch: %w", err)
121121
}
122122

context/context.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ func (c *fsContext) Branch() (string, error) {
207207
}
208208

209209
currentBranch, err := git.CurrentBranch()
210-
if err != nil {
210+
switch err {
211+
case nil:
212+
case git.ErrNotOnAnyBranch:
213+
return "", err
214+
default:
211215
return "", fmt.Errorf("could not determine current branch: %w", err)
212216
}
213217

git/git.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313
"github.com/cli/cli/internal/run"
1414
)
1515

16+
// ErrNotOnAnyBranch indicates that the users is in detached HEAD state
17+
var ErrNotOnAnyBranch = errors.New("git: not on any branch")
18+
1619
// Ref represents a git commit reference
1720
type Ref struct {
1821
Hash string
@@ -64,7 +67,7 @@ func CurrentBranch() (string, error) {
6467
if errors.As(err, &cmdErr) {
6568
if cmdErr.Stderr.Len() == 0 {
6669
// Detached head
67-
return "", errors.New("git: not on any branch")
70+
return "", ErrNotOnAnyBranch
6871
}
6972
}
7073

git/git_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ func Test_CurrentBranch_detached_head(t *testing.T) {
6767
if err == nil {
6868
t.Errorf("expected an error")
6969
}
70-
expectedError := "git: not on any branch"
71-
if err.Error() != expectedError {
72-
t.Errorf("got unexpected error: %s instead of %s", err.Error(), expectedError)
70+
if err != ErrNotOnAnyBranch {
71+
t.Errorf("got unexpected error: %s instead of %s", err, ErrNotOnAnyBranch)
7372
}
7473
if len(cs.Calls) != 1 {
7574
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))

0 commit comments

Comments
 (0)