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.
This commit is contained in:
Rūdolfs Ošiņš
2026-10-29 14:11:13 +01:00
parent 9c360925e1
commit d81d8c10b3
2 changed files with 58 additions and 9 deletions
+21 -4
View File
@@ -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>): [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<string, { tags: [string, Tag][]; newest: number }>();
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
+37 -5
View File
@@ -12,14 +12,46 @@ function remote(props: Partial<Remote> = {}): 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", () => {