From 8d269309622ed2eb57bffd235f2cf182481e28b0 Mon Sep 17 00:00:00 2001 From: ai-zen-future <154737176+ai-zen-future@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:18:50 +0800 Subject: [PATCH] perf(git): index loaded commits during graph pagination Refs #1063 (item 5) --- CHANGELOG.md | 2 ++ CHANGELOG_EN.md | 2 ++ src/client/changes/GitGraphSection.tsx | 12 ++++++--- tests/git-graph-ui.spec.tsx | 34 ++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58cccb1c..6123e0ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### 未发布 +- **减少 Git 提交图加载更多历史时的去重开销**([#1063](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1063) 第 5 项)。追加一页时按完整提交哈希为已加载历史建立集合,再按原顺序追加新记录。整页重复时也会继续推进分页游标。 + - 🧷 **Git 详情面板的关闭按钮常驻在 Tabs 同一行的最右侧**([#991](https://github.com/omdsh-dev/DSH-better-sidebar/issues/991)):底部面板的「提交详情 / 内容 / 差异」是一条 Tabs,但关闭整块面板的 X 原先画在「内容 / 差异」的页内头行上——切到「提交详情」时它整条头行都不渲染,于是那个页签根本没有关闭入口,只能先切页签再点 X。现在关闭按钮属于 Tabs 那一行:无论选中哪个页签都停在最右端、位置不变,点击关闭的是整块面板;「内容 / 差异」头行里那个重复的 X 已移除。只有一个视图、没有 Tabs 行的预览仍保持原来的头行位置。 - 🧪 测试:`tests/changes-tab.spec.tsx` 逐个页签断言面板里**只有一个**关闭按钮、它挂在 Tabs 那一行且排在 tablist 之后(行尾),并从「提交详情」页签点击它关闭整块面板。 diff --git a/CHANGELOG_EN.md b/CHANGELOG_EN.md index 14c5d425..e436fd86 100644 --- a/CHANGELOG_EN.md +++ b/CHANGELOG_EN.md @@ -6,6 +6,8 @@ ### Unreleased +- **Reduce Git Graph deduplication work when loading more history** ([#1063](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1063), item 5). Each append indexes the full hashes of loaded commits before filtering the incoming page. New commits keep their page order, and duplicate-only pages still advance the cursor. + - 🧷 **The Git details pane's close button now sits permanently at the right end of the tab row** ([#991](https://github.com/omdsh-dev/DSH-better-sidebar/issues/991)): the bottom pane's "commit details / content / diff" strip is one tab row, but the X that closes the whole pane was drawn inside the content/diff head — a row that does not render at all on the details tab, so that tab had no exit and closing meant switching views first. The button now belongs to the tab row: it stays at the right end on every tab, never moves, and closes the whole pane; the duplicate X inside the content/diff head is gone. A single-view preview with no tab row keeps its previous head placement. - 🧪 Tests: `tests/changes-tab.spec.tsx` asserts, per tab, that the pane holds exactly one close button, that it rides the tab row after the tablist (the row's right end), and that clicking it from the details tab closes the whole pane. diff --git a/src/client/changes/GitGraphSection.tsx b/src/client/changes/GitGraphSection.tsx index 911bbcb1..da52016c 100644 --- a/src/client/changes/GitGraphSection.tsx +++ b/src/client/changes/GitGraphSection.tsx @@ -245,10 +245,14 @@ export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onP } if (controller.signal.aborted || gen !== generation.current) return if (!append) setLinearFallback(fallback) - const next = append && previous ? { - ...result, - entries: [...previous.entries, ...result.entries.filter(entry => !previous.entries.some(old => old.hashFull === entry.hashFull))], - } : result + let next = result + if (append && previous) { + const previousHashes = new Set(previous.entries.map(entry => entry.hashFull)) + next = { + ...result, + entries: [...previous.entries, ...result.entries.filter(entry => !previousHashes.has(entry.hashFull))], + } + } pageRef.current = next setPage(next) setBranches(next.branches) diff --git a/tests/git-graph-ui.spec.tsx b/tests/git-graph-ui.spec.tsx index 8da3fbbd..9e2b8b60 100644 --- a/tests/git-graph-ui.spec.tsx +++ b/tests/git-graph-ui.spec.tsx @@ -527,6 +527,40 @@ describe('graph end-sentinel auto-load', () => { expect(mounted.container.textContent).toContain('subject b') } finally { mounted.unmount(); io.restore() } }) + it('deduplicates across pages by full hash and advances past duplicate-only pages', async () => { + const original = entry(HASH_A) + const repeated = { ...original, subject: 'Repeated commit from a later page' } + const sameShortHash = entry(`${original.hash}${'2'.repeat(33)}`) + const graph = vi.spyOn(api, 'gitGraph') + .mockResolvedValueOnce({ ...graphPage([original]), hasMore: true, nextCursor: 'cursor-2' }) + .mockResolvedValueOnce({ ...graphPage([repeated, sameShortHash, entry(HASH_B)]), hasMore: true, nextCursor: 'cursor-3' }) + .mockResolvedValueOnce({ ...graphPage([sameShortHash, repeated]), hasMore: true, nextCursor: 'cursor-4' }) + .mockResolvedValueOnce(graphPage([repeated])) + const io = installIntersectionObserver() + const mounted = renderRoot(createElement(GitGraphSection, defaults)) + const rowHashes = () => [...mounted.container.querySelectorAll('[data-graph-commit]')] + .map(row => row.dataset.graphCommit) + try { + await flush() + await intersect(io.observers.at(-1)!) + expect(rowHashes()).toEqual([HASH_A, sameShortHash.hashFull, HASH_B]) + expect(mounted.container.textContent).toContain(original.subject) + expect(mounted.container.textContent).not.toContain(repeated.subject) + + const duplicatePageObserver = io.observers.at(-1)! + await intersect(duplicatePageObserver) + expect(rowHashes()).toEqual([HASH_A, sameShortHash.hashFull, HASH_B]) + expect(mounted.container.querySelector('[data-graph-sentinel]')).not.toBeNull() + expect(duplicatePageObserver.disconnected).toBe(true) + const finalPageObserver = io.observers.at(-1)! + expect(finalPageObserver).not.toBe(duplicatePageObserver) + + await intersect(finalPageObserver) + expect(graph.mock.calls.map(call => call[1]?.cursor)).toEqual([undefined, 'cursor-2', 'cursor-3', 'cursor-4']) + expect(rowHashes()).toEqual([HASH_A, sameShortHash.hashFull, HASH_B]) + expect(mounted.container.querySelector('[data-graph-sentinel]')).toBeNull() + } finally { mounted.unmount(); io.restore() } + }) it('ignores a second intersection while an append is already in flight', async () => { let resolveAppend!: (value: ReturnType) => void const graph = vi.spyOn(api, 'gitGraph').mockResolvedValueOnce(page('a', true)).mockImplementationOnce(() => new Promise(yes => { resolveAppend = yes })).mockResolvedValue(page('c'))