diff --git a/desktop/src/App.svelte b/desktop/src/App.svelte
index 04d95d735..60e7a8238 100644
--- a/desktop/src/App.svelte
+++ b/desktop/src/App.svelte
@@ -289,7 +289,10 @@
{:else if $activeRouteStore.resource === "repo.home"}
{:else if $activeRouteStore.resource === "repo.commits"}
-
+
+ {#key `${$activeRouteStore.params.peer}:${$activeRouteStore.params.revision}:${$activeRouteStore.params.oid}`}
+
+ {/key}
{:else if $activeRouteStore.resource === "repo.commit"}
{:else if $activeRouteStore.resource === "repo.identity"}
diff --git a/desktop/src/components/VirtualList.svelte b/desktop/src/components/VirtualList.svelte
index 2d56d3d01..0f1f1ea7c 100644
--- a/desktop/src/components/VirtualList.svelte
+++ b/desktop/src/components/VirtualList.svelte
@@ -28,6 +28,9 @@
autoStartMargin?: boolean;
// Distance from the bottom (in pixels) at which to request the next page.
prefetchPx?: number;
+ // Keep the rows in view where they are when rows are added before them,
+ // for a list that only ever grows at the top or the bottom.
+ keepRowsOnPrepend?: boolean;
// Restore a previously-captured scroll position (used on history nav).
initialCache?: ListCacheSnapshot;
initialScrollOffset?: number;
@@ -60,6 +63,7 @@
startMargin = undefined,
autoStartMargin = false,
prefetchPx = 800,
+ keepRowsOnPrepend = false,
initialCache = undefined,
initialScrollOffset = undefined,
onRestored = undefined,
@@ -75,6 +79,39 @@
let handle = $state(undefined);
let restored = false;
+ // With `keepRowsOnPrepend`, rows added before the ones in view, such as new
+ // commits at the top, keep the rows in view where they are, so that nothing
+ // moves under the reader. Virtua keeps the position from the end while
+ // `shift` is set, which only holds when the end stays the same, so not for
+ // pages loaded at the end. At the very top, new rows show instead.
+ let shift = $state(false);
+ let lastKey: string | number | undefined;
+ let lastLength = 0;
+ $effect.pre(() => {
+ const last = items.length > 0 ? keyOf(items.length - 1) : undefined;
+ shift =
+ keepRowsOnPrepend &&
+ lastKey !== undefined &&
+ last === lastKey &&
+ items.length > lastLength &&
+ (viewport?.scrollTop ?? 0) > 0;
+ lastKey = last;
+ lastLength = items.length;
+ });
+ // Only for the update that added the rows: virtua also applies it to rows
+ // that resize later.
+ $effect(() => {
+ if (!shift) return;
+ const id = requestAnimationFrame(() => {
+ shift = false;
+ });
+ return () => cancelAnimationFrame(id);
+ });
+
+ function keyOf(index: number): string | number {
+ return getKey ? getKey(items[index], index) : index;
+ }
+
// A zero-height marker placed right before the virtualizer; its position
// tells us how far down the list starts inside the scroll viewport.
let anchorEl = $state(undefined);
@@ -211,6 +248,7 @@
itemSize={estimatedItemSize}
{bufferSize}
startMargin={effectiveStartMargin}
+ {shift}
cache={initialCache}
onscroll={onScroll}
onscrollend={reportState}>
diff --git a/desktop/src/lib/latestHead.svelte.ts b/desktop/src/lib/latestHead.svelte.ts
new file mode 100644
index 000000000..572fb5d01
--- /dev/null
+++ b/desktop/src/lib/latestHead.svelte.ts
@@ -0,0 +1,52 @@
+import type { Commit } from "@bindings/repo/Commit";
+
+import { keys } from "@radicle-gui/core/keys";
+import {
+ createQuery,
+ invalidatedSinceLoading,
+} from "@radicle-gui/ui/lib/query.svelte";
+
+import { invoke } from "@app/lib/invoke";
+import { queryClient } from "@app/lib/query";
+
+interface ResolvedRevision {
+ rid: string;
+ peer: string | undefined;
+ revision: string | undefined;
+ // The commit the route loader resolved the revision to.
+ oid: string;
+}
+
+// The commit a revision resolves to now. A branch, or the default branch
+// without a revision, can move on while the user reads, so a view keeps
+// showing the loaded commit and offers the newer one. Create it during
+// component initialisation; the route loader marks `keys.head` as loading first.
+export function latestHead(revision: () => ResolvedRevision): {
+ readonly current: string | undefined;
+} {
+ const headKey = $derived(
+ keys.head(revision().rid, revision().peer, revision().revision),
+ );
+ // The loader has just resolved the revision, so that is the latest commit.
+ // A ref update during the load may have moved it since, so then the head is
+ // checked again at mount, and a push during the load still shows as newer.
+ $effect.pre(() => {
+ queryClient.setQueryData(headKey, revision().oid);
+ if (invalidatedSinceLoading(queryClient, headKey)) {
+ void queryClient.invalidateQueries({ queryKey: headKey, exact: true });
+ }
+ });
+ const head = createQuery(queryClient, () => {
+ const { rid, peer, revision: rev } = revision();
+ return {
+ queryKey: headKey,
+ queryFn: async () =>
+ (await invoke("repo_commit", { rid, peer, revision: rev })).id,
+ };
+ });
+ return {
+ get current() {
+ return head.current.data;
+ },
+ };
+}
diff --git a/desktop/src/lib/newerCommits.ts b/desktop/src/lib/newerCommits.ts
new file mode 100644
index 000000000..7f7da472a
--- /dev/null
+++ b/desktop/src/lib/newerCommits.ts
@@ -0,0 +1,37 @@
+import type { PaginatedQuery } from "@bindings/cob/PaginatedQuery";
+
+interface CommitLike {
+ id: string;
+ parents: string[];
+}
+
+export type NewerCommits =
+ | { type: "added"; commits: T[]; merge: boolean }
+ // `from` isn't among the newer commit's ancestors, so history was rewritten.
+ | { type: "rewritten" };
+
+// The commits a branch gained since `from`, newest first, fetched a page at a
+// time from the new head and up to `limit` of them.
+export async function newerCommits(
+ fetchPage: (skip: number, take: number) => Promise>,
+ from: string,
+ limit: number,
+ pageSize: number,
+): Promise> {
+ const added: T[] = [];
+ for (let skip = 0; skip < limit; skip += pageSize) {
+ const page = await fetchPage(skip, pageSize);
+ const at = page.content.findIndex(commit => commit.id === from);
+ if (at >= 0) {
+ added.push(...page.content.slice(0, at));
+ return {
+ type: "added",
+ commits: added,
+ merge: added.some(commit => commit.parents.length > 1),
+ };
+ }
+ added.push(...page.content);
+ if (!page.more) break;
+ }
+ return { type: "rewritten" };
+}
diff --git a/desktop/src/lib/paginatedList.svelte.ts b/desktop/src/lib/paginatedList.svelte.ts
index 863fec518..21dbf3860 100644
--- a/desktop/src/lib/paginatedList.svelte.ts
+++ b/desktop/src/lib/paginatedList.svelte.ts
@@ -63,6 +63,8 @@ export function createPaginatedList(opts: PaginatedListOptions) {
let activeKey = opts.key();
const loader = mutexExecutor.create();
+ // Rows added at the top so far; see `prepend`.
+ let prepended = 0;
const abort = async (): Promise => undefined;
// Parent reuses the view across filter changes; a key change resets the
@@ -98,6 +100,8 @@ export function createPaginatedList(opts: PaginatedListOptions) {
}
}
let superseded = false;
+ let again = false;
+ const prependedBefore = prepended;
try {
const page = await loader.run(async () =>
opts.fetchPage(all ? 0 : cursor, all ? undefined : opts.pageSize),
@@ -112,6 +116,12 @@ export function createPaginatedList(opts: PaginatedListOptions) {
return;
}
+ // Rows added at the top meanwhile would be lost by replacing the list
+ // with a page fetched from before them, so fetch again.
+ if (all && prepended !== prependedBefore) {
+ again = true;
+ return;
+ }
more = page.more;
if (all) {
items = page.content;
@@ -135,6 +145,8 @@ export function createPaginatedList(opts: PaginatedListOptions) {
if (!superseded) {
loadingMore = false;
}
+ // In `finally`, since the check above returns early.
+ if (again) await loadMore(true);
}
}
@@ -148,12 +160,19 @@ export function createPaginatedList(opts: PaginatedListOptions) {
async function revalidate(window: number = opts.pageSize): Promise {
loadingMore = true;
let superseded = false;
+ let again = false;
+ const prependedBefore = prepended;
try {
const page = await loader.run(async () => opts.fetchPage(0, window));
if (page === undefined) {
superseded = true;
return;
}
+ // As in `loadMore`: fetch again, wide enough for the rows added.
+ if (prepended !== prependedBefore) {
+ again = true;
+ return;
+ }
items = page.content;
cursor = page.content.length;
more = page.more;
@@ -166,6 +185,8 @@ export function createPaginatedList(opts: PaginatedListOptions) {
if (!superseded) {
loadingMore = false;
}
+ // In `finally`, since the check above returns early.
+ if (again) await revalidate(window + prepended - prependedBefore);
}
}
@@ -217,6 +238,19 @@ export function createPaginatedList(opts: PaginatedListOptions) {
}
}
+ // Add rows that now come before the first one, such as commits on a branch
+ // that moved on. They count towards the offset, since `fetchPage` now pages
+ // from before them; a page appended while it was in flight still lands
+ // right, as it was asked for at the offset before them, and a page that
+ // replaces the list is fetched again.
+ function prepend(newItems: T[]): void {
+ // eslint-disable-next-line svelte/prefer-svelte-reactivity -- transient local set, never rendered
+ const seen = new Set(items.map(opts.id));
+ items = [...newItems.filter(i => !seen.has(opts.id(i))), ...items];
+ cursor += newItems.length;
+ prepended += newItems.length;
+ }
+
if (staleWindow !== undefined) {
void revalidate(staleWindow);
}
@@ -271,6 +305,7 @@ export function createPaginatedList(opts: PaginatedListOptions) {
loadMore,
revalidate,
refresh,
+ prepend,
persistScroll,
};
}
diff --git a/desktop/src/views/repo/RepoCommits.svelte b/desktop/src/views/repo/RepoCommits.svelte
index 3932d13f5..9c4e0683e 100644
--- a/desktop/src/views/repo/RepoCommits.svelte
+++ b/desktop/src/views/repo/RepoCommits.svelte
@@ -9,13 +9,17 @@
import { commitRowsOf, groupCommitsByDay } from "@app/lib/commitGroups";
import { debounced } from "@app/lib/debounced.svelte";
import { invoke } from "@app/lib/invoke";
+ import { latestHead } from "@app/lib/latestHead.svelte";
+ import { newerCommits } from "@app/lib/newerCommits";
import { createPaginatedList } from "@app/lib/paginatedList.svelte";
import * as router from "@app/lib/router";
import type { SidebarData } from "@app/lib/router/definitions";
import { modifierKey } from "@app/lib/utils";
+ import Button from "@app/components/Button.svelte";
import CobCommitTeaser from "@app/components/CobCommitTeaser.svelte";
import FuzzySearch from "@app/components/FuzzySearch.svelte";
+ import Icon from "@app/components/Icon.svelte";
import RepoHeader from "@app/components/RepoHeader.svelte";
import ScrollArea from "@app/components/ScrollArea.svelte";
import SourceHeader from "@app/components/SourceHeader.svelte";
@@ -52,13 +56,21 @@
const debouncedSearch = debounced(() => searchInput, 150);
let showSearch = $state(false);
+ // The commit the list pages from: the loaded one, then each newer one the
+ // branch moves on to, whose commits are added at the top. A reload from a
+ // newer head mounts the page anew, so the loaded props never change here.
+ // svelte-ignore state_referenced_locally
+ let listHead = $state(oid);
+ // svelte-ignore state_referenced_locally
+ let headCommit = $state(commit);
+
const list = createPaginatedList({
key: () => listKey(),
page: () => commits,
fetchPage: (skip, take) =>
invoke>("list_repo_commits", {
rid: repo.rid,
- head: oid,
+ head: listHead,
skip,
take,
}),
@@ -67,6 +79,72 @@
skipPersist: () => searchInput !== "",
});
+ const latest = latestHead(() => ({ rid: repo.rid, peer, revision, oid }));
+ // A newer head that doesn't descend from the list's, such as after a force
+ // push, can't be added at the top; the list offers to start over instead.
+ let rewritten: string | undefined = $state();
+ // A head whose commits couldn't be fetched, tried again once it moves on.
+ let failed: string | undefined = $state();
+ let catchingUp = $state(false);
+
+ // Catches up with each head in turn, also one that moved on while the last
+ // catch-up ran.
+ $effect(() => {
+ const newer = latest.current;
+ if (
+ newer === undefined ||
+ catchingUp ||
+ newer === listHead ||
+ newer === rewritten ||
+ newer === failed
+ ) {
+ return;
+ }
+ catchingUp = true;
+ void addNewer(newer)
+ .catch(error => {
+ failed = newer;
+ console.error("Adding newer commits failed", error);
+ })
+ .finally(() => {
+ catchingUp = false;
+ });
+ });
+
+ // How far back to look for the list's head before taking the history as
+ // rewritten.
+ const CATCH_UP_LIMIT = 1_000;
+ const CATCH_UP_PAGE = 50;
+
+ async function addNewer(newer: string): Promise {
+ const result = await newerCommits(
+ (skip, take) =>
+ invoke>("list_repo_commits", {
+ rid: repo.rid,
+ head: newer,
+ skip,
+ take,
+ }),
+ listHead,
+ CATCH_UP_LIMIT,
+ CATCH_UP_PAGE,
+ );
+ if (result.type === "rewritten") {
+ rewritten = newer;
+ return;
+ }
+ listHead = newer;
+ headCommit = result.commits[0] ?? headCommit;
+ rewritten = undefined;
+ if (result.merge) {
+ // A merge brings in commits older than the list's head, which belong
+ // further down, so the shown rows are fetched again instead.
+ await list.revalidate(list.items.length + result.commits.length);
+ } else {
+ list.prepend(result.commits);
+ }
+ }
+
const searchableCommits = $derived(
list.items.map(c => ({
commit: c,
@@ -130,10 +208,22 @@
{repo}
{peer}
{revision}
- {oid}
- {commit}
+ oid={listHead}
+ commit={headCommit}
{baseRoute}
active="commits">
+ {#snippet notice()}
+ {#if rewritten !== undefined && rewritten === latest.current}
+
+ {/if}
+ {/snippet}
{#snippet extra()}
0}
@@ -184,6 +274,7 @@
list.loadMore()}
getKey={r =>
diff --git a/desktop/src/views/repo/RepoHome.svelte b/desktop/src/views/repo/RepoHome.svelte
index 8d9c49be0..7c0cf7903 100644
--- a/desktop/src/views/repo/RepoHome.svelte
+++ b/desktop/src/views/repo/RepoHome.svelte
@@ -5,14 +5,12 @@
import type { Blob } from "@bindings/source/Blob";
import type { Tree } from "@bindings/source/Tree";
- import { keys } from "@radicle-gui/core/keys";
- import { createQuery } from "@radicle-gui/ui/lib/query.svelte";
import { toHtml } from "hast-util-to-html";
import { capitalize } from "lodash";
import { useOverlayScrollbars } from "overlayscrollbars-svelte";
import { invoke, InvokeError } from "@app/lib/invoke";
- import { queryClient } from "@app/lib/query";
+ import { latestHead } from "@app/lib/latestHead.svelte";
import * as router from "@app/lib/router";
import type { SidebarData } from "@app/lib/router/definitions";
import { highlight } from "@app/lib/syntax";
@@ -64,33 +62,9 @@
revision,
});
- // The commit the shown revision resolves to now. A branch, or the default
- // branch without a revision, can move on while the user reads, so the page
- // keeps showing `oid` and offers the newer commit.
- const headKey = $derived(keys.head(repo.rid, peer, revision));
- // The loader has just resolved the revision, so that is the latest commit.
- // A ref update during the load may have moved it since, so then the head is
- // checked again at mount, and a push during the load still shows as newer.
- $effect.pre(() => {
- queryClient.setQueryData(headKey, oid);
- const loaded = keys.headLoaded(repo.rid, peer, revision);
- if (queryClient.getQueryState(loaded)?.isInvalidated) {
- void queryClient.invalidateQueries({ queryKey: headKey, exact: true });
- }
- });
- const head = createQuery(queryClient, () => ({
- queryKey: headKey,
- queryFn: async () =>
- (
- await invoke("repo_commit", {
- rid: repo.rid,
- peer,
- revision,
- })
- ).id,
- }));
+ const head = latestHead(() => ({ rid: repo.rid, peer, revision, oid }));
const newerCommit = $derived(
- head.current.data !== undefined && head.current.data !== oid,
+ head.current !== undefined && head.current !== oid,
);
let currentPath = $state("");
diff --git a/desktop/src/views/repo/router.ts b/desktop/src/views/repo/router.ts
index 70a1eda93..f0ed92956 100644
--- a/desktop/src/views/repo/router.ts
+++ b/desktop/src/views/repo/router.ts
@@ -445,16 +445,15 @@ export async function loadRepoHome(
export async function loadRepoCommits(
route: RepoCommitsRoute,
): Promise {
- const [context, commits] = await Promise.all([
- loadSourceContext(route),
- invoke>("list_repo_commits", {
- rid: route.rid,
- peer: route.peer,
- revision: route.revision,
- skip: 0,
- take: COMMITS_PAGE_SIZE,
- }),
- ]);
+ // The list pages from the commit the context resolved, so that the two
+ // agree even when the branch moves during the load.
+ const context = await loadSourceContext(route);
+ const commits = await invoke>("list_repo_commits", {
+ rid: route.rid,
+ head: context.oid,
+ skip: 0,
+ take: COMMITS_PAGE_SIZE,
+ });
return {
resource: "repo.commits",
diff --git a/desktop/tests/unit/newerCommits.test.ts b/desktop/tests/unit/newerCommits.test.ts
new file mode 100644
index 000000000..e6be125db
--- /dev/null
+++ b/desktop/tests/unit/newerCommits.test.ts
@@ -0,0 +1,64 @@
+import { describe, expect, test } from "vitest";
+
+import { newerCommits } from "@app/lib/newerCommits";
+
+function commit(id: string, parents = ["p"]) {
+ return { id, parents };
+}
+
+// A branch's history from its head, newest first, paged like the backend.
+function history(...commits: { id: string; parents: string[] }[]) {
+ return async (skip: number, take: number) => ({
+ cursor: skip,
+ content: commits.slice(skip, skip + take),
+ more: skip + take < commits.length,
+ });
+}
+
+describe("newerCommits", () => {
+ test("returns the commits before the old head", async () => {
+ const fetch = history(commit("c"), commit("b"), commit("a"));
+ expect(await newerCommits(fetch, "a", 100, 10)).toEqual({
+ type: "added",
+ commits: [commit("c"), commit("b")],
+ merge: false,
+ });
+ });
+
+ test("follows pages until it finds the old head", async () => {
+ const fetch = history(commit("d"), commit("c"), commit("b"), commit("a"));
+ const result = await newerCommits(fetch, "a", 100, 2);
+ expect(result).toEqual({
+ type: "added",
+ commits: [commit("d"), commit("c"), commit("b")],
+ merge: false,
+ });
+ });
+
+ test("says when a merge brought in older commits", async () => {
+ const fetch = history(commit("m", ["a", "x"]), commit("a"));
+ const result = await newerCommits(fetch, "a", 100, 10);
+ expect(result).toMatchObject({ type: "added", merge: true });
+ });
+
+ test("an unmoved head adds nothing", async () => {
+ const fetch = history(commit("a"));
+ expect(await newerCommits(fetch, "a", 100, 10)).toEqual({
+ type: "added",
+ commits: [],
+ merge: false,
+ });
+ });
+
+ test("history without the old head was rewritten", async () => {
+ const fetch = history(commit("c"), commit("b"));
+ expect(await newerCommits(fetch, "a", 100, 10)).toEqual({
+ type: "rewritten",
+ });
+ });
+
+ test("gives up past the limit", async () => {
+ const fetch = history(commit("d"), commit("c"), commit("b"), commit("a"));
+ expect(await newerCommits(fetch, "a", 2, 2)).toEqual({ type: "rewritten" });
+ });
+});
diff --git a/desktop/tests/unit/paginatedList.svelte.test.ts b/desktop/tests/unit/paginatedList.svelte.test.ts
index 5de22b9d8..f8bea3eca 100644
--- a/desktop/tests/unit/paginatedList.svelte.test.ts
+++ b/desktop/tests/unit/paginatedList.svelte.test.ts
@@ -359,6 +359,32 @@ test("a refresh started on a key change isn't undone by the list's reset", async
await vi.waitFor(() => expect(list.items).toEqual(["fresh"]));
});
+test("rows added at the top move the paging offset along", async () => {
+ const { list } = setup(page(["c", "d"]));
+ list.prepend(["a", "b"]);
+ fetchPage.mockResolvedValueOnce(page(["e"], false));
+
+ await list.loadMore();
+
+ expect(list.items).toEqual(["a", "b", "c", "d", "e"]);
+ expect(fetchPage).toHaveBeenLastCalledWith(4, 2);
+});
+
+test("a replacing load that was in flight across a prepend is fetched again", async () => {
+ const { list } = setup(page(["c", "d"]));
+ const stale = deferred();
+ fetchPage.mockReturnValueOnce(stale.promise);
+ fetchPage.mockResolvedValueOnce(page(["a", "b", "c", "d"], false));
+
+ const loading = list.loadMore(true);
+ list.prepend(["a", "b"]);
+ stale.resolve(page(["c", "d"], false));
+ await loading;
+
+ expect(list.items).toEqual(["a", "b", "c", "d"]);
+ expect(fetchPage).toHaveBeenCalledTimes(2);
+});
+
test("a refresh during a page load leaves the list able to load more", async () => {
const { list } = setup();
const pageLoad = deferred();