Skip to content

Commit 904c541

Browse files
committed
Only delete local branch if it exists
1 parent a303dab commit 904c541

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

command/pr.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,13 @@ func prMerge(cmd *cobra.Command, args []string) error {
569569
}
570570
}
571571

572-
err = git.DeleteLocalBranch(pr.HeadRefName)
573-
if err != nil {
574-
fmt.Errorf("failed to delete local branch %s: %w", utils.Cyan(pr.HeadRefName), err)
575-
return err
572+
localBranchExists := git.DoesLocalBranchExist(pr.HeadRefName)
573+
if localBranchExists {
574+
err = git.DeleteLocalBranch(pr.HeadRefName)
575+
if err != nil {
576+
fmt.Errorf("failed to delete local branch %s: %w", utils.Cyan(pr.HeadRefName), err)
577+
return err
578+
}
576579
}
577580

578581
err = git.DeleteRemoteBranch(pr.HeadRefName)
@@ -583,9 +586,10 @@ func prMerge(cmd *cobra.Command, args []string) error {
583586

584587
branchSwitchString := ""
585588
if branchToSwitchTo != "" {
586-
branchSwitchString = fmt.Sprintf("and switched to branch %s", utils.Cyan(branchToSwitchTo))
589+
branchSwitchString = fmt.Sprintf(" and switched to branch %s", utils.Cyan(branchToSwitchTo))
587590
}
588-
fmt.Fprintf(colorableOut(cmd), "%s Deleted branch %s %s\n", utils.Red("✔"), utils.Cyan(pr.HeadRefName), branchSwitchString)
591+
592+
fmt.Fprintf(colorableOut(cmd), "%s Deleted branch %s%s\n", utils.Red("✔"), utils.Cyan(pr.HeadRefName), branchSwitchString)
589593
}
590594

591595
return nil

git/git.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ func DeleteRemoteBranch(branch string) error {
215215
return err
216216
}
217217

218+
func DoesLocalBranchExist(branch string) bool {
219+
configCmd := GitCommand("rev-parse", "--verify", branch)
220+
x, err := run.PrepareCmd(configCmd).Output()
221+
fmt.Printf("🌭 %+v|%+v\n", x, err != nil)
222+
return err == nil
223+
}
224+
218225
func CheckoutBranch(branch string) error {
219226
configCmd := GitCommand("checkout", branch)
220227
_, err := run.PrepareCmd(configCmd).Output()

0 commit comments

Comments
 (0)