X Tutup
Skip to content

Commit ab903bd

Browse files
authored
Merge pull request cli#1155 from metalogical/FIX-detached-head
fix regression in support for detached HEAD state
2 parents 9c80490 + fffa393 commit ab903bd

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

command/pr.go

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

134-
if err != nil && repoOverride == "" && err.Error() != "git: not on any branch" {
134+
if err != nil && repoOverride == "" && !errors.Is(err, git.ErrNotOnAnyBranch) {
135135
return fmt.Errorf("could not query for pull request for current branch: %w", err)
136136
}
137137

command/pr_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,38 @@ Requesting a code review from you
299299
}
300300
}
301301

302+
func TestPRStatus_detachedHead(t *testing.T) {
303+
initBlankContext("", "OWNER/REPO", "")
304+
http := initFakeHTTP()
305+
http.StubRepoResponse("OWNER", "REPO")
306+
307+
http.StubResponse(200, bytes.NewBufferString(`
308+
{ "data": {} }
309+
`))
310+
311+
output, err := RunCommand("pr status")
312+
if err != nil {
313+
t.Errorf("error running command `pr status`: %v", err)
314+
}
315+
316+
expected := `
317+
Relevant pull requests in OWNER/REPO
318+
319+
Current branch
320+
There is no current branch
321+
322+
Created by you
323+
You have no open pull requests
324+
325+
Requesting a code review from you
326+
You have no pull requests to review
327+
328+
`
329+
if output.String() != expected {
330+
t.Errorf("expected %q, got %q", expected, output.String())
331+
}
332+
}
333+
302334
func TestPRList(t *testing.T) {
303335
initBlankContext("", "OWNER/REPO", "master")
304336
http := initFakeHTTP()

context/blank_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (c *blankContext) SetAuthToken(t string) {
4040

4141
func (c *blankContext) Branch() (string, error) {
4242
if c.branch == "" {
43-
return "", fmt.Errorf("branch was not initialized")
43+
return "", fmt.Errorf("branch was not initialized: %w", git.ErrNotOnAnyBranch)
4444
}
4545
return c.branch, nil
4646
}

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)
X Tutup