mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-16 03:40:37 +08:00
explorer: Sort tags by version, newest first
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ņš <rudolfs@osins.org>
This commit is contained in:
committed by
Rūdolfs Ošiņš
co-authored by
Rūdolfs Ošiņš
parent
ad91a96490
commit
9c360925e1
@@ -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<T> {
|
||||
namespace: string;
|
||||
tags: [string, T][];
|
||||
}
|
||||
|
||||
export function groupTags<T>(
|
||||
tags: [string, T][],
|
||||
timestampOf: (tag: T) => number = () => 0,
|
||||
): TagGroup<T>[] {
|
||||
const groups = new Map<string, { tags: [string, T][]; newest: number }>();
|
||||
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<string, string>,
|
||||
): Record<string, string> {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 @@
|
||||
</div>
|
||||
</div>
|
||||
{#if expanded}
|
||||
{#each Object.entries(refs) as [name, head]}
|
||||
{#each refs as [name, head]}
|
||||
<Link
|
||||
style={subgridStyle}
|
||||
route={{
|
||||
|
||||
@@ -448,3 +448,68 @@ describe("fragmentId", () => {
|
||||
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"]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user