Skip to content

Commit a6161cb

Browse files
committed
pr close skips deleting local branch if not in a git repo
1 parent cf4b73f commit a6161cb

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

pkg/cmd/pr/close/close.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,34 +90,36 @@ func closeRun(opts *CloseOptions) error {
9090
if opts.DeleteBranch {
9191
branchSwitchString := ""
9292
apiClient := api.NewClientFromHTTP(httpClient)
93+
localBranchExists := git.HasLocalBranch(pr.HeadRefName)
9394

9495
if opts.DeleteLocalBranch {
95-
currentBranch, err := opts.Branch()
96-
if err != nil {
97-
return err
98-
}
99-
100-
var branchToSwitchTo string
101-
if currentBranch == pr.HeadRefName {
102-
branchToSwitchTo, err = api.RepoDefaultBranch(apiClient, baseRepo)
96+
if localBranchExists {
97+
currentBranch, err := opts.Branch()
10398
if err != nil {
10499
return err
105100
}
106-
err = git.CheckoutBranch(branchToSwitchTo)
107-
if err != nil {
108-
return err
101+
102+
var branchToSwitchTo string
103+
if currentBranch == pr.HeadRefName {
104+
branchToSwitchTo, err = api.RepoDefaultBranch(apiClient, baseRepo)
105+
if err != nil {
106+
return err
107+
}
108+
err = git.CheckoutBranch(branchToSwitchTo)
109+
if err != nil {
110+
return err
111+
}
109112
}
110-
}
111113

112-
localBranchExists := git.HasLocalBranch(pr.HeadRefName)
113-
if localBranchExists {
114114
if err := git.DeleteLocalBranch(pr.HeadRefName); err != nil {
115115
return fmt.Errorf("failed to delete local branch %s: %w", cs.Cyan(pr.HeadRefName), err)
116116
}
117-
}
118117

119-
if branchToSwitchTo != "" {
120-
branchSwitchString = fmt.Sprintf(" and switched to branch %s", cs.Cyan(branchToSwitchTo))
118+
if branchToSwitchTo != "" {
119+
branchSwitchString = fmt.Sprintf(" and switched to branch %s", cs.Cyan(branchToSwitchTo))
120+
}
121+
} else {
122+
fmt.Fprintf(opts.IO.ErrOut, "%s Skipped deleting the local branch since current directory is not a git repository \n", cs.WarningIcon())
121123
}
122124
}
123125

pkg/cmd/pr/close/close_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,37 @@ func TestPrClose_deleteBranch_sameBranch(t *testing.T) {
230230
✓ Deleted branch trunk and switched to branch main
231231
`), output.Stderr())
232232
}
233+
234+
func TestPrClose_deleteBranch_notInGitRepo(t *testing.T) {
235+
http := &httpmock.Registry{}
236+
defer http.Verify(t)
237+
238+
baseRepo, pr := stubPR("OWNER/REPO:main", "OWNER/REPO:trunk")
239+
pr.Title = "The title of the PR"
240+
shared.RunCommandFinder("96", pr, baseRepo)
241+
242+
http.Register(
243+
httpmock.GraphQL(`mutation PullRequestClose\b`),
244+
httpmock.GraphQLMutation(`{"id": "THE-ID"}`,
245+
func(inputs map[string]interface{}) {
246+
assert.Equal(t, inputs["pullRequestId"], "THE-ID")
247+
}),
248+
)
249+
http.Register(
250+
httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/trunk"),
251+
httpmock.StringResponse(`{}`))
252+
253+
cs, cmdTeardown := run.Stub()
254+
defer cmdTeardown(t)
255+
256+
cs.Register(`git rev-parse --verify refs/heads/trunk`, 128, "could not determine current branch: fatal: not a git repository (or any of the parent directories): .git")
257+
258+
output, err := runCommand(http, true, `96 --delete-branch`)
259+
assert.NoError(t, err)
260+
assert.Equal(t, "", output.String())
261+
assert.Equal(t, heredoc.Doc(`
262+
✓ Closed pull request #96 (The title of the PR)
263+
! Skipped deleting the local branch since current directory is not a git repository
264+
✓ Deleted branch trunk
265+
`), output.Stderr())
266+
}

0 commit comments

Comments
 (0)