Skip to content

Commit

Permalink
Add remote deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
probablycorey committed May 20, 2020
1 parent 46a1e3c commit a303dab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
21 changes: 17 additions & 4 deletions command/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {
prCmd.AddCommand(prCloseCmd)
prCmd.AddCommand(prReopenCmd)
prCmd.AddCommand(prMergeCmd)
prMergeCmd.Flags().BoolP("delete-branch", "d", true, "Delete the local branch after merge")
prMergeCmd.Flags().BoolP("delete-branch", "d", true, "Delete the local and remote branch after merge")
prMergeCmd.Flags().BoolP("merge", "m", false, "Merge the commits with the base branch")
prMergeCmd.Flags().BoolP("rebase", "r", false, "Rebase the commits onto the base branch")
prMergeCmd.Flags().BoolP("squash", "s", false, "Squash the commits into one commit and merge it into the base branch")
Expand Down Expand Up @@ -560,7 +560,9 @@ func prMerge(cmd *cobra.Command, args []string) error {
return err
}

var branchToSwitchTo string
if currentBranch == pr.HeadRefName {
branchToSwitchTo = repo.DefaultBranchRef.Name
err = git.CheckoutBranch(repo.DefaultBranchRef.Name)
if err != nil {
return err
Expand All @@ -569,10 +571,21 @@ func prMerge(cmd *cobra.Command, args []string) error {

err = git.DeleteLocalBranch(pr.HeadRefName)
if err != nil {
fmt.Fprintf(colorableErr(cmd), "%s Could not deleted local branch %s: %s\n", utils.Red("!"), utils.Cyan(pr.HeadRefName), err)
fmt.Errorf("failed to delete local branch %s: %w", utils.Cyan(pr.HeadRefName), err)
return err
}
fmt.Fprintf(colorableOut(cmd), "%s Deleted local branch %s\n", utils.Red("✔"), utils.Cyan(pr.HeadRefName))

err = git.DeleteRemoteBranch(pr.HeadRefName)
if err != nil {
fmt.Errorf("failed to delete remote branch %s: %w", utils.Cyan(pr.HeadRefName), err)
return err
}

branchSwitchString := ""
if branchToSwitchTo != "" {
branchSwitchString = fmt.Sprintf("and switched to branch %s", utils.Cyan(branchToSwitchTo))
}
fmt.Fprintf(colorableOut(cmd), "%s Deleted branch %s %s\n", utils.Red("✔"), utils.Cyan(pr.HeadRefName), branchSwitchString)
}

return nil
Expand All @@ -591,7 +604,7 @@ func prInteractiveMerge() (api.PullRequestMergeMethod, bool, error) {
deleteBranchQuestion := &survey.Question{
Name: "deleteBranch",
Prompt: &survey.Confirm{
Message: "Delete the branch locally?",
Message: "Delete the branch locally and on GitHub?",
Default: true,
},
}
Expand Down
6 changes: 6 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ func DeleteLocalBranch(branch string) error {
return err
}

func DeleteRemoteBranch(branch string) error {
configCmd := GitCommand("push", "origin", "--delete", branch)
_, err := run.PrepareCmd(configCmd).Output()
return err
}

func CheckoutBranch(branch string) error {
configCmd := GitCommand("checkout", branch)
_, err := run.PrepareCmd(configCmd).Output()
Expand Down

0 comments on commit a303dab

Please sign in to comment.