From 9c360925e1a9ad1f53b71c99ba916efaa1b59a83 Mon Sep 17 00:00:00 2001 From: Brandon Oxendine Date: Tue, 27 Oct 2026 16:42:57 +0100 Subject: [PATCH] explorer: Sort tags by version, newest first MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tag names were compared as plain strings, so 0.10.0 sorted between 0.1.0 and 0.2.0. Canonical tags were ordered by tagger time, which interleaved the httpd and desktop releases in monorepos. Tags now stay together per namespace, newest namespace first, sorted by version. Co-authored-by: Rūdolfs Ošiņš --- explorer/src/lib/utils.ts | 33 ++++++++++ .../repos/Source/PeerBranchSelector.svelte | 13 ++-- .../Source/PeerBranchSelector/Peer.svelte | 9 ++- explorer/tests/unit/utils.test.ts | 65 +++++++++++++++++++ 4 files changed, 109 insertions(+), 11 deletions(-) diff --git a/explorer/src/lib/utils.ts b/explorer/src/lib/utils.ts index 68fd692b3..071955029 100644 --- a/explorer/src/lib/utils.ts +++ b/explorer/src/lib/utils.ts @@ -368,6 +368,39 @@ export function getBranchesFromRefs( return branches; } +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 interface TagGroup { + namespace: string; + tags: [string, T][]; +} + +export function groupTags( + tags: [string, T][], + timestampOf: (tag: T) => number = () => 0, +): TagGroup[] { + const groups = new Map(); + for (const tag of tags) { + const slash = tag[0].lastIndexOf("/"); + const namespace = slash === -1 ? "" : tag[0].slice(0, slash); + const group = groups.get(namespace) ?? { tags: [], newest: 0 }; + group.tags.push(tag); + group.newest = Math.max(group.newest, timestampOf(tag[1])); + groups.set(namespace, group); + } + + return [...groups.entries()] + .sort(([nsA, a], [nsB, b]) => b.newest - a.newest || nsA.localeCompare(nsB)) + .map(([namespace, group]) => ({ + namespace, + tags: group.tags.sort(([a], [b]) => compareTagNames(a, b)), + })); +} + export function getTagsFromRefs( refs: Record, ): Record { diff --git a/explorer/src/views/repos/Source/PeerBranchSelector.svelte b/explorer/src/views/repos/Source/PeerBranchSelector.svelte index 4ceed3566..4c7ddcea3 100644 --- a/explorer/src/views/repos/Source/PeerBranchSelector.svelte +++ b/explorer/src/views/repos/Source/PeerBranchSelector.svelte @@ -12,6 +12,7 @@ getBranchesFromRefs, getTagsFromRefs, gravatarURL, + groupTags, safeDecodeURIComponent, } from "@app/lib/utils"; @@ -125,14 +126,10 @@ (r.obj.peer === undefined ? 10 : 1) * (r.obj.peer?.alias ? 2 : 1), }); - $: canonicalTags = Object.entries(canonicalTagsInfo).sort( - ([nameA, infoA], [nameB, infoB]) => { - const tsA = infoA.tagger?.timestamp ?? 0; - const tsB = infoB.tagger?.timestamp ?? 0; - if (tsA !== tsB) return tsB - tsA; - return nameB.localeCompare(nameA); - }, - ); + $: canonicalTags = groupTags( + Object.entries(canonicalTagsInfo), + info => info.tagger?.timestamp ?? 0, + ).flatMap(group => group.tags); $: canonicalBranches = Object.entries(canonicalBranchesMap).filter( ([branchName]) => branchName !== repo.payloads["xyz.radicle.project"].data.defaultBranch, diff --git a/explorer/src/views/repos/Source/PeerBranchSelector/Peer.svelte b/explorer/src/views/repos/Source/PeerBranchSelector/Peer.svelte index 7a9f51f4d..cfb5f772e 100644 --- a/explorer/src/views/repos/Source/PeerBranchSelector/Peer.svelte +++ b/explorer/src/views/repos/Source/PeerBranchSelector/Peer.svelte @@ -7,6 +7,7 @@ formatCommit, getBranchesFromRefs, getTagsFromRefs, + groupTags, } from "@app/lib/utils"; import { replace } from "@app/lib/router"; @@ -32,8 +33,10 @@ $: refs = type === "branches" - ? getBranchesFromRefs(peer.remote.refs) - : getTagsFromRefs(peer.remote.refs); + ? Object.entries(getBranchesFromRefs(peer.remote.refs)) + : groupTags(Object.entries(getTagsFromRefs(peer.remote.refs))).flatMap( + group => group.tags, + ); $: if (peer.selected) { expanded = true; @@ -66,7 +69,7 @@ {#if expanded} - {#each Object.entries(refs) as [name, head]} + {#each refs as [name, head]} { expect(utils.fragmentId(hash)).toEqual(expected); }); }); + +describe("compareTagNames", () => { + test("sorts version tags numerically, newest first", () => { + const tags = [ + "v0.1.0", + "v0.10.0", + "v0.2.0", + "v1.0.0-rc.10", + "v1.0.0", + "v1.0.0-rc.2", + "v0.10.1", + ]; + expect(tags.sort(utils.compareTagNames)).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", + ]); + }); +}); + +describe("groupTags", () => { + test("keeps namespaces together, most recent first", () => { + const tags: [string, number][] = [ + ["releases/0.29.0", 3], + ["releases/0.9.0", 1], + ["releases/desktop/0.16.0", 3], + ["releases/desktop/0.17.0", 4], + ["releases/httpd/0.29.0", 3], + ["releases/httpd/0.30.0", 5], + ]; + expect( + utils + .groupTags(tags, timestamp => timestamp) + .map(group => ({ + namespace: group.namespace, + tags: group.tags.map(([name]) => name), + })), + ).toEqual([ + { + namespace: "releases/httpd", + tags: ["releases/httpd/0.30.0", "releases/httpd/0.29.0"], + }, + { + namespace: "releases/desktop", + tags: ["releases/desktop/0.17.0", "releases/desktop/0.16.0"], + }, + { namespace: "releases", tags: ["releases/0.29.0", "releases/0.9.0"] }, + ]); + }); + + test("orders namespaces by name without timestamps", () => { + const tags: [string, undefined][] = [ + ["v1.0.0", undefined], + ["releases/httpd/0.30.0", undefined], + ["releases/desktop/0.17.0", undefined], + ]; + expect( + utils.groupTags(tags).flatMap(group => group.tags.map(([name]) => name)), + ).toEqual(["v1.0.0", "releases/desktop/0.17.0", "releases/httpd/0.30.0"]); + }); +});