From d81d8c10b3e8894b78fbd6a3f3894363a355fbc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Thu, 29 Oct 2026 14:11:13 +0100 Subject: [PATCH] desktop: Sort tags by version, newest first Same as in explorer: compare numeric parts numerically and keep tags from the same namespace together instead of interleaving them by time. --- desktop/src/lib/refs.ts | 25 ++++++++++++++++---- desktop/tests/unit/refs.test.ts | 42 +++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/desktop/src/lib/refs.ts b/desktop/src/lib/refs.ts index 83e295a9b..e14878593 100644 --- a/desktop/src/lib/refs.ts +++ b/desktop/src/lib/refs.ts @@ -4,11 +4,28 @@ import type { Tag } from "@bindings/repo/Tag"; import orderBy from "lodash/orderBy"; +export function compareTagNames(a: string, b: string): number { + if (b.startsWith(`${a}-`)) return -1; + if (a.startsWith(`${b}-`)) return 1; + return b.localeCompare(a, undefined, { numeric: true }); +} + export function sortedTags(tags: Record): [string, Tag][] { - return Object.entries(tags).sort(([nameA, a], [nameB, b]) => { - if (a.timestamp !== b.timestamp) return b.timestamp - a.timestamp; - return nameB.localeCompare(nameA); - }); + const groups = new Map(); + for (const entry of Object.entries(tags)) { + const slash = entry[0].lastIndexOf("/"); + const namespace = slash === -1 ? "" : entry[0].slice(0, slash); + const group = groups.get(namespace) ?? { tags: [], newest: 0 }; + group.tags.push(entry); + group.newest = Math.max(group.newest, entry[1].timestamp); + groups.set(namespace, group); + } + + return [...groups.entries()] + .sort(([nsA, a], [nsB, b]) => b.newest - a.newest || nsA.localeCompare(nsB)) + .flatMap(([, group]) => + group.tags.sort(([a], [b]) => compareTagNames(a, b)), + ); } // Delegates first; within each (delegate / non-delegate) group, peers with diff --git a/desktop/tests/unit/refs.test.ts b/desktop/tests/unit/refs.test.ts index 260b75e54..e3927e9da 100644 --- a/desktop/tests/unit/refs.test.ts +++ b/desktop/tests/unit/refs.test.ts @@ -12,14 +12,46 @@ function remote(props: Partial = {}): Remote { return { id: "z6Mk", delegate: false, branches: {}, tags: {}, ...props }; } -test("sortedTags lists the newest first, then by name descending", () => { +test("sortedTags sorts by version, newest first", () => { const sorted = sortedTags({ - "v1.0": tag(1), - "v1.2": tag(2), - "v1.1": tag(2), + "v0.1.0": tag(1), + "v0.10.0": tag(4), + "v0.2.0": tag(2), + "v1.0.0-rc.10": tag(6), + "v1.0.0": tag(7), + "v1.0.0-rc.2": tag(5), + "v0.10.1": tag(3), }); - expect(sorted.map(([name]) => name)).toEqual(["v1.2", "v1.1", "v1.0"]); + expect(sorted.map(([name]) => name)).toEqual([ + "v1.0.0", + "v1.0.0-rc.10", + "v1.0.0-rc.2", + "v0.10.1", + "v0.10.0", + "v0.2.0", + "v0.1.0", + ]); +}); + +test("sortedTags keeps namespaces together, most recent first", () => { + const sorted = sortedTags({ + "releases/0.29.0": tag(3), + "releases/0.9.0": tag(1), + "releases/desktop/0.16.0": tag(3), + "releases/desktop/0.17.0": tag(4), + "releases/httpd/0.29.0": tag(3), + "releases/httpd/0.30.0": tag(5), + }); + + expect(sorted.map(([name]) => name)).toEqual([ + "releases/httpd/0.30.0", + "releases/httpd/0.29.0", + "releases/desktop/0.17.0", + "releases/desktop/0.16.0", + "releases/0.29.0", + "releases/0.9.0", + ]); }); test("sortRemotes puts delegates first, then aliased peers by alias, then by NID", () => {