Files
apes-sidebar/tests/git-worktree.spec.ts
T
unsc9 bdf3c33865 fix(changes): 变更面板两侧行数不再相加,各分组各读自己那一侧(#131)
同一文件同时有已暂存与未暂存改动(porcelain 'MM')时,status() 把
`git diff --cached --numstat`(index 对 HEAD)与 `git diff --numstat`
(worktree 对 index)按路径相加成一个数。两次 diff 的基线不同,相加会把
两者都看到的那几行算两遍:改写一行并暂存、再改同一行,两组各自显示
`+2 −2`,真值只有 `+1 −1`;面板里该文件出现两次,于是同一个虚高值被印
两遍、也被两个分组头各合计一遍。原注释写的「两侧都是对 HEAD 量的」前提
不成立——不带 --cached 的 git diff 量的是 worktree 对 index。

改为让两侧各自保留读数,一路传到分组:

- src/git.ts / src/client/api.ts:`counts` 拆成 `staged` / `unstaged`,
  各查各的 numstat 表;删掉 addLineCounts
- src/client/changes/change-tree.ts:`buildChangeTree(entries, side)`
  按分组取自己那一侧,叶子上的 `counts` 因此只代表所在分组
- src/client/changes/GitLens.tsx:两个分组各传自己的 side;内容键改成
  同时含两侧,否则「只动了一侧的行数」不会重建树
- src/client/ui/git-status.ts:快照比较逐侧比,语义与原来一致

用例:`tests/git.spec.ts` 新增真实仓库用例钉住 `MM` 的两侧读数并断言不再
出现相加值(原用例正好钉着旧行为,一并改);`tests/change-tree-counts.spec.ts`
与 `tests/git-view-worktree.spec.tsx` 钉住每个分组只读自己那一侧。
2026-10-10 20:07:32 +08:00

105 lines
4.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { spawnSync } from 'node:child_process'
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { parseWorktreeList, resolveWorktree, status, worktrees } from '../src/git.ts'
const IDENTITY = {
GIT_AUTHOR_NAME: 'dsh-better-sidebar-test',
GIT_AUTHOR_EMAIL: 'test@dsh.invalid',
GIT_COMMITTER_NAME: 'dsh-better-sidebar-test',
GIT_COMMITTER_EMAIL: 'test@dsh.invalid',
}
function git(cwd: string, args: string[]): string {
const result = spawnSync('git', ['-C', cwd, ...args], {
encoding: 'utf8',
env: { ...process.env, ...IDENTITY },
})
if (result.status !== 0) throw new Error(result.stderr || `git ${args.join(' ')} failed`)
return result.stdout
}
describe('linked Git worktrees', () => {
it('parses porcelain records with spaces in checkout paths', () => {
expect(parseWorktreeList([
'worktree C:/repo/main checkout',
'HEAD abc',
'branch refs/heads/main',
'',
'worktree C:/repo/agent checkout',
'HEAD def',
'detached',
'',
].join('\n'))).toEqual([
{ path: 'C:/repo/main checkout', branch: 'main', locked: false, prunable: false },
{ path: 'C:/repo/agent checkout', branch: 'HEAD', locked: false, prunable: false },
])
})
it('preserves locked metadata and marks stale records prunable', () => {
expect(parseWorktreeList([
'worktree C:/repo/locked',
'HEAD abc',
'branch refs/heads/locked',
'locked in use',
'',
'worktree C:/repo/missing',
'HEAD def',
'prunable gitdir file points to non-existent location',
'',
].join('\0'))).toEqual([
{ path: 'C:/repo/locked', branch: 'locked', locked: true, prunable: false },
{ path: 'C:/repo/missing', branch: 'HEAD', locked: false, prunable: true },
])
})
it('discovers dirty linked checkouts and fences selected targets', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-worktrees-'))
const main = join(root, 'main')
const agent = join(root, 'agent worktree')
try {
git(root, ['init', '-q', main])
git(main, ['checkout', '-q', '-b', 'main'])
writeFileSync(join(main, 'tracked.txt'), 'base\n')
git(main, ['add', '-A'])
git(main, ['commit', '-q', '-m', 'base'])
git(main, ['worktree', 'add', '-q', '-b', 'agent', agent])
writeFileSync(join(agent, 'tracked.txt'), 'changed by agent\n')
// macOS tmpdir() keeps the /var symlink while git reports the resolved
// /private/var prefix; on Windows TEMP is often the 8.3 short form
// (C:\Users\RUNNER~1\...) while git reports the long path. The NATIVE
// realpath resolves both aliasings (libuv's GetFinalPathNameByHandle
// expands 8.3 names) — canonicalize every path handed to git APIs.
const agentPath = realpathSync.native(agent)
const listed = await worktrees(main)
expect(listed).toHaveLength(2)
expect(listed.find(entry => entry.current)).toMatchObject({ branch: 'main', changes: 0 })
expect(listed.find(entry => !entry.current)).toMatchObject({ branch: 'agent', changes: 1 })
expect(resolve(await resolveWorktree(main, agentPath))).toBe(resolve(agentPath))
// The line counts ride the same status answer (#131): 'base' →
// 'changed by agent' is exactly one line replaced, on the worktree side
// (nothing is staged here, so the index side has no reading at all).
expect((await status(await resolveWorktree(main, agentPath))).entries).toEqual([
{ path: 'tracked.txt', xy: ' M', unstaged: { additions: 1, deletions: 1 } },
])
await expect(resolveWorktree(main, root)).rejects.toThrow('unknown linked worktree')
// Git retains administrative metadata after a checkout directory is
// deleted, reporting the record as prunable. It must disappear from both
// the selector inventory and the accepted command-target allowlist.
rmSync(agent, { recursive: true, force: true })
const remaining = await worktrees(main)
expect(remaining).toHaveLength(1)
expect(resolve(remaining[0]!.path)).toBe(resolve(realpathSync.native(main)))
expect(remaining[0]!.current).toBe(true)
await expect(resolveWorktree(main, agentPath)).rejects.toThrow('unknown linked worktree')
} finally {
try { git(main, ['worktree', 'remove', '--force', agent]) } catch { /* fixture may not be fully initialized */ }
rmSync(root, { recursive: true, force: true })
}
})
})