✨ feat(vscode): 代码阅读预览按侧栏筛选算代码差异(未提交的更改对比上一次提交、审查对比 PR 目标分支;兄弟块按名字 / 相似度对齐,卡片内按行 / 值 / 词比对),算好后补发给预览

This commit is contained in:
cruldra committed 2026-09-30 08:35:17 +08:00
1 parent 3db44f84ae
commit 2a5806c6b5
23 files changed
+1527 -31

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { fileAtRef } from './fileAt'
import { runGit } from './worktree'
describe('fileAtRef', () => {
let repo: string
let head: string
beforeAll(async () => {
repo = mkdtempSync(path.join(os.tmpdir(), 'file-at-'))
await runGit(repo, ['init', '-q'])
mkdirSync(path.join(repo, 'src'))
writeFileSync(path.join(repo, 'src', 'a.py'), 'x = 1\n')
await runGit(repo, ['add', '.'])
await runGit(repo, ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-q', '-m', 'init'])
head = (await runGit(repo, ['rev-parse', 'HEAD'])).trim()
writeFileSync(path.join(repo, 'src', 'a.py'), 'x = 2\n')
writeFileSync(path.join(repo, 'src', 'b.py'), 'y = 1\n')
})
afterAll(() => {
rmSync(repo, { recursive: true, force: true })
})
it('取提交里的内容,不是工作区里改过的', async () => {
expect(await fileAtRef(repo, head, 'src/a.py')).toBe('x = 1\n')
})
it('提交里没有这个文件是 null', async () => {
expect(await fileAtRef(repo, head, 'src/b.py')).toBeNull()
})
it('提交不存在时抛错', async () => {
await expect(fileAtRef(repo, '0123456789abcdef0123456789abcdef01234567', 'src/a.py')).rejects.toThrow()
})
})
+12
View File
@@ -0,0 +1,12 @@
import { runGit } from './worktree'
/**
* 仓库里 ref 这个提交上 path 这个文件的内容(path 相对仓库根、`/` 分隔);那个提交里没有这个文件时是 null。
* 先 ls-tree 看有没有、再 show 取内容,这样「没有这个文件」和 git 出错分得开(出错照常抛)。
*/
export async function fileAtRef(repoRoot: string, ref: string, path: string): Promise<string | null> {
const listed = await runGit(repoRoot, ['ls-tree', '--name-only', ref, '--', path], `查 ${ref} 上有没有 ${path}`)
if (!listed.trim())
return null
return runGit(repoRoot, ['show', `${ref}:${path}`], `读 ${ref} 上的 ${path}`)
}
+7
View File
@@ -36,7 +36,14 @@ export interface GitChange {
readonly status: GitStatus
}
/** 当前检出的分支或提交;commit 是它指向的提交,仓库还没有提交时没有。 */
export interface GitHead {
readonly commit?: string
}
export interface GitRepositoryState {
/** 仓库还没打开完时没有 */
readonly HEAD: GitHead | undefined
/** 合并冲突的 */
readonly mergeChanges: readonly GitChange[]
/** 暂存的 */