Merge pull request #803 from MengJiapeng/fix/multi-repo-git-history
fix(git): keep history and status on the selected child repository
This commit is contained in:
5 files changed
+85
-19
No files matched your search
@@ -338,9 +338,9 @@ export function GitLens(props: GitLensProps) {
|
||||
return repoRootRef.current === undefined ? { ...base } : { ...base, repoRoot: repoRootRef.current }
|
||||
}, [])
|
||||
|
||||
// The status store keys on the checkout being LISTED: a selected child
|
||||
// repository becomes the effective cwd, which is also what makes the status
|
||||
// and the inventory agree after a repo switch.
|
||||
// Keep the session cwd and pass the child selection explicitly: an attached
|
||||
// session overrides client cwd, so changing cwd alone still reads the first
|
||||
// repository. The status store includes repoRoot in its cache key.
|
||||
// The status store keys on `worktree`; the PRIMARY checkout is exactly what
|
||||
// an unscoped status call resolves to, so passing its path would mint a
|
||||
// second key (and a second 2.5s `git status` poll) for the same answer the
|
||||
@@ -350,7 +350,7 @@ export function GitLens(props: GitLensProps) {
|
||||
? selectedWorktree
|
||||
: undefined
|
||||
const status = useGitStatus(
|
||||
{ sessionId: scope.sessionId, cwd: repoRoot ?? scope.cwd },
|
||||
{ sessionId: scope.sessionId, cwd: scope.cwd, repoRoot },
|
||||
{ worktree: statusWorktree, visible },
|
||||
)
|
||||
const snapshot: GitStatusResult | null = status.snapshot
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* (a 2 s poll inside `changes/GitLens.tsx`) while the file tree had no git
|
||||
* data at all — so a VS Code-style "color the changed rows" feature would
|
||||
* have added a SECOND poller of the same `git status --porcelain` call. The
|
||||
* store keeps one snapshot per `sessionId + cwd + worktree`, one poller per
|
||||
* store keeps one snapshot per `sessionId + cwd + repoRoot + worktree`, one poller per
|
||||
* key while at least one VISIBLE consumer is subscribed, and one path index
|
||||
* built lazily per snapshot, so the tree (coloring) and the changes page
|
||||
* (list) read the same answer.
|
||||
@@ -21,7 +21,7 @@
|
||||
* returned early, so a click during the poll silently did nothing).
|
||||
*/
|
||||
import { useCallback, useEffect, useSyncExternalStore } from 'react'
|
||||
import { api, type GitStatusResult } from '../api.ts'
|
||||
import { api, type GitStatusResult, type SessionScope } from '../api.ts'
|
||||
|
||||
/** The semantic class of one changed path (drives the row's ink). */
|
||||
export type GitTone =
|
||||
@@ -141,6 +141,7 @@ interface Slot {
|
||||
key: string
|
||||
sessionId: string
|
||||
cwd: string | undefined
|
||||
repoRoot: string | undefined
|
||||
worktree: string | undefined
|
||||
snapshot: GitStatusResult | null
|
||||
loading: boolean
|
||||
@@ -161,12 +162,13 @@ interface Slot {
|
||||
|
||||
const slots = new Map<string, Slot>()
|
||||
|
||||
function slotOf(sessionId: string, cwd: string | undefined, worktree: string | undefined): Slot {
|
||||
const key = `${sessionId}\u0000${cwd ?? ''}\u0000${worktree ?? ''}`
|
||||
function slotOf(scope: SessionScope, worktree: string | undefined): Slot {
|
||||
const { sessionId, cwd, repoRoot } = scope
|
||||
const key = `${sessionId}\u0000${cwd ?? ''}\u0000${repoRoot ?? ''}\u0000${worktree ?? ''}`
|
||||
const existing = slots.get(key)
|
||||
if (existing !== undefined) return existing
|
||||
const slot: Slot = {
|
||||
key, sessionId, cwd, worktree,
|
||||
key, sessionId, cwd, repoRoot, worktree,
|
||||
snapshot: null, loading: false, error: false, version: 0, generation: 0,
|
||||
running: false, pending: false, visible: 0, pollMs: 2_500, timer: undefined,
|
||||
listeners: new Set(),
|
||||
@@ -191,7 +193,7 @@ function fetchSlot(slot: Slot): void {
|
||||
const generation = ++slot.generation
|
||||
slot.loading = slot.snapshot === null
|
||||
notify(slot)
|
||||
const scope = { sessionId: slot.sessionId, ...(slot.cwd !== undefined ? { cwd: slot.cwd } : {}) }
|
||||
const scope: SessionScope = { sessionId: slot.sessionId, cwd: slot.cwd, repoRoot: slot.repoRoot }
|
||||
api.gitStatus(scope, slot.worktree)
|
||||
.then((snapshot) => {
|
||||
if (generation !== slot.generation) return
|
||||
@@ -295,11 +297,11 @@ export interface GitStatusView {
|
||||
* linked worktree). The poll runs while `visible` is true, at `pollMs`.
|
||||
*/
|
||||
export function useGitStatus(
|
||||
scope: { sessionId: string; cwd?: string },
|
||||
scope: SessionScope,
|
||||
options: { worktree?: string; visible?: boolean; pollMs?: number } = {},
|
||||
): GitStatusView {
|
||||
const { worktree, visible = true, pollMs = 2_500 } = options
|
||||
const slot = slotOf(scope.sessionId, scope.cwd, worktree)
|
||||
const slot = slotOf(scope, worktree)
|
||||
const subscribe = useCallback((listener: () => void) => {
|
||||
slot.listeners.add(listener)
|
||||
return () => { slot.listeners.delete(listener) }
|
||||
|
||||
+7
-2
@@ -284,13 +284,18 @@ function buildApi(
|
||||
const clientCwd = typeof record?.cwd === 'string' && record.cwd !== '' ? record.cwd : undefined
|
||||
return { sessionId, cwd: await sessionCwdOf(ctx, sessionId, clientCwd) }
|
||||
}
|
||||
/** Resolve the optional Git-panel checkout selector against the authoritative
|
||||
/** Resolve the optional Git-panel checkout selector against the selected
|
||||
* session repository. Unlike `cwd`, `worktree` is never trusted directly. */
|
||||
const gitCwdOf = async (payload: unknown): Promise<{ sessionId: string; cwd: string }> => {
|
||||
const base = await cwdOf(payload)
|
||||
const record = payload as { worktree?: unknown } | null
|
||||
const requested = typeof record?.worktree === 'string' && record.worktree !== '' ? record.worktree : undefined
|
||||
return { sessionId: base.sessionId, cwd: await git.resolveWorktree(base.cwd, requested) }
|
||||
if (requested === undefined) return base
|
||||
// A container workspace is not itself a repository. Validate the checkout
|
||||
// against the selected child (or the default discovered repository), just
|
||||
// as the inventory does, before accepting the requested worktree path.
|
||||
const root = await git.repoRoot(base.cwd, selectedRepoOf(payload))
|
||||
return { sessionId: base.sessionId, cwd: await git.resolveWorktree(root, requested) }
|
||||
}
|
||||
// Subagent live previews: one batch request instead of N per-child
|
||||
// `subagents.history` calls. The route degrades to a 503 when the host
|
||||
|
||||
@@ -197,9 +197,10 @@ describe('GitLens (changes tab, git lens) linked-worktree consistency', () => {
|
||||
scope.repoRoot === REPO_B ? repoBWorktrees : []
|
||||
))
|
||||
const status = vi.spyOn(api, 'gitStatus').mockImplementation(async (scope) => (
|
||||
scope.cwd === REPO_B
|
||||
? { isRepo: true, branch: 'b-main', entries: [], root: REPO_B, repositories: [REPO_B] }
|
||||
: { isRepo: true, branch: 'a-main', entries: [], root: REPO_A, repositories: [REPO_A, REPO_B] }
|
||||
// Attached sessions ignore the client's cwd override; repoRoot selects the child.
|
||||
scope.repoRoot === REPO_B
|
||||
? { isRepo: true, branch: 'b-main', entries: [{ path: 'b-change.ts', xy: ' M' }], root: REPO_B, repositories: [REPO_B] }
|
||||
: { isRepo: true, branch: 'a-main', entries: [{ path: 'a-change.ts', xy: ' M' }], root: REPO_A, repositories: [REPO_A, REPO_B] }
|
||||
))
|
||||
vi.spyOn(api, 'gitBranch').mockImplementation(async (scope) => (
|
||||
scope.repoRoot === REPO_B ? { current: 'b-main', names: ['b-main'] } : { current: 'a-main', names: ['a-main'] }
|
||||
@@ -231,9 +232,21 @@ describe('GitLens (changes tab, git lens) linked-worktree consistency', () => {
|
||||
// Exactly ONE full refresh for the switch (not a burst).
|
||||
expect(worktrees.mock.calls.length).toBe(listingsBefore + 1)
|
||||
// The shared status follows the same selection.
|
||||
expect(status.mock.calls.some(([scope]) => scope.cwd === REPO_B)).toBe(true)
|
||||
expect(status.mock.calls.at(-1)![0]).toMatchObject({ sessionId: 'session', cwd: WS, repoRoot: REPO_B })
|
||||
expect(container.querySelector(`[title="${t('branch')}: b-main"]`)).not.toBeNull()
|
||||
expect(container.querySelector('[data-path="b-change.ts"]')).not.toBeNull()
|
||||
expect(container.querySelector('[data-path="a-change.ts"]')).toBeNull()
|
||||
const worktreeSelect = container.querySelectorAll<HTMLSelectElement>('select')[1]!
|
||||
expect(worktreeSelect.value).toBe(REPO_B)
|
||||
|
||||
await act(async () => {
|
||||
repoSelect.value = REPO_A
|
||||
repoSelect.dispatchEvent(new Event('change', { bubbles: true }))
|
||||
})
|
||||
await flushEffects()
|
||||
expect(container.querySelector(`[title="${t('branch')}: a-main"]`)).not.toBeNull()
|
||||
expect(container.querySelector('[data-path="a-change.ts"]')).not.toBeNull()
|
||||
expect(container.querySelector('[data-path="b-change.ts"]')).toBeNull()
|
||||
} finally {
|
||||
act(() => { root.unmount() })
|
||||
container.remove()
|
||||
|
||||
+47
-1
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { spawnSync } from 'node:child_process'
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
|
||||
import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
|
||||
import { tmpdir, homedir } from 'node:os'
|
||||
import { dirname, join, resolve as resolvePath } from 'node:path'
|
||||
import { SettingsConflictError, type SettingsNamespace } from '@deepseek-ai/dsh-settings'
|
||||
@@ -461,6 +461,52 @@ describe('session cwd resolution over the API route', () => {
|
||||
return out
|
||||
}
|
||||
|
||||
it('loads branches and history when switching child repositories and their linked checkouts', async () => {
|
||||
const scratch = realpathSync.native(mkdtempSync(join(tmpdir(), 'sidebar-multi-repo-')))
|
||||
const cwd = join(scratch, 'workspace')
|
||||
mkdirSync(cwd)
|
||||
const run = (dir: string, args: string[]): void => {
|
||||
const result = spawnSync('git', ['-C', dir, '-c', 'user.name=Test', '-c', 'user.email=test@dsh.invalid', ...args], { encoding: 'utf8' })
|
||||
if (result.status !== 0) throw new Error(result.stderr)
|
||||
}
|
||||
const a = join(cwd, 'a')
|
||||
const b = join(cwd, 'b')
|
||||
const linked = join(scratch, 'b-linked')
|
||||
const unrelated = join(scratch, 'unrelated')
|
||||
try {
|
||||
for (const [path, branch] of [[a, 'a-main'], [b, 'b-main'], [unrelated, 'other']]) {
|
||||
run(scratch, ['init', '-q', path!])
|
||||
run(path!, ['checkout', '-q', '-b', branch!])
|
||||
run(path!, ['commit', '-q', '--allow-empty', '-m', branch!])
|
||||
}
|
||||
run(b, ['worktree', 'add', '-q', '-b', 'b-linked', linked])
|
||||
const route = mount({ sessions: { get: () => ({ header: { cwd } }) } })
|
||||
const base = { sessionId: 'multi', cwd }
|
||||
expect(await invoke(route, 'git.log', base)).toMatchObject({ ok: true, value: [{ subject: 'a-main' }] })
|
||||
// Match the UI: inventory first, then both repoRoot and worktree on
|
||||
// derived requests. Switching back used to fail just like switching away.
|
||||
for (const [repoRoot, worktree, branch, subject] of [
|
||||
[b, b, 'b-main', 'b-main'],
|
||||
[a, a, 'a-main', 'a-main'],
|
||||
[b, linked, 'b-linked', 'b-main'],
|
||||
]) {
|
||||
expect(await invoke(route, 'git.worktrees', { ...base, repoRoot })).toMatchObject({ ok: true })
|
||||
const payload = { ...base, repoRoot, worktree }
|
||||
// Primary status omits worktree, but must still select the child.
|
||||
const statusPayload = worktree === repoRoot ? { ...base, repoRoot } : payload
|
||||
expect(await invoke(route, 'git.status', statusPayload)).toMatchObject({ ok: true, value: { branch } })
|
||||
expect(await invoke(route, 'git.branch', payload)).toMatchObject({ ok: true, value: { current: branch } })
|
||||
expect(await invoke(route, 'git.log', { ...payload, count: 1, skip: 0 })).toMatchObject({ ok: true, value: [{ subject }] })
|
||||
expect(await invoke(route, 'git.log', { ...payload, count: 1, skip: 1 })).toMatchObject({ ok: true, value: [] })
|
||||
}
|
||||
for (const worktree of [a, unrelated]) {
|
||||
expect(await invoke(route, 'git.log', { ...base, repoRoot: b, worktree })).toMatchObject({ ok: false })
|
||||
}
|
||||
} finally {
|
||||
rmSync(scratch, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('uses the client summary cwd while the session is detached', async () => {
|
||||
const route = mount()
|
||||
const result = await invoke(route, 'session.cwd', { sessionId: 's-detached', cwd: '/tmp/summary-cwd' })
|
||||
|
||||
Reference in new issue
Block a user