Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 13 additions & 15 deletions cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def upstream(self):
@property
def sorted_branches(self):
"""Return the branches to cherry-pick to, sorted by version."""
return sorted(self.branches, reverse=True, key=version_from_branch)
return sorted(self.branches, key=version_sort_key)

@property
def username(self):
Expand Down Expand Up @@ -733,7 +733,7 @@ def get_base_branch(cherry_pick_branch):

# Subject the parsed base_branch to the same tests as when we generated it
# This throws a ValueError if the base_branch doesn't meet our requirements
version_from_branch(base_branch)
version_sort_key(base_branch)

return base_branch

Expand All @@ -753,23 +753,21 @@ def validate_sha(sha):
)


def version_from_branch(branch):
def version_sort_key(branch):
"""
return version information from a git branch name
return sort key based on version information from a git branch name
Comment thread
Jackenmen marked this conversation as resolved.
Outdated
"""
try:
return tuple(
map(
int,
re.match(r"^.*(?P<version>\d+(\.\d+)+).*$", branch)
.groupdict()["version"]
.split("."),
)
)
match = re.match(r"^.*(?P<version>\d+(\.\d+)+).*$", branch)
Comment thread
Jackenmen marked this conversation as resolved.
Outdated
raw_version = match.groupdict()["version"].split(".")
except AttributeError as attr_err:
raise ValueError(
f"Branch {branch} seems to not have a version in its name."
) from attr_err
if not branch:
raise ValueError("Branch name is an empty string.") from attr_err
# Use '0' to sort regular branch names *after* version numbers
Comment thread
Jackenmen marked this conversation as resolved.
Outdated
return (1, branch)
else:
# Use '0' to sort version numbers *before* regular branch names
Comment thread
Jackenmen marked this conversation as resolved.
Outdated
return (0, *(-int(x) for x in raw_version))


def get_current_branch():
Expand Down
17 changes: 7 additions & 10 deletions cherry_picker/test_cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_get_base_branch_which_has_dashes(subprocess_check_output):
[
"backport-22a594a", # Not enough fields
"prefix-22a594a-2.7", # Not the prefix we were expecting
"backport-22a594a-base", # No version info in the base branch
"backport-22a594a-", # No base branch
],
)
@mock.patch("subprocess.check_output")
Expand Down Expand Up @@ -194,6 +194,10 @@ def test_get_author_info_from_short_sha(subprocess_check_output):
["stable-3.1", "lts-2.7", "3.10-other", "smth3.6else"],
["3.10-other", "smth3.6else", "stable-3.1", "lts-2.7"],
),
(
["3.7", "3.10", "2.7", "foo", "stable", "branch"],
["3.10", "3.7", "2.7", "branch", "foo", "stable"],
),
],
)
@mock.patch("os.path.exists")
Expand All @@ -208,20 +212,13 @@ def test_sorted_branch(os_path_exists, config, input_branches, sorted_branches):
assert cp.sorted_branches == sorted_branches


@pytest.mark.parametrize(
"input_branches",
[
(["3.1", "2.7", "3.x10", "3.6", ""]),
(["stable-3.1", "lts-2.7", "3.10-other", "smth3.6else", "invalid"]),
],
)
@mock.patch("os.path.exists")
def test_invalid_branches(os_path_exists, config, input_branches):
def test_invalid_branch_empty_string(os_path_exists, config):
os_path_exists.return_value = True
cp = CherryPicker(
"origin",
"22a594a0047d7706537ff2ac676cdc0f1dcb329c",
input_branches,
["3.1", "2.7", "3.10", "3.6", ""],
config=config,
)
with pytest.raises(ValueError):
Comment thread
Jackenmen marked this conversation as resolved.
Outdated
Expand Down