From 0fcae469b351ba50c26dea057ef6a80112dc542f Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Sun, 10 Jul 2022 21:59:22 +0200 Subject: [PATCH] Allow passing a base branch that doesn't have version info --- cherry_picker/cherry_picker.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker.py index bb4b848..8f83315 100755 --- a/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker.py @@ -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): @@ -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 @@ -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 """ try: - return tuple( - map( - int, - re.match(r"^.*(?P\d+(\.\d+)+).*$", branch) - .groupdict()["version"] - .split("."), - ) - ) + match = re.match(r"^.*(?P\d+(\.\d+)+).*$", branch) + 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 + return (1, branch) + else: + # Use '0' to sort version numbers *before* regular branch names + return (0, *(-int(x) for x in raw_version)) def get_current_branch():