From ceeab4e2c64415a08d9dd8c95a27102d813223d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Wed, 30 Sep 2026 15:17:03 +0200 Subject: [PATCH] Share which commits each revision adds Patch and Revision each computed it with their own copy of the loop. --- src/components/Revision.svelte | 18 ++++++++---------- src/lib/revisionList.ts | 18 ++++++++++++++++++ src/views/repo/Patch.svelte | 22 +++++++++++----------- tests/unit/revisionList.test.ts | 28 +++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/components/Revision.svelte b/src/components/Revision.svelte index 6313dea89..3a3aa7aca 100644 --- a/src/components/Revision.svelte +++ b/src/components/Revision.svelte @@ -11,6 +11,7 @@ import type { Commit } from "@bindings/repo/Commit"; import type { Snippet } from "svelte"; + import mapValues from "lodash/mapValues"; import partial from "lodash/partial"; import { slide } from "svelte/transition"; @@ -38,6 +39,7 @@ splitDescription, visibleCommitsOf, } from "@app/lib/revisionDescription"; + import { newCommitsByRevision } from "@app/lib/revisionList"; import * as roles from "@app/lib/roles"; import { push } from "@app/lib/router"; import { @@ -539,16 +541,12 @@ } }), ).then(entries => { - const next: Record = {}; - // eslint-disable-next-line svelte/prefer-svelte-reactivity - const seen = new Set(); - revisions.forEach((rev, i) => { - const [, commits] = entries[i]; - const novel = commits.filter(c => !seen.has(c.id)); - novel.forEach(c => seen.add(c.id)); - next[rev.id] = [...novel].reverse(); - }); - commitsByRevision = next; + const commits = Object.fromEntries(entries); + // Newest first, as the timeline lists them. + commitsByRevision = mapValues( + newCommitsByRevision(revisions, id => commits[id] ?? []), + added => [...added].reverse(), + ); }); }); const olderRevisionIds = $derived( diff --git a/src/lib/revisionList.ts b/src/lib/revisionList.ts index 8fb935ca2..c7b1251bb 100644 --- a/src/lib/revisionList.ts +++ b/src/lib/revisionList.ts @@ -1,4 +1,5 @@ import type { Revision } from "@bindings/cob/patch/Revision"; +import type { Commit } from "@bindings/repo/Commit"; import type { RevisionListSettings } from "@app/lib/revisionListSettings"; @@ -46,3 +47,20 @@ export function orderRevisions( return settings.sortDesc ? [...group].reverse() : group; }); } + +/// The commits each revision adds over the revisions before it, in the order +/// `commitsOf` lists them. A later revision usually contains every earlier +/// commit, so counting its full range would count them again. +export function newCommitsByRevision( + revisions: { id: string }[], + commitsOf: (revisionId: string) => Commit[], +): Record { + const seen = new Set(); + const result: Record = {}; + for (const revision of revisions) { + const added = commitsOf(revision.id).filter(c => !seen.has(c.id)); + added.forEach(c => seen.add(c.id)); + result[revision.id] = added; + } + return result; +} diff --git a/src/views/repo/Patch.svelte b/src/views/repo/Patch.svelte index ee5b11dc2..b5449b9f4 100644 --- a/src/views/repo/Patch.svelte +++ b/src/views/repo/Patch.svelte @@ -12,6 +12,7 @@ import type { Commit } from "@bindings/repo/Commit"; import type { RepoInfo } from "@bindings/repo/RepoInfo"; + import mapValues from "lodash/mapValues"; import { SvelteSet } from "svelte/reactivity"; import { commentSourcesOf } from "@app/lib/commentSources"; @@ -28,7 +29,11 @@ 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 { + newCommitsByRevision, + orderRevisions, + revisionNumbers, + } from "@app/lib/revisionList"; import { revisionListSettings } from "@app/lib/revisionListSettings"; import * as roles from "@app/lib/roles"; import * as router from "@app/lib/router"; @@ -378,16 +383,11 @@ } }), ).then(entries => { - const next: Record = {}; - // eslint-disable-next-line svelte/prefer-svelte-reactivity - const seen = new Set(); - ordered.forEach((rev, i) => { - const [, commits] = entries[i]; - const novel = commits.filter(c => !seen.has(c.id)); - novel.forEach(c => seen.add(c.id)); - next[rev.id] = novel.length; - }); - commitCountsByRevisionId = next; + const commits = Object.fromEntries(entries); + commitCountsByRevisionId = mapValues( + newCommitsByRevision(ordered, id => commits[id] ?? []), + added => added.length, + ); }); }); // Per-revision diff stats for the dropdown: one request each, so they are diff --git a/tests/unit/revisionList.test.ts b/tests/unit/revisionList.test.ts index faaf426d8..611f5ddbe 100644 --- a/tests/unit/revisionList.test.ts +++ b/tests/unit/revisionList.test.ts @@ -1,6 +1,12 @@ +import type { Commit } from "@bindings/repo/Commit"; + import { describe, expect, test } from "vitest"; -import { orderRevisions, revisionNumbers } from "@app/lib/revisionList"; +import { + newCommitsByRevision, + orderRevisions, + revisionNumbers, +} from "@app/lib/revisionList"; import type { RevisionListSettings } from "@app/lib/revisionListSettings"; import { author, revision } from "./support/cobs"; @@ -93,3 +99,23 @@ describe("orderRevisions", () => { expect(ids(input)).toEqual(ids(revisions)); }); }); + +test("newCommitsByRevision keeps only commits not in an earlier revision", () => { + const c = (id: string) => ({ id }) as Commit; + const commits: Record = { + a1: [c("1"), c("2")], + b1: [c("1"), c("2"), c("3")], + c1: [c("2"), c("4"), c("3"), c("5")], + }; + + const added = newCommitsByRevision( + [{ id: "a1" }, { id: "b1" }, { id: "c1" }], + id => commits[id], + ); + + expect( + Object.fromEntries( + Object.entries(added).map(([id, list]) => [id, list.map(x => x.id)]), + ), + ).toEqual({ a1: ["1", "2"], b1: ["3"], c1: ["4", "5"] }); +});