8000 Remote delete by probablycorey · Pull Request #982 · cli/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remote delete #982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 26, 2020
Merged

Remote delete #982

merged 18 commits into from
May 26, 2020

Conversation

probablycorey
Copy link
Contributor
@probablycorey probablycorey commented May 21, 2020

This PR needs #899 to be merged first.

Remember when I didn't explain how pr merge didn't have the ability to delete the remote branch 😬. Well this PR is my atonement. I also added some more error checking so if the local branch doesn't exist it won't try and delete it.

Here are examples of it in use.

When you are on the branch being deleted
image

Handle when the local branch doesn’t exist. I opted for no noticeable output here.
image

Remote branch doesn’t exist (it was already deleted). The error here is pretty ugly, but maybe this is ok since it is so rare?
image

I was worried about the situation where you wouldn't have access to delete the remote branch, but I couldn't get any example of that working.

@probablycorey probablycorey requested review from mislav and vilmibm May 21, 2020 01:19
@probablycorey probablycorey self-assigned this May 21, 2020
func DoesLocalBranchExist(branch string) bool {
configCmd := GitCommand("rev-parse", "--verify", branch)
_, err := run.PrepareCmd(configCmd).Output()
return err == nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the assumption here that any error means the branch doesn't exist. I figured that will be more stable in the future than doing a string match on the error message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a fair assumption! rev-parse --verify is sufficiently low-level

command/pr.go Outdated
Comment on lines 587 to 592
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of messy, but I haven't figured out a good way to do simple string interpolated stuff in go.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until we switch to some kind of template-based output with embedded if/else control flow, I think this is perfectly fine! 👍

command/pr.go Outdated
Comment on lines 587 to 592
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until we switch to some kind of template-based output with embedded if/else control flow, I think this is perfectly fine! 👍

git/git.go Outdated
@@ -209,6 +209,18 @@ func DeleteLocalBranch(branch string) error {
return err
}

func DeleteRemoteBranch(branch string) error {
configCmd := GitCommand("push", "origin", "--delete", branch)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This works when the --repo flag was not used, but I think it might be wiser to delete branches via API operations if we were to support scripting via --repo OWNER/REPO. In the latter mode, the local git repo will likely not correspond to the repo chosen via --repo argument.

  2. If we're keeping this approach, then it might be prudent to delete the refs/heads/<branch> ref instead of just <branch> to disambiguate when the branch name matches an existing tag (so we don't delete a tag by accident).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry for the extra work but I agree that deleting via the API is safer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. It now deletes the remote branch using the API. If the repo flag is set it will not delete the local branch, only the remote branch.

func DoesLocalBranchExist(branch string) bool {
configCmd := GitCommand("rev-parse", "--verify", branch)
_, err := run.PrepareCmd(configCmd).Output()
return err == nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a fair assumption! rev-parse --verify is sufficiently low-level

git/git.go Outdated
@@ -209,6 +209,18 @@ func DeleteLocalBranch(branch string) error {
return err
}

func DeleteRemoteBranch(branch string) error {
configCmd := GitCommand("push", "origin", "--delete", branch)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry for the extra work but I agree that deleting via the API is safer.

@probablycorey
Copy link
Contributor Author

All right! This now uses the github API to delete the ref. It also does some work to make sure only remote branches are deleted when the repo flag is set

@probablycorey probablycorey mentioned this pull request May 23, 2020
Copy link
Contributor
@mislav mislav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, but note that the head branch of some PRs might be coming from forks!

@@ -209,6 +209,12 @@ func DeleteLocalBranch(branch string) error {
return err
}

func HasLocalBranch(branch string) bool {
configCmd := GitCommand("rev-parse", "--verify", "refs/heads/"+branch)
_, err := run.PrepareCmd(configCmd).Output()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip: to run a command without being interested in its output, you can use Run() instead of Output().

command/pr.go Outdated
}
}

err = git.DeleteLocalBranch(pr.HeadRefName)
err = api.BranchDeleteRemote(apiClient, baseRepo, pr.HeadRefName)
Copy link
Contributor
@mislav mislav May 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. If this PR comes from a fork, we probably don't want to attempt to delete a same-named branch in the base repo since that won't be the same branch. We most likely don't have the privileges to delete a branch on someone's fork, but we could optionally check for the off chance that we have write privileges on the PR head repo.

  2. We could change the order of the logic: delete the remote branch before attempting to delete the local branch. That way, if something went wrong with deleting the local branch (e.g. the local working copy was dirty and git checkout failed), at least we've already cleaned up the remote branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR comes from a fork, we probably don't want to attempt to delete a same-named branch in the base repo since that won't be the same branch.

👍 I've added a cross-repo PR check to prevent branch deletion on PRs that come from forks.

We could change the order of the logic: delete the remote branch before attempting to delete the local branch.

I'm going to skip this for now because I want to reduce the number of changes before we release this.

Base automatically changed from merge-interactive-merge to master May 26, 2020 15:34
@billygriffin billygriffin requested a review from vilmibm May 26, 2020 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0