Reference
diff.compare 工具契约
所有集成面(DSH / 扩展 / CLI / 本地站)共享的唯一比对契约。
参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
old | string | 是* | 旧文件路径 |
new | string | 是* | 新文件路径;与 revision 连用时可为同一路径 |
revision | string | 否 | git 版本(如 HEAD~1),与工作区对比 |
cwd | string | 否 | 相对路径基准,默认进程 cwd |
context | integer | 否 | hunk 上下文行数,默认 3,最大 20 |
maxHunks | integer | 否 | 返回的 hunk 上限,默认 40,最大 200 |
format | json | unified | 否 | 输出形态,默认 json |
* old 与 new 至少一个;缺 new 时须给 revision。
返回
interface CompareResult {
kind: "text" | "code" | "binary";
family: "text" | "markdown" | "code" | "docx" | "xlsx" | "pptx" | "pdf";
oldLabel: string;
newLabel: string;
added: number;
removed: number;
hunks: Array<{
header: string;
lines: Array<{ kind: "equal" | "insert" | "delete"; oldLine: number | null; newLine: number | null; text: string }>;
}>;
truncated: boolean;
symbols?: { added: string[]; removed: string[] };
unified?: string;
note?: string;
}
实现位置
- 纯逻辑:
packages/diff-tool/src/index.ts(Node,读盘 +git show) - HTTP:
packages/diff-tool/src/server.ts→POST /api/diff - 浏览器内等价实现:
packages/core-text+packages/core-code
HTTP 形态
curl -s -X POST http://127.0.0.1:5279/api/diff \
-H 'content-type: application/json' \
-d '{"old":"examples/parse-v1.ts","new":"examples/parse-v2.ts"}'
错误
| 情况 | 行为 |
|---|---|
| 缺参数 | 抛错:diff.compare needs ... |
| 二进制族 | 返回 kind: "binary" + note,不报错 |
| 文件不存在 | 抛错(HTTP 500 带 message) |
本页章节:参数 · 返回 · 实现位置 · HTTP 形态 · 错误