diff --git a/desktop/src/components/Changes.svelte b/desktop/src/components/Changes.svelte index f62c71785..9ec993931 100644 --- a/desktop/src/components/Changes.svelte +++ b/desktop/src/components/Changes.svelte @@ -31,9 +31,14 @@ } from "@app/lib/invoke"; import { anchorOf, commentCountsByPath } from "@app/lib/pierreComments"; import { isCommitListDescription } from "@app/lib/revisionDescription"; + import { + autoSelectedCommit, + lastRevisionWithChanges, + } from "@app/lib/revisionList"; import { useShortcuts } from "@app/lib/shortcuts.svelte"; import { pluralize } from "@app/lib/utils"; + import Button from "@app/components/Button.svelte"; import CobCommitTeaser from "@app/components/CobCommitTeaser.svelte"; import CommitsContainer from "@app/components/CommitsContainer.svelte"; import ExtendedTextarea from "@app/components/ExtendedTextarea.svelte"; @@ -65,6 +70,12 @@ // Where the comment stepper stands, for the tab bar to render. An index of // `-1` means it has not been stepped yet. commentPosition?: { index: number; total: number }; + // The patch's revisions in timeline order, so an empty revision can point + // back at the last one that had changes. + revisions?: Revision[]; + // The revision and commit the patch is marked merged at, if any. + merge?: { revision: string; commit: string }; + onViewRevision?: (revisionId: string) => void; } /* eslint-disable prefer-const */ @@ -86,6 +97,9 @@ // Another output binding, like `filesExpanded`: the tab bar renders the // stepper and reads this, while the stepping itself happens here. commentPosition = $bindable({ index: -1, total: 0 }), + revisions = [], + merge, + onViewRevision, }: Props = $props(); /* eslint-enable prefer-const */ @@ -295,27 +309,49 @@ selectedCommit ? selectedCommit !== commitId : false; let commitList = $state([]); + let commitListRange = $state(); $effect(() => { const ridLocal = rid; const baseRev = revision.base; const headRev = revision.head; let cancelled = false; void cachedListCommits(ridLocal, baseRev, headRev).then(c => { - if (!cancelled) commitList = c; + if (!cancelled) { + commitList = c; + commitListRange = `${baseRev}..${headRev}`; + } }); return () => { cancelled = true; }; }); - // A single-commit revision has nothing to choose between, so select that - // commit automatically to surface its message. The diff stays the - // revision's own range: a merge commit's first parent is the side it merged - // into, not the revision base, so narrowing to the commit diff there would - // show the other branch's changes instead of the patch's. + // A revision whose head is already part of its base has nothing to list or + // diff. Why that happened (a merge pushed before the patch branch, a push of + // the wrong commit, an explicit base) isn't recorded, so the empty state only + // states the fact and points at the last revision that had changes. An empty + // list alone decides it: when the head is a strict ancestor of the base, the + // tree diff between them is the base's newer commits in reverse, not the + // patch's changes. + const emptyRevision = $derived( + commitListRange === `${revision.base}..${revision.head}` && + commitList.length === 0, + ); + const earlierRevision = $derived( + lastRevisionWithChanges(revisions, revision.id), + ); + + // The diff stays the revision's own range: a merge commit's first parent is + // the side it merged into, not the revision base, so narrowing to the commit + // diff there would show the other branch's changes instead of the patch's. $effect(() => { - if (commitList.length === 1 && !selectedCommit) { - const only = commitList[0]; + const only = autoSelectedCommit( + commitList, + commitListRange, + `${revision.base}..${revision.head}`, + selectedCommit, + ); + if (only) { selectRevision({ headId: revision.head, baseId: revision.base, @@ -581,6 +617,26 @@ .stats { min-width: 0; } + .no-commits { + display: flex; + flex-direction: column; + align-items: center; + gap: 0.5rem; + margin-bottom: 1rem; + padding: 2rem 1.25rem; + border: 1px solid var(--color-border-subtle); + border-radius: var(--border-radius-md); + background-color: var(--color-surface-canvas); + color: var(--color-text-tertiary); + text-align: center; + } + .no-commits-title { + color: var(--color-text-primary); + } + .no-commits-body { + max-width: 32rem; + color: var(--color-text-secondary); + } .selected-commit-message { margin-bottom: 1rem; padding: 0.75rem; @@ -819,29 +875,59 @@ {/if} {/if} -
-
- {#if diffFailed} - - Failed to load the changes. - - {:else if stats} - {stats.filesChanged} - {pluralize("file", stats.filesChanged)} modified with - - {stats.insertions} - {pluralize("insertion", stats.insertions)} - - and - - {stats.deletions} - {pluralize("deletion", stats.deletions)} - - {:else} - Loading… + {#if emptyRevision} +
+ +
+ No changes in this revision +
+
+ {#if revision.head === revision.base} + Its head is the same commit as its base. + {:else} + Its head is already part of its base. + {/if} + {#if merge?.revision === revision.id} + The patch was marked as merged at this revision, at commit + . Because this revision has no changes, that + commit doesn't show where the patch's changes landed. + {/if} +
+ {#if earlierRevision && onViewRevision} + {@const earlier = earlierRevision} + {/if}
-
+ {:else} +
+
+ {#if diffFailed} + + Failed to load the changes. + + {:else if stats} + {stats.filesChanged} + {pluralize("file", stats.filesChanged)} modified with + + {stats.insertions} + {pluralize("insertion", stats.insertions)} + + and + + {stats.deletions} + {pluralize("deletion", stats.deletions)} + + {:else} + Loading… + {/if} +
+
+ {/if}
{#if selectedCommitData} @@ -861,7 +947,7 @@ diff --git a/desktop/src/components/Revision.svelte b/desktop/src/components/Revision.svelte index b12a1a458..672e9bf60 100644 --- a/desktop/src/components/Revision.svelte +++ b/desktop/src/components/Revision.svelte @@ -108,6 +108,8 @@ hiddenCommentSources?: string[]; filesExpanded?: boolean; onViewChanges?: (revisionId: string) => void; + // The revision and commit the patch is marked merged at, if any. + merge?: { revision: string; commit: string }; // The patch view's own header, forwarded to the Changes tab so it can render // it inside the diff's scroll content. chrome?: Snippet; @@ -136,6 +138,7 @@ hiddenCommentSources = [], filesExpanded = $bindable(true), onViewChanges, + merge, chrome, tabs, commentPosition = $bindable({ index: -1, total: 0 }), @@ -1563,6 +1566,9 @@ onSaveDescription={async (body, embeds) => { await editRevision(body, embeds, revision.id); }} + {revisions} + {merge} + onViewRevision={onViewChanges} {chrome} {tabs} bind:showingRevisionDiff diff --git a/desktop/src/lib/revisionList.ts b/desktop/src/lib/revisionList.ts index c7b1251bb..d918642d7 100644 --- a/desktop/src/lib/revisionList.ts +++ b/desktop/src/lib/revisionList.ts @@ -64,3 +64,35 @@ export function newCommitsByRevision( } return result; } + +/// The latest revision before `revisionId` that has changes of its own, with +/// its 1-based timeline number. An empty revision (head already part of its +/// base) doesn't record why it is empty, so the Changes view points at this +/// one instead of guessing what was meant. +export function lastRevisionWithChanges( + revisions: Revision[], + revisionId: string, +): { id: string; number: number } | undefined { + const index = revisions.findIndex(r => r.id === revisionId); + for (let i = index - 1; i >= 0; i--) { + if (revisions[i].base !== revisions[i].head) { + return { id: revisions[i].id, number: i + 1 }; + } + } + return undefined; +} + +/// The commit to select automatically: a single-commit revision has nothing +/// to choose between, so its only commit is selected to surface its message. +/// `listedRange` is the `base..head` the list was loaded for. Right after a +/// revision switch the list still holds the previous revision's commits, and +/// selecting from it would carry that commit over. +export function autoSelectedCommit( + commits: Commit[], + listedRange: string | undefined, + range: string, + selectedCommit: string | undefined, +): Commit | undefined { + if (listedRange !== range || selectedCommit) return undefined; + return commits.length === 1 ? commits[0] : undefined; +} diff --git a/desktop/src/views/repo/Patch.svelte b/desktop/src/views/repo/Patch.svelte index f2dcab155..e6ce06171 100644 --- a/desktop/src/views/repo/Patch.svelte +++ b/desktop/src/views/repo/Patch.svelte @@ -1412,6 +1412,7 @@ selectedRevisionId = revisionId; setView("changes"); }} + merge={patch.state.status === "merged" ? patch.state : undefined} chrome={patchHeader} {tabs} bind:showingRevisionDiff diff --git a/desktop/tests/unit/revisionList.test.ts b/desktop/tests/unit/revisionList.test.ts index 611f5ddbe..f253c3d04 100644 --- a/desktop/tests/unit/revisionList.test.ts +++ b/desktop/tests/unit/revisionList.test.ts @@ -3,6 +3,8 @@ import type { Commit } from "@bindings/repo/Commit"; import { describe, expect, test } from "vitest"; import { + autoSelectedCommit, + lastRevisionWithChanges, newCommitsByRevision, orderRevisions, revisionNumbers, @@ -119,3 +121,70 @@ test("newCommitsByRevision keeps only commits not in an earlier revision", () => ), ).toEqual({ a1: ["1", "2"], b1: ["3"], c1: ["4", "5"] }); }); + +describe("lastRevisionWithChanges", () => { + // Revisions with `base === head` are empty: their head was already part of + // their base when they were pushed. + const changed = (id: string) => revision({ id, base: "b", head: id }); + const empty = (id: string) => revision({ id, base: id, head: id }); + + test("finds the latest earlier revision with changes, numbered", () => { + const list = [changed("r1"), changed("r2"), empty("r3"), empty("r4")]; + + expect(lastRevisionWithChanges(list, "r4")).toEqual({ + id: "r2", + number: 2, + }); + }); + + test("can point at the first revision", () => { + expect(lastRevisionWithChanges([changed("r1"), empty("r2")], "r2")).toEqual( + { id: "r1", number: 1 }, + ); + }); + + test("ignores revisions after the selected one", () => { + const list = [empty("r1"), empty("r2"), changed("r3")]; + + expect(lastRevisionWithChanges(list, "r2")).toBeUndefined(); + }); + + test("has nothing to point at for the first revision", () => { + expect(lastRevisionWithChanges([empty("r1")], "r1")).toBeUndefined(); + }); + + test("has nothing to point at for an unknown revision", () => { + expect( + lastRevisionWithChanges([changed("r1"), empty("r2")], "gone"), + ).toBeUndefined(); + }); +}); + +describe("autoSelectedCommit", () => { + const c = (id: string) => ({ id }) as Commit; + + test("selects the only commit of the listed revision", () => { + expect(autoSelectedCommit([c("1")], "b..h", "b..h", undefined)?.id).toBe( + "1", + ); + }); + + // Regression: switching revisions left the previous revision's single + // commit selected, before the new revision's list had loaded. + test("doesn't select from a list loaded for another revision", () => { + expect( + autoSelectedCommit([c("1")], "b..old", "b..new", undefined), + ).toBeUndefined(); + }); + + test("keeps a selection that is already made", () => { + expect(autoSelectedCommit([c("1")], "b..h", "b..h", "1")).toBeUndefined(); + }); + + test("selects nothing when there is nothing or more than one", () => { + expect(autoSelectedCommit([], "b..h", "b..h", undefined)).toBeUndefined(); + expect( + autoSelectedCommit([c("1"), c("2")], "b..h", "b..h", undefined), + ).toBeUndefined(); + }); +});