mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 02:00:31 +08:00
Move revision description helpers into lib
The commit-list check was written twice, and revision titles and the commit group collapse rule were untested inside components.
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
getDiffText,
|
||||
} from "@app/lib/invoke";
|
||||
import { anchorOf, commentCountsByPath } from "@app/lib/pierreComments";
|
||||
import { isCommitListDescription } from "@app/lib/revisionDescription";
|
||||
import { useShortcuts } from "@app/lib/shortcuts.svelte";
|
||||
import { pluralize } from "@app/lib/utils";
|
||||
|
||||
@@ -329,16 +330,6 @@
|
||||
}
|
||||
});
|
||||
|
||||
function isCommitListDescription(description: string, commits: Commit[]) {
|
||||
if (commits.length === 0) return false;
|
||||
const chunks = description
|
||||
.split("\n")
|
||||
.map(l => l.trim())
|
||||
.filter(l => l.length > 0);
|
||||
if (chunks.length !== commits.length) return false;
|
||||
const summaries = new Set(commits.map(c => c.summary.trim()));
|
||||
return chunks.every(line => summaries.has(line));
|
||||
}
|
||||
const revisionDescription = $derived(
|
||||
revision.description.slice(-1)[0]?.body?.trim() ?? "",
|
||||
);
|
||||
|
||||
@@ -4,22 +4,6 @@
|
||||
import type { FlattenedPatchOperation } from "@app/lib/patchTimeline";
|
||||
|
||||
export type { FlattenedPatchOperation };
|
||||
|
||||
export function splitDescription(text: string): {
|
||||
subject?: string;
|
||||
body?: string;
|
||||
} {
|
||||
const trimmed = text.trim();
|
||||
if (!trimmed) return {};
|
||||
const idx = trimmed.indexOf("\n");
|
||||
if (idx === -1) return { subject: trimmed };
|
||||
const subject = trimmed.slice(0, idx).trim();
|
||||
const body = trimmed.slice(idx + 1).trim();
|
||||
return {
|
||||
subject: subject || undefined,
|
||||
body: body || undefined,
|
||||
};
|
||||
}
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
@@ -27,6 +11,7 @@
|
||||
|
||||
import { itemDiff } from "@app/lib/cobActivity";
|
||||
import { cachedConfig, writeToClipboard } from "@app/lib/invoke";
|
||||
import { splitDescription } from "@app/lib/revisionDescription";
|
||||
import {
|
||||
absoluteTimestamp,
|
||||
authorForNodeId,
|
||||
|
||||
@@ -32,6 +32,12 @@
|
||||
} from "@app/lib/invoke";
|
||||
import type { PatchActivityData } from "@app/lib/patchTimeline";
|
||||
import { patchTimeline } from "@app/lib/patchTimeline";
|
||||
import {
|
||||
groupCommitsByAuthor,
|
||||
isCommitListDescription,
|
||||
splitDescription,
|
||||
visibleCommitsOf,
|
||||
} from "@app/lib/revisionDescription";
|
||||
import * as roles from "@app/lib/roles";
|
||||
import { push } from "@app/lib/router";
|
||||
import {
|
||||
@@ -61,9 +67,7 @@
|
||||
import Icon from "@app/components/Icon.svelte";
|
||||
import Markdown from "@app/components/Markdown.svelte";
|
||||
import NodeId from "@app/components/NodeId.svelte";
|
||||
import PatchActivityItem, {
|
||||
splitDescription,
|
||||
} from "@app/components/PatchActivityItem.svelte";
|
||||
import PatchActivityItem from "@app/components/PatchActivityItem.svelte";
|
||||
import Path from "@app/components/Path.svelte";
|
||||
import PierreSnippet from "@app/components/PierreSnippet.svelte";
|
||||
import { closeFocused } from "@app/components/Popover.svelte";
|
||||
@@ -185,8 +189,6 @@
|
||||
revisionDescriptionEdits = {};
|
||||
}
|
||||
});
|
||||
const MAX_COMMITS_VISIBLE = 3;
|
||||
const COMMIT_COLLAPSE_THRESHOLD = 5;
|
||||
function isRevisionExpanded(revId: string): boolean {
|
||||
if (revId in revisionToggles) {
|
||||
return revisionToggles[revId];
|
||||
@@ -227,34 +229,6 @@
|
||||
revisionToggles = next;
|
||||
}
|
||||
}
|
||||
// A description that is exactly the list of commit summaries is the default
|
||||
// Radicle produces, and is noise next to the commits themselves.
|
||||
function isCommitListDescription(
|
||||
description: string,
|
||||
commits: Commit[] | undefined,
|
||||
): boolean {
|
||||
if (!commits || commits.length === 0) return false;
|
||||
const chunks = description
|
||||
.split("\n")
|
||||
.map(l => l.trim())
|
||||
.filter(l => l.length > 0);
|
||||
if (chunks.length !== commits.length) return false;
|
||||
const summaries = new Set(commits.map(c => c.summary.trim()));
|
||||
return chunks.every(line => summaries.has(line));
|
||||
}
|
||||
function groupCommitsByAuthor(commits: Commit[]): Commit[][] {
|
||||
const groups: Commit[][] = [];
|
||||
for (const commit of commits) {
|
||||
const last = groups[groups.length - 1];
|
||||
if (last && last[0].author.name === commit.author.name) {
|
||||
last.push(commit);
|
||||
} else {
|
||||
groups.push([commit]);
|
||||
}
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
const draftReview = $derived(
|
||||
draftReviewStorage.getForRevision(revision.id, currentUserAuthor),
|
||||
);
|
||||
@@ -1202,15 +1176,11 @@
|
||||
{#if group.length > 1}
|
||||
{@const groupKey = group[0].id}
|
||||
{@const groupExpanded = isCommitGroupExpanded(groupKey)}
|
||||
{@const collapsed =
|
||||
!groupExpanded &&
|
||||
group.length > COMMIT_COLLAPSE_THRESHOLD}
|
||||
{@const visibleCommits = collapsed
|
||||
? group.slice(0, MAX_COMMITS_VISIBLE)
|
||||
: group}
|
||||
{@const hiddenCount = collapsed
|
||||
? group.length - MAX_COMMITS_VISIBLE
|
||||
: 0}
|
||||
{@const {
|
||||
collapsed,
|
||||
visible: visibleCommits,
|
||||
hiddenCount,
|
||||
} = visibleCommitsOf(group, groupExpanded)}
|
||||
<div class="commit-group">
|
||||
<div class="commit-group-author txt-body-m-regular">
|
||||
{group[0].author.name} <{group[0].author.email}>
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import type { Revision } from "@bindings/cob/patch/Revision";
|
||||
import type { Commit } from "@bindings/repo/Commit";
|
||||
|
||||
/// A description's first line and the rest, both trimmed.
|
||||
export function splitDescription(text: string): {
|
||||
subject?: string;
|
||||
body?: string;
|
||||
} {
|
||||
const trimmed = text.trim();
|
||||
if (!trimmed) return {};
|
||||
const idx = trimmed.indexOf("\n");
|
||||
if (idx === -1) return { subject: trimmed };
|
||||
const subject = trimmed.slice(0, idx).trim();
|
||||
const body = trimmed.slice(idx + 1).trim();
|
||||
return {
|
||||
subject: subject || undefined,
|
||||
body: body || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
/// The first line of a revision's current description.
|
||||
export function revisionTitle(revision: Revision): string | undefined {
|
||||
return splitDescription(revision.description.at(-1)?.body ?? "").subject;
|
||||
}
|
||||
|
||||
/// A description that is exactly the list of commit summaries is the default
|
||||
/// Radicle produces, and is noise next to the commits themselves.
|
||||
export function isCommitListDescription(
|
||||
description: string,
|
||||
commits: Commit[] | undefined,
|
||||
): boolean {
|
||||
if (!commits || commits.length === 0) return false;
|
||||
const lines = description
|
||||
.split("\n")
|
||||
.map(line => line.trim())
|
||||
.filter(line => line.length > 0);
|
||||
if (lines.length !== commits.length) return false;
|
||||
const summaries = new Set(commits.map(c => c.summary.trim()));
|
||||
return lines.every(line => summaries.has(line));
|
||||
}
|
||||
|
||||
/// Consecutive commits by the same author name.
|
||||
export function groupCommitsByAuthor(commits: Commit[]): Commit[][] {
|
||||
const groups: Commit[][] = [];
|
||||
for (const commit of commits) {
|
||||
const last = groups[groups.length - 1];
|
||||
if (last && last[0].author.name === commit.author.name) {
|
||||
last.push(commit);
|
||||
} else {
|
||||
groups.push([commit]);
|
||||
}
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
const MAX_COMMITS_VISIBLE = 3;
|
||||
const COMMIT_COLLAPSE_THRESHOLD = 5;
|
||||
|
||||
/// Which of a group's commits to show. A long group shows its first few until
|
||||
/// the reader expands it.
|
||||
export function visibleCommitsOf(
|
||||
group: Commit[],
|
||||
expanded: boolean,
|
||||
): { collapsed: boolean; visible: Commit[]; hiddenCount: number } {
|
||||
const collapsed = !expanded && group.length > COMMIT_COLLAPSE_THRESHOLD;
|
||||
return collapsed
|
||||
? {
|
||||
collapsed,
|
||||
visible: group.slice(0, MAX_COMMITS_VISIBLE),
|
||||
hiddenCount: group.length - MAX_COMMITS_VISIBLE,
|
||||
}
|
||||
: { collapsed, visible: group, hiddenCount: 0 };
|
||||
}
|
||||
@@ -27,6 +27,7 @@
|
||||
import { setPatchActivitySource } from "@app/lib/patchActivityContext";
|
||||
import { patchContributions } from "@app/lib/patchContributions";
|
||||
import type { ReviewEntry } from "@app/lib/reviewSummary";
|
||||
import { revisionTitle } from "@app/lib/revisionDescription";
|
||||
import { orderRevisions, revisionNumbers } from "@app/lib/revisionList";
|
||||
import { revisionListSettings } from "@app/lib/revisionListSettings";
|
||||
import * as roles from "@app/lib/roles";
|
||||
@@ -417,13 +418,6 @@
|
||||
orderedRevisions.findIndex(r => r.id === selectedRevisionId),
|
||||
);
|
||||
|
||||
function revisionTitle(rev: Revision): string | undefined {
|
||||
const body = rev.description.at(-1)?.body?.trim();
|
||||
if (!body) return undefined;
|
||||
const line = body.split("\n")[0].trim();
|
||||
return line.length > 0 ? line : undefined;
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
const rev = latestRevision;
|
||||
let cancelled = false;
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import type { Commit } from "@bindings/repo/Commit";
|
||||
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import {
|
||||
groupCommitsByAuthor,
|
||||
isCommitListDescription,
|
||||
revisionTitle,
|
||||
splitDescription,
|
||||
visibleCommitsOf,
|
||||
} from "@app/lib/revisionDescription";
|
||||
|
||||
import { revision } from "./support/cobs";
|
||||
|
||||
function commit(id: string, summary = id, name = "Alice"): Commit {
|
||||
const person = { name, email: `${name}@x`, time: 0 };
|
||||
return {
|
||||
id,
|
||||
author: person,
|
||||
committer: person,
|
||||
summary,
|
||||
message: summary,
|
||||
parents: [],
|
||||
};
|
||||
}
|
||||
|
||||
describe("splitDescription", () => {
|
||||
test.each([
|
||||
["", {}],
|
||||
[" ", {}],
|
||||
["Subject", { subject: "Subject" }],
|
||||
[" Subject \n\n Body text ", { subject: "Subject", body: "Body text" }],
|
||||
["Subject\n", { subject: "Subject" }],
|
||||
])("splits %j into %j", (text, expected) => {
|
||||
expect(splitDescription(text)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
test("revisionTitle is the first line of the current description", () => {
|
||||
const edit = (body: string) => ({
|
||||
author: revision().author,
|
||||
timestamp: 0,
|
||||
body,
|
||||
});
|
||||
|
||||
expect(
|
||||
revisionTitle(revision({ description: [edit("Old"), edit("New\nbody")] })),
|
||||
).toBe("New");
|
||||
expect(revisionTitle(revision({ description: [] }))).toBeUndefined();
|
||||
expect(
|
||||
revisionTitle(revision({ description: [edit(" \n ")] })),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
describe("isCommitListDescription", () => {
|
||||
const commits = [commit("a", "Add a"), commit("b", "Fix b")];
|
||||
|
||||
test("matches a description that lists the commit summaries", () => {
|
||||
expect(isCommitListDescription("Add a\nFix b", commits)).toBe(true);
|
||||
expect(isCommitListDescription(" Fix b\n\nAdd a ", commits)).toBe(true);
|
||||
});
|
||||
|
||||
test.each([
|
||||
["Add a", "a line is missing"],
|
||||
["Add a\nFix b\nMore", "there is an extra line"],
|
||||
["Add a\nSomething else", "a line isn't a summary"],
|
||||
])("doesn't match %j because %s", description => {
|
||||
expect(isCommitListDescription(description, commits)).toBe(false);
|
||||
});
|
||||
|
||||
test("doesn't match without commits", () => {
|
||||
expect(isCommitListDescription("", [])).toBe(false);
|
||||
expect(isCommitListDescription("Add a", undefined)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
test("groupCommitsByAuthor groups consecutive commits by author name", () => {
|
||||
const groups = groupCommitsByAuthor([
|
||||
commit("1", "1", "Alice"),
|
||||
commit("2", "2", "Alice"),
|
||||
commit("3", "3", "Bob"),
|
||||
commit("4", "4", "Alice"),
|
||||
]);
|
||||
|
||||
expect(groups.map(g => g.map(c => c.id))).toEqual([["1", "2"], ["3"], ["4"]]);
|
||||
});
|
||||
|
||||
describe("visibleCommitsOf", () => {
|
||||
const group = (n: number) =>
|
||||
Array.from({ length: n }, (_, i) => commit(`${i}`));
|
||||
|
||||
test("shows a short group in full", () => {
|
||||
expect(visibleCommitsOf(group(5), false)).toMatchObject({
|
||||
collapsed: false,
|
||||
hiddenCount: 0,
|
||||
});
|
||||
expect(visibleCommitsOf(group(5), false).visible).toHaveLength(5);
|
||||
});
|
||||
|
||||
test("collapses a long group to its first three", () => {
|
||||
const { collapsed, visible, hiddenCount } = visibleCommitsOf(
|
||||
group(6),
|
||||
false,
|
||||
);
|
||||
|
||||
expect(collapsed).toBe(true);
|
||||
expect(visible.map(c => c.id)).toEqual(["0", "1", "2"]);
|
||||
expect(hiddenCount).toBe(3);
|
||||
});
|
||||
|
||||
test("shows a long group in full once expanded", () => {
|
||||
expect(visibleCommitsOf(group(6), true)).toMatchObject({
|
||||
collapsed: false,
|
||||
hiddenCount: 0,
|
||||
});
|
||||
expect(visibleCommitsOf(group(6), true).visible).toHaveLength(6);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user