Skip to content
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

Fix exit with bad state when backport branch already exists #39

Merged
merged 7 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@


class BranchCheckoutException(Exception):
pass
def __init__(self, branch_name):
self.branch_name = branch_name
super().__init__(f"Error checking out the branch {branch_name!r}.")


class CherryPickException(Exception):
Expand Down Expand Up @@ -214,12 +216,10 @@ def checkout_branch(self, branch_name, *, create_branch=False):
self.run_cmd(cmd)
except subprocess.CalledProcessError as err:
click.echo(
f"Error checking out the branch {branch_name}."
f"Error checking out the branch {checked_out_branch!r}."
)
click.echo(err.output)
raise BranchCheckoutException(
f"Error checking out the branch {branch_name}."
)
raise BranchCheckoutException(checked_out_branch)

def get_commit_message(self, commit_sha):
"""
Expand Down Expand Up @@ -425,7 +425,13 @@ def backport(self):
click.echo(f"Now backporting '{self.commit_sha1}' into '{maint_branch}'")

cherry_pick_branch = self.get_cherry_pick_branch(maint_branch)
self.checkout_branch(maint_branch, create_branch=True)
try:
self.checkout_branch(maint_branch, create_branch=True)
except BranchCheckoutException:
self.checkout_default_branch()
reset_stored_config_ref()
reset_state()
raise
commit_message = ""
try:
self.cherry_pick()
Expand Down
33 changes: 33 additions & 0 deletions cherry_picker/test_cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .cherry_picker import (
DEFAULT_CONFIG,
WORKFLOW_STATES,
BranchCheckoutException,
CherryPicker,
CherryPickException,
InvalidRepoException,
Expand Down Expand Up @@ -825,6 +826,38 @@ def test_backport_cherry_pick_crash_ignored(
assert get_state() == WORKFLOW_STATES.UNSET


def test_backport_cherry_pick_branch_already_exists(
tmp_git_repo_dir, git_branch, git_add, git_commit, git_checkout
):
cherry_pick_target_branches = ("3.8",)
pr_remote = "origin"
test_file = "some.file"
tmp_git_repo_dir.join(test_file).write("some contents")
git_branch(cherry_pick_target_branches[0])
git_branch(
f"{pr_remote}/{cherry_pick_target_branches[0]}", cherry_pick_target_branches[0]
)
git_add(test_file)
git_commit("Add a test file")
scm_revision = get_sha1_from("HEAD")

with mock.patch("cherry_picker.cherry_picker.validate_sha", return_value=True):
cherry_picker = CherryPicker(
pr_remote, scm_revision, cherry_pick_target_branches
)

backport_branch_name = cherry_picker.get_cherry_pick_branch(cherry_pick_target_branches[0])
git_branch(backport_branch_name)

with mock.patch.object(cherry_picker, "fetch_upstream"), pytest.raises(
BranchCheckoutException
) as exc_info:
cherry_picker.backport()

assert exc_info.value.branch_name == backport_branch_name
assert get_state() == WORKFLOW_STATES.UNSET


def test_backport_success(
tmp_git_repo_dir, git_branch, git_add, git_commit, git_checkout
):
Expand Down