dsh-remote-vs 0.27.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +110 -19
- package/README.md +82 -19
- package/lib/client.js +6789 -6607
- package/lib/index.js +546 -16
- package/lib/types/changes/recorder.d.ts +119 -2
- package/lib/types/client/CodeEditor.d.ts +15 -0
- package/lib/types/client/api.d.ts +17 -1
- package/lib/types/client/changes/apply-failure.d.ts +41 -0
- package/lib/types/client/changes/block-confirm.d.ts +97 -0
- package/lib/types/client/changes/diff-outcome.d.ts +56 -0
- package/lib/types/client/changes/freshness.d.ts +51 -0
- package/lib/types/client/changes/model.d.ts +19 -1
- package/lib/types/client/changes/session-diff.d.ts +100 -0
- package/lib/types/client/changes/staleness.d.ts +98 -0
- package/lib/types/client/changes/turn-reverted.d.ts +82 -0
- package/lib/types/client/changes/turn-view.d.ts +131 -0
- package/lib/types/client/changes/undo-affordance.d.ts +44 -10
- package/lib/types/client/changes/undo-target.d.ts +12 -0
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/store.d.ts +91 -1
- package/lib/types/client/vsc/change-marks.d.ts +21 -0
- package/lib/types/protocol.d.ts +99 -1
- package/package.json +1 -1
- package/src/changes/recorder.ts +851 -8
- package/src/client/CodeEditor.tsx +43 -3
- package/src/client/EditorPane.tsx +64 -7
- package/src/client/api.ts +30 -3
- package/src/client/changes/ChangesDock.tsx +539 -54
- package/src/client/changes/apply-failure.ts +73 -0
- package/src/client/changes/block-confirm.ts +180 -0
- package/src/client/changes/changes.css +45 -0
- package/src/client/changes/diff-outcome.ts +97 -0
- package/src/client/changes/freshness.ts +68 -0
- package/src/client/changes/model.ts +56 -1
- package/src/client/changes/session-diff.ts +187 -0
- package/src/client/changes/staleness.ts +148 -0
- package/src/client/changes/turn-reverted.ts +254 -0
- package/src/client/changes/turn-view.ts +195 -0
- package/src/client/changes/undo-affordance.ts +295 -63
- package/src/client/changes/undo-target.ts +12 -0
- package/src/client/i18n.ts +102 -4
- package/src/client/scm/DiffView.tsx +32 -13
- package/src/client/store.tsx +392 -16
- package/src/client/vsc/change-marks.ts +21 -0
- package/src/client/vsc/preview-document.tsx +4 -1
- package/src/client/vsc/vsc.css +6642 -6509
- package/src/protocol.ts +101 -1
- package/src/routes.ts +20 -9
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 撤销失败时说人话:把 `git apply` 的 stderr 归成几种**可操作**的原因。
|
|
3
|
+
*
|
|
4
|
+
* 这条路原来只有一句「这处改动的上下文已经变了」。可实际失败原因不止一种,而且
|
|
5
|
+
* 读法完全不同:
|
|
6
|
+
* - 上下文真的变了 → 重新点该行重算标记,再看一遍;
|
|
7
|
+
* - 文件已经不在仓库里 → 打开/重命名过,去列表里重新找;
|
|
8
|
+
* - 权限 / 只读文件系统 → 远端那头的问题,换条路(终端 / 另一个写入者);
|
|
9
|
+
* - 仓库所有权不对(git 的 `dubious ownership`)→ 去配 `safe.directory` 或修所有权,
|
|
10
|
+
* 换仓库目录解决不了它;
|
|
11
|
+
* - 补丁本身坏了(截断 / 手改)→ 重新取一次改动内容;
|
|
12
|
+
* - 其它 → 至少把 git 的原话留在后面。
|
|
13
|
+
*
|
|
14
|
+
* 样本来自 `git apply` 的真实 stderr 形状(git 2.55 / apply.c 与 setup.c 的消息;
|
|
15
|
+
* 2026-11-26 审核用真远端 git 复跑过):`patch failed: … / patch does not apply`、
|
|
16
|
+
* `already exists in working directory`、`No such file or directory`、
|
|
17
|
+
* `does not exist in index`、`Permission denied`、`unable to write new index file`、
|
|
18
|
+
* `detected dubious ownership in repository at …`、`corrupt patch at line N`、
|
|
19
|
+
* `patch fragment without header at line N`、`patch with only garbage at <file>:<line>`。
|
|
20
|
+
*
|
|
21
|
+
* **没有"不是 git 仓库"这一档**:`git apply` 不要求当前目录是仓库,跑在非仓库目录
|
|
22
|
+
* 得到的仍是文件级错误(实测是 `No such file or directory`,归 path-missing);
|
|
23
|
+
* 把那种情形报成"重选仓库目录"会把人引到错误的修法上(审核 S3)。
|
|
24
|
+
*
|
|
25
|
+
* 纯函数 + 一张样本表,`scripts/check-apply-failure.mjs` 用真实 stderr 驱动。
|
|
26
|
+
* 传输失败(socket/超时)不在这里:那是链路问题,调用方先用 `isConnectionFailure`
|
|
27
|
+
* 拦掉,报成"补丁上下文变了"会把人引到错误的修法上。
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/** 撤销失败的原因分类。 */
|
|
31
|
+
export type ApplyFailureKind = 'context-moved' | 'path-missing' | 'permission' | 'ownership' | 'corrupt' | 'other'
|
|
32
|
+
|
|
33
|
+
/** 每种分类对应的字典键(zh + en 见 `i18n.ts`)。 */
|
|
34
|
+
const FAILURE_KEY: Record<ApplyFailureKind, string> = {
|
|
35
|
+
'context-moved': 'changes.applyContextMoved',
|
|
36
|
+
'path-missing': 'changes.applyPathMissing',
|
|
37
|
+
permission: 'changes.applyPermission',
|
|
38
|
+
ownership: 'changes.applyOwnership',
|
|
39
|
+
corrupt: 'changes.applyCorrupt',
|
|
40
|
+
other: 'changes.applyOther',
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 一份 stderr 属于哪类原因。 */
|
|
44
|
+
export function classifyApplyFailure(detail: string): ApplyFailureKind {
|
|
45
|
+
const text = detail.toLowerCase()
|
|
46
|
+
// 顺序承重:所有权问题是 git 启动阶段就拒绝的(后面常跟着一堆补丁错误),
|
|
47
|
+
// 必须先认出来,否则会被认成路径/上下文问题。
|
|
48
|
+
if (/dubious ownership|safe\.directory|owner of the repository|repository at .* is owned by/.test(text)) return 'ownership'
|
|
49
|
+
if (/permission denied|operation not permitted|read-only file system|unable to write|unable to create file|eacces|eperm/.test(text)) return 'permission'
|
|
50
|
+
if (/no such file or directory|does not exist in index|did not match any file/.test(text)) return 'path-missing'
|
|
51
|
+
if (/corrupt patch|patch fragment without header|patch with only garbage|malformed patch|unrecognized input|bad hunk header/.test(text)) return 'corrupt'
|
|
52
|
+
if (/patch does not apply|patch failed|already exists in working directory|does not match index|while searching for/.test(text)) {
|
|
53
|
+
return 'context-moved'
|
|
54
|
+
}
|
|
55
|
+
return 'other'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 一份 stderr 对应的字典键。 */
|
|
59
|
+
export function applyFailureKey(detail: string): string {
|
|
60
|
+
return FAILURE_KEY[classifyApplyFailure(detail)]
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 失败提示的正文:分类那句话 + git 的原话(次要信息)。
|
|
65
|
+
*
|
|
66
|
+
* 原话一定留在后面:分类是我们对 stderr 的读法,读错了用户还能从原文看出来。
|
|
67
|
+
* `detail` 为空时返回 `undefined`,由调用方退回既有的兜底文案。
|
|
68
|
+
*/
|
|
69
|
+
export function applyFailureText(detail: string, t: (key: string) => string): string | undefined {
|
|
70
|
+
const text = detail.trim()
|
|
71
|
+
if (text === '') return undefined
|
|
72
|
+
return `${t(applyFailureKey(text))} · ${text}`
|
|
73
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 编辑器里「这处改动我看过,保留」的块级本地笔记。
|
|
3
|
+
*
|
|
4
|
+
* 面板那一行上的 ✓ 是**文件级**人工标记(`review-state.ts`),回答的是"这个文件我
|
|
5
|
+
* 审过了"。但一处三十行的重构里,真正需要收敛的是"这**一块**我看过了、同意保留" ——
|
|
6
|
+
* 文件级 ✓ 要么在看完第一块时就被打上,要么看到最后才敢打。
|
|
7
|
+
*
|
|
8
|
+
* 所以这里补第二级:鼠标停在改动行上弹出的那条悬浮栏里,除了「撤销这处改动」,还有
|
|
9
|
+
* 一个切换式的「确认保留」。它**什么也不写**:不写远端、不发请求、不动 diff 数据
|
|
10
|
+
* (撤销是远端 `git apply -R`,确认只是给自己留个记号),状态存在 `localStorage`,
|
|
11
|
+
* 按会话分开 —— 它描述的是"我读到哪儿了",不是远端的任何事实。
|
|
12
|
+
*
|
|
13
|
+
* 块的身份用 `${远端绝对路径}|${oldStart}:${oldLines}:${newStart}:${newLines}`:行号
|
|
14
|
+
* 会随远端改动漂移,而 hunk 头的四个数字 + 文件的远端绝对路径才代表"同一处改动"
|
|
15
|
+
* (用绝对路径而不是会话相对的显示路径:两个仓库子目录可以给出同一个显示路径)。
|
|
16
|
+
* 文件被重新统计后 hunk 头变了,旧笔记自然不再命中 —— 这正是想要的(内容变过,值得
|
|
17
|
+
* 再看一眼),而且不需要监听任何事件。
|
|
18
|
+
*
|
|
19
|
+
* **已知限制(审核 N3)**:身份里没有**轴**也没有轮次号。同一文件的同一坐标若在
|
|
20
|
+
* 「会话累计」与「本轮」两份数据里都出现(例如同一轮就是首次改动、两份 hunk 头恰好
|
|
21
|
+
* 相同),那一处块确认是**共用的**;同理,隔着新一轮但 hunk 头四个数字完全没变的
|
|
22
|
+
* 另一处改动也会沿用旧笔记。不把轴/轮号并进身份是刻意的取舍:身份串会随轮次变化,
|
|
23
|
+
* 已确认状态在每次轮次关闭后全部失效;而"同一坐标、内容未变"沿用确认,正是这套
|
|
24
|
+
* 记号想要的收敛效果。真要区分只能连内容指纹一起入键,那需要为每块算一次哈希。
|
|
25
|
+
*
|
|
26
|
+
* 两级语义**不联动**:块确认不会自动打文件级 ✓,文件级 ✓ 也不代表每一块都点过。
|
|
27
|
+
* 纯函数 + 一个有上限的表,`scripts/check-block-confirm.mjs` 直接驱动真实判定,
|
|
28
|
+
* 并用 jsdom 里的真 CodeMirror 点一次真正的按钮。
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** 一个会话最多记住这么多块(长命页面不能无限长)。 */
|
|
32
|
+
export const BLOCK_CONFIRM_CAP = 2000
|
|
33
|
+
|
|
34
|
+
/** `localStorage` 键前缀(按会话分开;与面板的审阅标记同一套约定)。 */
|
|
35
|
+
export const BLOCK_CONFIRM_PREFIX = 'rvs.changes.confirmed.'
|
|
36
|
+
|
|
37
|
+
/** 已确认的块:键 → 1(用对象是为了能直接 JSON 往返)。 */
|
|
38
|
+
export type ConfirmedBlocks = Readonly<Record<string, 1>>
|
|
39
|
+
|
|
40
|
+
/** 只读存取的存储接口(真实实现是 `window.localStorage`;门禁用假表)。 */
|
|
41
|
+
export interface ConfirmStorage {
|
|
42
|
+
getItem(key: string): string | null
|
|
43
|
+
setItem(key: string, value: string): void
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 一个 hunk 头里参与块身份的那四个数字。 */
|
|
47
|
+
export interface BlockHeader {
|
|
48
|
+
oldStart: number
|
|
49
|
+
oldLines: number
|
|
50
|
+
newStart: number
|
|
51
|
+
newLines: number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 块标识(见文件头;字段用 `|` 与 `:` 分隔,数字不会包含这两个字符)。
|
|
56
|
+
*
|
|
57
|
+
* @param path - 文件的**远端绝对路径**(不是显示路径)。
|
|
58
|
+
* @param hunk - 这个 hunk 头里的四个数字。
|
|
59
|
+
*/
|
|
60
|
+
export function blockIdOf(path: string, hunk: BlockHeader): string {
|
|
61
|
+
return `${path}|${hunk.oldStart}:${hunk.oldLines}:${hunk.newStart}:${hunk.newLines}`
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 一处改动(`ChangeMark`)对应的块标识。
|
|
66
|
+
*
|
|
67
|
+
* `mark.hunk` 是 `ChangeSet.patches` 的下标,`ChangeSet.blocks` 与它同序,所以这里
|
|
68
|
+
* 只是换个名字;没有大括号(`blocks` 省略、或 `hunk` 缺失)时返回 `undefined` ——
|
|
69
|
+
* 调用方据此不提供「确认保留」。
|
|
70
|
+
*/
|
|
71
|
+
export function blockIdForMark(
|
|
72
|
+
blocks: readonly string[] | undefined,
|
|
73
|
+
mark: { hunk?: number },
|
|
74
|
+
): string | undefined {
|
|
75
|
+
if (blocks === undefined) return undefined
|
|
76
|
+
const index = mark.hunk
|
|
77
|
+
if (index === undefined || index < 0) return undefined
|
|
78
|
+
return blocks[index]
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 这一块是否已被确认。 */
|
|
82
|
+
export function isBlockConfirmed(blocks: ConfirmedBlocks, id: string): boolean {
|
|
83
|
+
return blocks[id] === 1
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 切换一块的确认状态,返回新表(输入不被修改;超过上限时丢掉最早的那条)。
|
|
88
|
+
*
|
|
89
|
+
* 上限按 `Object.keys` 顺序(插入顺序)丢最旧的一条:块笔记是"我最近看过的",
|
|
90
|
+
* 丢掉最早看过的那条比拒绝新笔记更符合直觉。
|
|
91
|
+
*/
|
|
92
|
+
export function toggleConfirmedBlock(blocks: ConfirmedBlocks, id: string): Record<string, 1> {
|
|
93
|
+
const next: Record<string, 1> = { ...blocks }
|
|
94
|
+
if (next[id] === 1) {
|
|
95
|
+
delete next[id]
|
|
96
|
+
return next
|
|
97
|
+
}
|
|
98
|
+
const keys = Object.keys(next)
|
|
99
|
+
if (keys.length >= BLOCK_CONFIRM_CAP) {
|
|
100
|
+
const oldest = keys[0]
|
|
101
|
+
if (oldest !== undefined) delete next[oldest]
|
|
102
|
+
}
|
|
103
|
+
next[id] = 1
|
|
104
|
+
return next
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 解析持久化的一串 JSON。
|
|
109
|
+
*
|
|
110
|
+
* 任何畸形输入都给空表而不是抛错:这份状态只是"看过没看过",读不出来最多重看一遍,
|
|
111
|
+
* 不值得让编辑器崩掉。键必须非空、值必须是 1,且受 {@link BLOCK_CONFIRM_CAP} 限制。
|
|
112
|
+
*/
|
|
113
|
+
export function parseConfirmedBlocks(raw: string | null | undefined): Record<string, 1> {
|
|
114
|
+
if (typeof raw !== 'string' || raw === '') return {}
|
|
115
|
+
let parsed: unknown
|
|
116
|
+
try {
|
|
117
|
+
parsed = JSON.parse(raw)
|
|
118
|
+
} catch {
|
|
119
|
+
return {}
|
|
120
|
+
}
|
|
121
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return {}
|
|
122
|
+
const next: Record<string, 1> = {}
|
|
123
|
+
let kept = 0
|
|
124
|
+
for (const [key, value] of Object.entries(parsed as Record<string, unknown>)) {
|
|
125
|
+
if (value !== 1 || key === '') continue
|
|
126
|
+
next[key] = 1
|
|
127
|
+
kept += 1
|
|
128
|
+
if (kept >= BLOCK_CONFIRM_CAP) break
|
|
129
|
+
}
|
|
130
|
+
return next
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** 序列化(键数量已受上限约束,所以这串不会无限长)。 */
|
|
134
|
+
export function serializeConfirmedBlocks(blocks: ConfirmedBlocks): string {
|
|
135
|
+
return JSON.stringify(blocks)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 这一会话的存储键。 */
|
|
139
|
+
export function confirmedStorageKey(sessionId: string): string {
|
|
140
|
+
return `${BLOCK_CONFIRM_PREFIX}${sessionId}`
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 读一个会话的块笔记(存储拒绝回答时给空表:私有模式最多是不持久化)。 */
|
|
144
|
+
export function loadConfirmedBlocks(storage: ConfirmStorage, sessionId: string): Record<string, 1> {
|
|
145
|
+
if (sessionId === '') return {}
|
|
146
|
+
try {
|
|
147
|
+
return parseConfirmedBlocks(storage.getItem(confirmedStorageKey(sessionId)))
|
|
148
|
+
} catch {
|
|
149
|
+
return {}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 写一个会话的块笔记(存不下就算了,绝不能因此打断编辑)。 */
|
|
154
|
+
export function saveConfirmedBlocks(storage: ConfirmStorage, sessionId: string, blocks: ConfirmedBlocks): void {
|
|
155
|
+
if (sessionId === '') return
|
|
156
|
+
try {
|
|
157
|
+
storage.setItem(confirmedStorageKey(sessionId), serializeConfirmedBlocks(blocks))
|
|
158
|
+
} catch {
|
|
159
|
+
// 私有模式 / 配额满:见上。
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 切换并落盘一块,返回切换后的状态。
|
|
165
|
+
*
|
|
166
|
+
* 一次调用完成"读 → 切 → 写":编辑器里的按钮可能连点,中间隔着一次 React 渲染,
|
|
167
|
+
* 拿旧表覆盖会把连点丢一半。
|
|
168
|
+
*/
|
|
169
|
+
export function toggleStoredBlock(storage: ConfirmStorage, sessionId: string, id: string): boolean {
|
|
170
|
+
if (sessionId === '' || id === '') return false
|
|
171
|
+
const next = toggleConfirmedBlock(loadConfirmedBlocks(storage, sessionId), id)
|
|
172
|
+
saveConfirmedBlocks(storage, sessionId, next)
|
|
173
|
+
return isBlockConfirmed(next, id)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** 读一块的当前状态(每次现读:切换发生在 React 之外)。 */
|
|
177
|
+
export function isStoredBlockConfirmed(storage: ConfirmStorage, sessionId: string, id: string): boolean {
|
|
178
|
+
if (sessionId === '' || id === '') return false
|
|
179
|
+
return isBlockConfirmed(loadConfirmedBlocks(storage, sessionId), id)
|
|
180
|
+
}
|
|
@@ -103,6 +103,51 @@
|
|
|
103
103
|
|
|
104
104
|
.rvs-changes-stale { color: var(--rvs-chg-mod); }
|
|
105
105
|
|
|
106
|
+
/*
|
|
107
|
+
* 作用域切换(会话累计 / 本轮)。
|
|
108
|
+
*
|
|
109
|
+
* 一段式小胶囊:两个按钮共用一条描边,选中的那个用悬停底色 + 主色字,未选中的
|
|
110
|
+
* 用次级色 —— 折叠状态下这一行已经有文件数、+/−、审阅计数,切换必须比那些更像
|
|
111
|
+
* "开关"而不是"又一个计数"。按钮本身高度与 `.rvs-changes-reviewed` 一致(18px),
|
|
112
|
+
* 头行不会因为它变高。
|
|
113
|
+
*/
|
|
114
|
+
.rvs-changes-scope {
|
|
115
|
+
display: inline-flex;
|
|
116
|
+
flex: 0 0 auto;
|
|
117
|
+
border: 1px solid var(--rvs-chg-border);
|
|
118
|
+
border-radius: 9px;
|
|
119
|
+
overflow: hidden;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
.rvs-changes-scope-btn {
|
|
123
|
+
padding: 0 6px;
|
|
124
|
+
height: 18px;
|
|
125
|
+
border: none;
|
|
126
|
+
background: transparent;
|
|
127
|
+
color: var(--rvs-chg-fg3);
|
|
128
|
+
font: inherit;
|
|
129
|
+
font-size: 11px;
|
|
130
|
+
line-height: 18px;
|
|
131
|
+
white-space: nowrap;
|
|
132
|
+
cursor: pointer;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
.rvs-changes-scope-btn + .rvs-changes-scope-btn {
|
|
136
|
+
border-left: 1px solid var(--rvs-chg-border);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.rvs-changes-scope-btn:hover {
|
|
140
|
+
background-color: var(--rvs-chg-hover);
|
|
141
|
+
color: var(--rvs-chg-fg);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.rvs-changes-scope-btn[aria-pressed='true'] {
|
|
145
|
+
background-color: var(--rvs-chg-hover);
|
|
146
|
+
color: var(--rvs-chg-fg);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
.rvs-changes-scope-btn[aria-pressed='true']:hover { color: var(--rvs-chg-fg); }
|
|
150
|
+
|
|
106
151
|
.rvs-changes-spacer { flex: 1 1 auto; }
|
|
107
152
|
|
|
108
153
|
.rvs-changes-nav {
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 点开一行时,Host 给的这份 diff 到底能画出什么 —— 以及必须当面说清的那几种情况。
|
|
3
|
+
*
|
|
4
|
+
* 面板原来只看 `kind === 'text'`:二进制与读不到的文件都落进"空标记集",
|
|
5
|
+
* 屏幕上什么也没有、也没说为什么(`changes/ChangesDock.tsx` 的旧写法)。
|
|
6
|
+
*
|
|
7
|
+
* 三种"不能画"的原因必须分开说,因为读法完全不同:
|
|
8
|
+
* - `truncated`:Host 的读取被字节上限扣下。注意它**不是**"只给了前 N 处":被扣的是
|
|
9
|
+
* 文件的 after 文本,hunk 是拿 before 对**空文本**算出来的,等于整篇都算改动,而且
|
|
10
|
+
* `reverse` 为空、每个 hunk 的 patch 都是 ''(`recorder.ts` 394-407)—— 撤销不可
|
|
11
|
+
* 能成功。所以这种 diff 一行标记都不建,只说"比较不完整、不能撤销";
|
|
12
|
+
* - `binary`:二进制/非 UTF-8,没有可画的文本行;
|
|
13
|
+
* - `oversized`:文件超过行级比较上限(或字节被扣下),行数本身就不可信,不做行级比较;
|
|
14
|
+
* - `missing`:Host 读不到那个文件(`kind: 'missing'`,或调用本身没拿到东西);
|
|
15
|
+
* - `empty`:两侧都读到了、也比较了,但**现在**没有差异(行还留在列表里,内容已被改回)。
|
|
16
|
+
*
|
|
17
|
+
* 这里把"这份 diff 属于哪种结局"收成一个纯判定,并顺手把 `ChangeSet` 建好,让
|
|
18
|
+
* 面板只剩"显示哪句话"一件事。纯函数,`scripts/check-diff-outcome.mjs` 直接驱动。
|
|
19
|
+
*/
|
|
20
|
+
import type { RemoteChangesDiff } from '../../protocol.ts'
|
|
21
|
+
import { EMPTY_CHANGE_SET, changeSetFromHunks, type ChangeSet } from './model.ts'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 一份 diff 的可见结论。
|
|
25
|
+
*
|
|
26
|
+
* 顺序即判定优先级(见 {@link noticeOfDiff}):读不到 → 二进制 → 过大/截断 → 空比较。
|
|
27
|
+
*/
|
|
28
|
+
export type DiffNotice = 'truncated' | 'oversized' | 'binary' | 'missing' | 'empty'
|
|
29
|
+
|
|
30
|
+
/** i18n 键:每种结论一句可操作/可解释的话(zh + en 都在 `i18n.ts`)。 */
|
|
31
|
+
const NOTICE_KEY: Record<DiffNotice, string> = {
|
|
32
|
+
truncated: 'changes.diffTruncated',
|
|
33
|
+
oversized: 'changes.diffOversized',
|
|
34
|
+
binary: 'changes.diffBinary',
|
|
35
|
+
missing: 'changes.diffMissing',
|
|
36
|
+
empty: 'changes.diffEmpty',
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 一份结论对应的字典键。 */
|
|
40
|
+
export function diffNoticeKey(notice: DiffNotice): string {
|
|
41
|
+
return NOTICE_KEY[notice]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 面板拿到的这份 diff 属于哪种结论;`undefined` = 正常文本差异。 */
|
|
45
|
+
export function noticeOfDiff(diff: RemoteChangesDiff | null): DiffNotice | undefined {
|
|
46
|
+
if (diff === null) return 'missing'
|
|
47
|
+
if (diff.kind === 'binary') return 'binary'
|
|
48
|
+
if (diff.kind === 'missing') return 'missing'
|
|
49
|
+
if (diff.truncated === true) return 'truncated'
|
|
50
|
+
return diff.hunks.length === 0 ? 'empty' : undefined
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 建一批标记时调用方手头知道的那些事实。 */
|
|
54
|
+
export interface DiffMeta {
|
|
55
|
+
/** 文件的远端绝对路径(块标识的前半段,见 `changes/block-confirm.ts`)。 */
|
|
56
|
+
path?: string
|
|
57
|
+
/** 列表行的状态字母(`RemoteChangedFile.status`)。 */
|
|
58
|
+
status?: string
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 一次"点开一行"的完整结果。 */
|
|
62
|
+
export interface ChangeSetFromDiffResult {
|
|
63
|
+
set: ChangeSet
|
|
64
|
+
/** 必须显示给用户的结论;`undefined` = 一切正常,不必出提示。 */
|
|
65
|
+
notice?: DiffNotice
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 把一份 diff 结论同时翻成"要画的标记"与"要说的话"。
|
|
70
|
+
*
|
|
71
|
+
* **`truncated` 一行标记都不建**(M2):那种 diff 的 hunk 是 before 对空文本算的
|
|
72
|
+
* 整篇假差异,照着画会把每个改动点都标成删除,撤销更不可能成立(patch 全为空)。
|
|
73
|
+
* 宁可不画,也不给一份"看着像真相"的假标记。
|
|
74
|
+
*
|
|
75
|
+
* @param diff - `api.changes.diff` 的答案(`null` = 没拿到)。
|
|
76
|
+
* @param stamp - 这份数据的身份(文件 + 统计时刻 + 轮次)。
|
|
77
|
+
* @param meta - 调用方手头知道的事实,见 {@link DiffMeta}。
|
|
78
|
+
*/
|
|
79
|
+
export function changeSetFromDiff(
|
|
80
|
+
diff: RemoteChangesDiff | null,
|
|
81
|
+
stamp: string,
|
|
82
|
+
meta: DiffMeta = {},
|
|
83
|
+
): ChangeSetFromDiffResult {
|
|
84
|
+
const notice = noticeOfDiff(diff)
|
|
85
|
+
const unusable = diff === null || diff.kind !== 'text' || diff.truncated === true || diff.hunks.length === 0
|
|
86
|
+
if (unusable) {
|
|
87
|
+
return {
|
|
88
|
+
set: { ...EMPTY_CHANGE_SET, stamp },
|
|
89
|
+
...(notice === undefined ? {} : { notice }),
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const set = changeSetFromHunks(diff.hunks, stamp, diff.patch, {
|
|
93
|
+
...(meta.path === undefined ? {} : { path: meta.path }),
|
|
94
|
+
...(meta.status === undefined ? {} : { status: meta.status }),
|
|
95
|
+
})
|
|
96
|
+
return { set }
|
|
97
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「回到页面/窗口时要不要强制刷新一次」的纯判定。
|
|
3
|
+
*
|
|
4
|
+
* 背景:宿主账本的自愈阈值是自适应的(`recorder.ts` 的 `selfHealMaxAge`,5–30s),
|
|
5
|
+
* 而面板在页面隐藏期间不轮询。用户切走再回来时,屏幕上那份统计可能已经落后一个
|
|
6
|
+
* 阈值;普通轮询读(`load(false)`)仍受阈值保护,所以"回来"这一刻要**强制刷新一次**
|
|
7
|
+
* (等价于点 ⟳,`force` 会绕过节流并立刻重扫)。
|
|
8
|
+
*
|
|
9
|
+
* 但"回来"会连环触发事件:切回标签页同时有 `visibilitychange` 与 `focus`;在窗口
|
|
10
|
+
* 之间点几下也会连续 `focus`。所以这里给三条门槛,任何一条不满足就不刷新:
|
|
11
|
+
* - 页面必须真的可见(隐藏期间不轮询,也不该偷偷 force);
|
|
12
|
+
* - 距上一次强制刷新至少 {@link FORCE_REFRESH_DEBOUNCE_MS}(5s);
|
|
13
|
+
* - 没有同类的 `list` 请求在飞(它马上就会带回新数据)。
|
|
14
|
+
*
|
|
15
|
+
* 纯函数 + 显式时钟入参,`scripts/check-change-freshness.mjs` 直接驱动。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** 两次强制刷新之间的最小间隔("点回窗口"的连环事件都落在这之内)。 */
|
|
19
|
+
export const FORCE_REFRESH_DEBOUNCE_MS = 5_000
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 一个 `list` 请求最多算"在飞"多久(60s)。
|
|
23
|
+
*
|
|
24
|
+
* 传输层理论上不会永远不 settle,但一次挂住的 fetch(半开连接、Host 卡死)会让
|
|
25
|
+
* "在飞请求跳过"变成**永久**跳过——回到页面再也不强制刷新。超过这个时长就当作
|
|
26
|
+
* 那次请求已经失败(迟到的响应仍会自己 setData,重发的这次不会破坏什么)。
|
|
27
|
+
*/
|
|
28
|
+
export const LOAD_IN_FLIGHT_MAX_MS = 60_000
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 这次 `list` 请求现在还算在飞吗?
|
|
32
|
+
*
|
|
33
|
+
* @param now - 当前时刻(毫秒)。
|
|
34
|
+
* @param startedAt - 那次请求开始的时刻;没有在飞的请求为 `undefined`。
|
|
35
|
+
*/
|
|
36
|
+
export function loadStillInFlight(now: number, startedAt: number | undefined): boolean {
|
|
37
|
+
if (startedAt === undefined) return false
|
|
38
|
+
if (!Number.isFinite(now) || !Number.isFinite(startedAt)) return false
|
|
39
|
+
// Exactly 60s counts as "no longer in flight": the cap is a deadline, not a window.
|
|
40
|
+
return now - startedAt < LOAD_IN_FLIGHT_MAX_MS
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** @see shouldForceRefresh */
|
|
44
|
+
export interface ForceRefreshInput {
|
|
45
|
+
/** 当前时刻(毫秒;真实调用是 `Date.now()`,门禁注入假时钟)。 */
|
|
46
|
+
now: number
|
|
47
|
+
/** 上一次强制刷新的时刻;从未刷新过为 `undefined`。 */
|
|
48
|
+
lastForceAt: number | undefined
|
|
49
|
+
/** 页面当前是否可见(`document.hidden === false`)。 */
|
|
50
|
+
visible: boolean
|
|
51
|
+
/** 是否已有 `list` 请求在飞(那一次请求会带回最新数据)。 */
|
|
52
|
+
inFlight?: boolean
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 这一刻要不要为了"用户回来了"强制刷新一次?
|
|
57
|
+
*
|
|
58
|
+
* @param input - 见 {@link ForceRefreshInput}。
|
|
59
|
+
* @returns true = 调用方应立刻 `load(true)` 并记下 `now`。
|
|
60
|
+
*/
|
|
61
|
+
export function shouldForceRefresh(input: ForceRefreshInput): boolean {
|
|
62
|
+
if (!input.visible) return false
|
|
63
|
+
if (input.inFlight === true) return false
|
|
64
|
+
if (!Number.isFinite(input.now)) return false
|
|
65
|
+
if (input.lastForceAt === undefined) return true
|
|
66
|
+
if (!Number.isFinite(input.lastForceAt)) return true
|
|
67
|
+
return input.now - input.lastForceAt >= FORCE_REFRESH_DEBOUNCE_MS
|
|
68
|
+
}
|
|
@@ -395,6 +395,48 @@ function collectHunk(hunk: UnifiedHunk, marks: ChangeMark[], index: number): num
|
|
|
395
395
|
return first
|
|
396
396
|
}
|
|
397
397
|
|
|
398
|
+
/**
|
|
399
|
+
* 建立这批标记时随数据一起带下来的元信息(都可省略)。
|
|
400
|
+
*
|
|
401
|
+
* 两者各自服务一个上层判断,而且都**只能在这一层算**:块标识要用 hunk 头的四个
|
|
402
|
+
* 数字 + 文件身份;状态来自列表行。行数统计也在这里算(面板的过期判定要用)。
|
|
403
|
+
*/
|
|
404
|
+
export interface ChangeSetMeta {
|
|
405
|
+
/**
|
|
406
|
+
* 文件的**远端绝对路径**(块标识的前半段,见 `changes/block-confirm.ts`)。
|
|
407
|
+
*
|
|
408
|
+
* 用绝对路径而不是显示路径:显示路径是相对会话根的,两个绑定/两个仓库子目录
|
|
409
|
+
* 可以有相同的显示路径 + 相同的 hunk 头,那样它们的块会被认成同一块。
|
|
410
|
+
*/
|
|
411
|
+
path?: string
|
|
412
|
+
/** 列表行的状态字母(`RemoteChangedFile.status`)。 */
|
|
413
|
+
status?: string
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** `+` / `-` 行数(`--numstat` 语义),与列表行给出的数字同源。 */
|
|
417
|
+
function countHunkLines(hunks: readonly UnifiedHunk[]): { added: number; deleted: number } {
|
|
418
|
+
let added = 0
|
|
419
|
+
let deleted = 0
|
|
420
|
+
for (const hunk of hunks) {
|
|
421
|
+
for (const raw of hunk.lines) {
|
|
422
|
+
if (raw[0] === '+') added += 1
|
|
423
|
+
else if (raw[0] === '-') deleted += 1
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return { added, deleted }
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* 每个 hunk 的稳定块标识:`远端绝对路径|oldStart:oldLines:newStart:newLines`。
|
|
431
|
+
*
|
|
432
|
+
* 行号会随远端改动漂移,hunk 头里的四个数字才是"这一处改动"的身份;带上绝对
|
|
433
|
+
* 路径是为了不同文件、不同仓库里恰好同号的块不会互相认领(显示路径是相对会话根
|
|
434
|
+
* 的,两个仓库子目录可以给出同一个字符串)。
|
|
435
|
+
*/
|
|
436
|
+
function blockIdsOf(hunks: readonly UnifiedHunk[], path: string): string[] {
|
|
437
|
+
return hunks.map(hunk => `${path}|${hunk.oldStart}:${hunk.oldCount}:${hunk.newStart}:${hunk.newCount}`)
|
|
438
|
+
}
|
|
439
|
+
|
|
398
440
|
/**
|
|
399
441
|
* Build the editable set for one file.
|
|
400
442
|
*
|
|
@@ -402,9 +444,15 @@ function collectHunk(hunk: UnifiedHunk, marks: ChangeMark[], index: number): num
|
|
|
402
444
|
* @param stamp - identity of the comparison (file + baseline + turn).
|
|
403
445
|
* @param filePatch - the host's whole-file reverse patch, when it served one
|
|
404
446
|
* (see `ChangeSet.filePatch`); '' / missing = nothing to revert as a whole.
|
|
447
|
+
* @param meta - 行数 / 状态 / 块标识的来处(见 {@link ChangeSetMeta})。
|
|
405
448
|
* @returns the mark set the editor paints, or {@link EMPTY_CHANGE_SET}.
|
|
406
449
|
*/
|
|
407
|
-
export function changeSetFromHunks(
|
|
450
|
+
export function changeSetFromHunks(
|
|
451
|
+
hunks: readonly UnifiedHunk[],
|
|
452
|
+
stamp: string,
|
|
453
|
+
filePatch?: string,
|
|
454
|
+
meta: ChangeSetMeta = {},
|
|
455
|
+
): ChangeSet {
|
|
408
456
|
if (hunks.length === 0) return { marks: [], hunks: [], stamp, patches: [] }
|
|
409
457
|
const marks: ChangeMark[] = []
|
|
410
458
|
// `hunks[i]` / `patches[i]` must stay in step with the mark's `hunk` index, so
|
|
@@ -421,11 +469,18 @@ export function changeSetFromHunks(hunks: readonly UnifiedHunk[], stamp: string,
|
|
|
421
469
|
// Jump order is ascending by line; the mark's `hunk` is a key into `patches`,
|
|
422
470
|
// never into this list — that indirection is what keeps both orderings right.
|
|
423
471
|
const order = [...anchors].sort((left, right) => left.line - right.line)
|
|
472
|
+
const counts = countHunkLines(hunks)
|
|
424
473
|
return {
|
|
425
474
|
marks,
|
|
426
475
|
hunks: [...new Set(order.map(anchor => anchor.line))],
|
|
427
476
|
stamp,
|
|
428
477
|
patches,
|
|
429
478
|
...(filePatch === undefined || filePatch === '' ? {} : { filePatch }),
|
|
479
|
+
// 行数只在**完整比较**里才建:调用方(`changeSetFromDiff`)不会把被字节上限
|
|
480
|
+
// 扣下的 diff 送进来,所以走到这里的 hunk 就是可信的比较结果。
|
|
481
|
+
added: counts.added,
|
|
482
|
+
deleted: counts.deleted,
|
|
483
|
+
...(meta.status === undefined || meta.status === '' ? {} : { status: meta.status }),
|
|
484
|
+
...(meta.path === undefined || meta.path === '' ? {} : { blocks: blockIdsOf(hunks, meta.path) }),
|
|
430
485
|
}
|
|
431
486
|
}
|