Skip to content

Commit 73931c8

Browse files
committed
Really sort branches in the version module
To be able to properly sort branch versions, we need to handle all special cases for versions with a `None` minor, as we can simply compare `int`s to `None`. We consider `None` to be always bigger than any `int` and 2 `None` to be equal. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent a1f5353 commit 73931c8

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/frequenz/repo/config/version.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,12 @@ def branch_not_none(
131131
return branch[1] is not None
132132

133133
return dict(
134-
filter(
135-
branch_not_none,
136-
((branch, BranchVersion.parse(branch)) for branch in (branches or [])),
134+
sorted(
135+
filter(
136+
branch_not_none,
137+
((branch, BranchVersion.parse(branch)) for branch in (branches or [])),
138+
),
139+
key=lambda branch: branch[1],
137140
)
138141
)
139142

@@ -202,12 +205,26 @@ def __lt__(self, other: BranchVersion) -> bool:
202205
"""
203206
if not isinstance(other, BranchVersion):
204207
return NotImplemented
205-
minor = (
206-
other.minor + 1
207-
if self.minor is None and other.minor is not None
208-
else self.minor
208+
if self.minor is None and other.minor is None:
209+
self_minor = 0
210+
other_minor = 0
211+
elif self.minor is None and other.minor is not None:
212+
self_minor = other.minor + 1
213+
other_minor = other.minor
214+
elif other.minor is None and self.minor is not None:
215+
self_minor = self.minor
216+
other_minor = self.minor + 1
217+
elif self.minor is not None and other.minor is not None:
218+
self_minor = self.minor
219+
other_minor = other.minor
220+
else:
221+
# We need this because mypy is not smart enough to figure it out
222+
assert False, "unreachable"
223+
return (self.major, self_minor, self.name) < (
224+
other.major,
225+
other_minor,
226+
other.name,
209227
)
210-
return (self.major, minor, self.name) < (other.major, other.minor, other.name)
211228

212229

213230
class RepoVersionInfo: # pylint: disable=too-many-instance-attributes

0 commit comments

Comments
 (0)