dsh-volcano-git-plugin 0.0.0-stage → 1.7.2

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/index.js ADDED
@@ -0,0 +1,712 @@
1
+ /**
2
+ * dsh-volcano-git-plugin — Host half.
3
+ *
4
+ * A Cordis "class plugin": this module exports `GitManagerService` extending
5
+ * `TypertRemoteService`. The DSH loader instantiates the class and registers it
6
+ * as the `gitManager` service; the Typert Gateway exposes its `@Remote`-marked
7
+ * methods to the browser Client half under the `gitManager` Remote namespace.
8
+ *
9
+ * Responsibilities:
10
+ * - 63 Remote methods(30 个 v1 + v2 §2.1/§2.2/§2.3 的 21 个 + P3-A §8.1/§8.3
11
+ * 的 5 个:rebasePlan/rebaseRun/fixupCommit/rebaseBranch/lineApply,子仓库
12
+ * 支持的 repoTree(P0 仓库树发现)与 revExists(P1 sha 存在性门控),以及
13
+ * 子仓库支持 P2 的 submoduleInit/submoduleUpdate/submoduleSync/
14
+ * submoduleDeinit/submoduleAdd 五个 submodule 管理方法):
15
+ * probe / overview / status / diff / log / branches / worktrees /
16
+ * conflictContent + 全部变更类(含 hunkApply 逐块操作、cherryPick 单提交
17
+ * 拣选、stageHunk 逐块暂存、lineApply 行级暂存/撤销)。
18
+ * - Path validation (fs.stat) + envelope wrapping (`{ok, value}` / `{ok, error}`).
19
+ * - GitError → user-friendly error code mapping (not-a-repo / dubious-ownership
20
+ * / auth-failed / timeout / too-large / git-missing / git-failed).
21
+ * - Network ops (fetch/pull/push): on failure, surface `_output` + `_status`
22
+ * side-channel data from git-core as envelope fields, so Client can refresh
23
+ * the UI while showing the error.
24
+ *
25
+ * All file I/O + git invocation goes through git-core.mjs; this file is purely
26
+ * the Cordis glue.
27
+ */
28
+
29
+ import { statSync } from "node:fs";
30
+ import { Remote, TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
31
+ import { Service } from "@deepseek-ai/cordis";
32
+
33
+ // 诊断冒烟:loader import 本 entry 时必然执行。若 boot 日志没有这行,
34
+ // 说明 entry 根本没被 import(组合树/加载层问题),而非模块代码错误。
35
+ try { console.error("[dsh-volcano-git-plugin] index.js module body executed"); } catch (_) { /* noop */ }
36
+
37
+ import {
38
+ GitError,
39
+ probeRepo,
40
+ getStatus,
41
+ listSubRepos,
42
+ revExists,
43
+ submoduleInit,
44
+ submoduleUpdate,
45
+ submoduleSync,
46
+ submoduleDeinit,
47
+ submoduleAdd,
48
+ safeJoin,
49
+ getBranches,
50
+ getRemotes,
51
+ getLog,
52
+ getWorktrees,
53
+ getConflictContent,
54
+ getDiff,
55
+ stageFiles,
56
+ unstageFiles,
57
+ discardFiles,
58
+ applyHunk,
59
+ stageHunkFile,
60
+ commitStaged,
61
+ createBranch,
62
+ switchBranch,
63
+ deleteBranch,
64
+ renameBranch,
65
+ mergeBranch,
66
+ abortMerge,
67
+ continueMerge,
68
+ cherryPickCommit,
69
+ revertCommit,
70
+ resolveConflictFile,
71
+ fetchRemote,
72
+ pullBranch,
73
+ pushBranch,
74
+ addWorktree,
75
+ removeWorktree,
76
+ pruneWorktrees,
77
+ initRepo,
78
+ getStashes,
79
+ stashPush,
80
+ stashPop,
81
+ stashApply,
82
+ stashDrop,
83
+ stashClear,
84
+ getTags,
85
+ createTag,
86
+ deleteTag,
87
+ resetRepo,
88
+ getBlame,
89
+ getDiffRange,
90
+ getReflog,
91
+ addRemote,
92
+ removeRemote,
93
+ renameRemote,
94
+ getConfig,
95
+ setConfig,
96
+ unsetConfig,
97
+ rebasePlan,
98
+ rebaseRun,
99
+ fixupCommit,
100
+ rebaseBranch,
101
+ applyLine,
102
+ } from "./git-core.mjs";
103
+ import { computeGraph } from "./git-graph.mjs";
104
+
105
+ // Node ESM 不支持 Stage 3 装饰器,手动驱动 Remote() 装饰器。
106
+ function markRemoteMethod(instance, method, exportName) {
107
+ const decorator = Remote(method, undefined);
108
+ const initializers = [];
109
+ decorator(undefined, {
110
+ kind: "method", name: method, static: false, private: false,
111
+ addInitializer: (fn) => initializers.push(fn),
112
+ });
113
+ for (const fn of initializers) fn.call(instance);
114
+ }
115
+
116
+ // ---- error envelope mapping ------------------------------------------------
117
+
118
+ /**
119
+ * Map a raw error (usually GitError) to the user-facing {code, message}.
120
+ * Codes are documented in §3.3 of plan + plan §5.
121
+ */
122
+ function mapGitError(e) {
123
+ if (e instanceof GitError) {
124
+ const stderr = e.stderr || e.message || "";
125
+ const out = { code: "git-failed", message: String(stderr).trim() || "git 命令失败" };
126
+ if (e.kind === "missing") { out.code = "git-missing"; out.message = "找不到 git 可执行文件,请先安装 Git。"; return out; }
127
+ if (e.kind === "timeout") { out.code = "timeout"; return out; }
128
+ if (e.kind === "too-large") { out.code = "diff-too-large"; return out; }
129
+ // kind === "exit"
130
+ const low = stderr.toLowerCase();
131
+ if (/not a git repository/.test(low)) { out.code = "not-a-repo"; return out; }
132
+ if (/dubious ownership/.test(low)) {
133
+ out.code = "dubious-ownership";
134
+ // 安全:不给用户擅自改全局配置;只提示命令
135
+ const path = e && e._path ? e._path : "<path>";
136
+ out.message = `目录所有权不被信任(git ≥2.35 新安全特性)。请运行:git config --global --add safe.directory ${path}`;
137
+ return out;
138
+ }
139
+ if (/authentication failed|permission denied|could not read from remote/i.test(stderr)) {
140
+ out.code = "auth-failed"; return out;
141
+ }
142
+ if (/conflict/i.test(stderr)) { out.code = "merge-conflict"; return out; }
143
+ return out;
144
+ }
145
+ return { code: "internal", message: String(e && e.message || e) };
146
+ }
147
+
148
+ // 把 fetch/pull/push 失败时 GitError 上挂的 _output / _status 也回传出去
149
+ function netErrorEnvelope(e, path) {
150
+ const env = mapGitError(e);
151
+ const out = { ok: false, error: env };
152
+ if (e && typeof e._output === "string") out.output = e._output;
153
+ if (e && e._status) out.status = e._status;
154
+ return out;
155
+ }
156
+
157
+ // ---- path validation ------------------------------------------------------
158
+
159
+ function validatePath(path) {
160
+ if (typeof path !== "string" || path.length === 0) {
161
+ throw new GitError("exit", "请求缺少 path");
162
+ }
163
+ try {
164
+ const s = statSync(path);
165
+ if (!s.isDirectory()) {
166
+ throw new GitError("exit", "path 不是目录:" + path);
167
+ }
168
+ } catch (e) {
169
+ if (e instanceof GitError) throw e;
170
+ throw new GitError("exit", "path 不可访问:" + path);
171
+ }
172
+ }
173
+
174
+ // ---- the service ----------------------------------------------------------
175
+
176
+ export class GitManagerService extends TypertRemoteService {
177
+ // 不需要 workspaceRegistry / sessions ——目标路径全由 client 显式传入
178
+ static inject = [];
179
+ constructor(ctx, config) { super(ctx, "gitManager"); } // 必须传精确服务键
180
+
181
+ [Service.init]() {
182
+ markRemoteMethod(this, "probe", "probe");
183
+ markRemoteMethod(this, "overview", "overview");
184
+ markRemoteMethod(this, "status", "status");
185
+ markRemoteMethod(this, "diff", "diff");
186
+ markRemoteMethod(this, "log", "log");
187
+ markRemoteMethod(this, "branches", "branches");
188
+ markRemoteMethod(this, "remotes", "remotes");
189
+ markRemoteMethod(this, "worktrees", "worktrees");
190
+ markRemoteMethod(this, "conflictContent", "conflictContent");
191
+ markRemoteMethod(this, "stage", "stage");
192
+ markRemoteMethod(this, "unstage", "unstage");
193
+ markRemoteMethod(this, "discard", "discard");
194
+ markRemoteMethod(this, "hunkApply", "hunkApply");
195
+ markRemoteMethod(this, "commit", "commit");
196
+ markRemoteMethod(this, "branchCreate", "branchCreate");
197
+ markRemoteMethod(this, "checkout", "checkout");
198
+ markRemoteMethod(this, "branchDelete", "branchDelete");
199
+ markRemoteMethod(this, "branchRename", "branchRename");
200
+ markRemoteMethod(this, "merge", "merge");
201
+ markRemoteMethod(this, "mergeAbort", "mergeAbort");
202
+ markRemoteMethod(this, "mergeContinue", "mergeContinue");
203
+ markRemoteMethod(this, "cherryPick", "cherryPick");
204
+ markRemoteMethod(this, "resolveConflict", "resolveConflict");
205
+ markRemoteMethod(this, "fetch", "fetch");
206
+ markRemoteMethod(this, "pull", "pull");
207
+ markRemoteMethod(this, "push", "push");
208
+ markRemoteMethod(this, "worktreeAdd", "worktreeAdd");
209
+ markRemoteMethod(this, "worktreeRemove", "worktreeRemove");
210
+ markRemoteMethod(this, "worktreePrune", "worktreePrune");
211
+ markRemoteMethod(this, "init", "init");
212
+ // v2 §2.1 stageHunk + §2.2 P1
213
+ markRemoteMethod(this, "stageHunk", "stageHunk");
214
+ markRemoteMethod(this, "stashList", "stashList");
215
+ markRemoteMethod(this, "stashPush", "stashPush");
216
+ markRemoteMethod(this, "stashPop", "stashPop");
217
+ markRemoteMethod(this, "stashApply", "stashApply");
218
+ markRemoteMethod(this, "stashDrop", "stashDrop");
219
+ markRemoteMethod(this, "stashClear", "stashClear");
220
+ markRemoteMethod(this, "tags", "tags");
221
+ markRemoteMethod(this, "tagCreate", "tagCreate");
222
+ markRemoteMethod(this, "tagDelete", "tagDelete");
223
+ markRemoteMethod(this, "reset", "reset");
224
+ markRemoteMethod(this, "revert", "revert");
225
+ markRemoteMethod(this, "blame", "blame");
226
+ markRemoteMethod(this, "diffRange", "diffRange");
227
+ markRemoteMethod(this, "reflog", "reflog");
228
+ // v2 §2.3 P2
229
+ markRemoteMethod(this, "remoteAdd", "remoteAdd");
230
+ markRemoteMethod(this, "remoteRemove", "remoteRemove");
231
+ markRemoteMethod(this, "remoteRename", "remoteRename");
232
+ markRemoteMethod(this, "configList", "configList");
233
+ markRemoteMethod(this, "configSet", "configSet");
234
+ markRemoteMethod(this, "configUnset", "configUnset");
235
+ // P3-A §8.1/§8.3(51 → 56)
236
+ markRemoteMethod(this, "rebasePlan", "rebasePlan");
237
+ markRemoteMethod(this, "rebaseRun", "rebaseRun");
238
+ markRemoteMethod(this, "fixupCommit", "fixupCommit");
239
+ markRemoteMethod(this, "rebaseBranch", "rebaseBranch");
240
+ markRemoteMethod(this, "lineApply", "lineApply");
241
+ // 子仓库支持(2026-10-13 P0):仓库树发现。必须与 typert.host.js 的
242
+ // METHODS 数组同名同序(self-test 静态守卫比对两个列表)。
243
+ markRemoteMethod(this, "repoTree", "repoTree");
244
+ // 子仓库支持 P1:子仓库历史跳转的可用性门控(sha 是否在该仓库对象库里)
245
+ markRemoteMethod(this, "revExists", "revExists");
246
+ // 子仓库支持 P2:submodule 管理五方法(58 → 63)。位置参数是仓库内路径
247
+ // (subPath,相对 toplevel),不是 .gitmodules 注册名——只有 submoduleAdd 的
248
+ // name 是注册名。
249
+ markRemoteMethod(this, "submoduleInit", "submoduleInit");
250
+ markRemoteMethod(this, "submoduleUpdate", "submoduleUpdate");
251
+ markRemoteMethod(this, "submoduleSync", "submoduleSync");
252
+ markRemoteMethod(this, "submoduleDeinit", "submoduleDeinit");
253
+ markRemoteMethod(this, "submoduleAdd", "submoduleAdd");
254
+ }
255
+
256
+ // === query ===
257
+ async probe(request) {
258
+ try { return { ok: true, value: await probeRepo(request.path) }; }
259
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
260
+ }
261
+
262
+ async overview(request) {
263
+ try {
264
+ validatePath(request.path);
265
+ const [probe, status, remotes] = await Promise.all([
266
+ probeRepo(request.path),
267
+ getStatus(request.path),
268
+ getRemotes(request.path),
269
+ ]);
270
+ return { ok: true, value: { probe, status, remotes } };
271
+ } catch (e) {
272
+ const err = mapGitError(e);
273
+ // probe 失败(非仓库)让客户端拿到 isRepo:false —— 改 ok:true, value.probe.isRepo
274
+ if (err.code === "not-a-repo") {
275
+ return { ok: true, value: { probe: { isRepo: false }, status: null, remotes: [] } };
276
+ }
277
+ return { ok: false, error: err };
278
+ }
279
+ }
280
+
281
+ async status(request) {
282
+ try { validatePath(request.path); return { ok: true, value: await getStatus(request.path) }; }
283
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
284
+ }
285
+
286
+ async diff(request) {
287
+ try { validatePath(request.path); return { ok: true, value: await getDiff(request.path, request) }; }
288
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
289
+ }
290
+
291
+ async log(request) {
292
+ try {
293
+ validatePath(request.path);
294
+ const r = await getLog(request.path, request);
295
+ // Host 侧组装 graph,Client 只画
296
+ const graph = computeGraph(r.commits);
297
+ return { ok: true, value: Object.assign({}, r, { graph }) };
298
+ } catch (e) { return { ok: false, error: mapGitError(e) }; }
299
+ }
300
+
301
+ async branches(request) {
302
+ try { validatePath(request.path); return { ok: true, value: await getBranches(request.path) }; }
303
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
304
+ }
305
+
306
+ async remotes(request) {
307
+ try { validatePath(request.path); return { ok: true, value: await getRemotes(request.path) }; }
308
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
309
+ }
310
+
311
+ async worktrees(request) {
312
+ try { validatePath(request.path); return { ok: true, value: await getWorktrees(request.path) }; }
313
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
314
+ }
315
+
316
+ async conflictContent(request) {
317
+ try { validatePath(request.path); return { ok: true, value: await getConflictContent(request.path, request.file) }; }
318
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
319
+ }
320
+
321
+ // === mutations (all return fresh status/worktrees in value) ===
322
+ async stage(request) {
323
+ try {
324
+ validatePath(request.path);
325
+ // exclude(子仓库支持 §5.1):「暂存全部」时把嵌套独立仓库目录排掉,避免
326
+ // `git add dir/` 把它折成意外 gitlink。每个条目先过 safeJoin(与 discard
327
+ // 同款防护):拒绝绝对路径与 .. 越界。
328
+ let exclude;
329
+ if (Array.isArray(request.exclude) && request.exclude.length > 0) {
330
+ const probe = await probeRepo(request.path);
331
+ if (!probe.isRepo) throw new GitError("exit", "stage:仓库不可用:" + request.path);
332
+ exclude = request.exclude.map((p) => {
333
+ safeJoin(probe.toplevel, p);
334
+ return p;
335
+ });
336
+ }
337
+ return { ok: true, value: { status: await stageFiles(request.path, request.files || [], !!request.all, exclude) } };
338
+ }
339
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
340
+ }
341
+
342
+ async unstage(request) {
343
+ try { validatePath(request.path); return { ok: true, value: { status: await unstageFiles(request.path, request.files || []) } }; }
344
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
345
+ }
346
+
347
+ async discard(request) {
348
+ try {
349
+ validatePath(request.path);
350
+ const raw = await discardFiles(request.path, request.files || [], !!request.includeUntracked);
351
+ // discardFiles 把「因含 .git 被跳过、不删除」的路径挂在 status.skipped 上,
352
+ // 这里提到 envelope 的 { status, skipped? }——status 自身严格对齐 statusSchema
353
+ // (不认识的 key 会被 zod 剥掉,客户端就拿不到了)。
354
+ const value = { status: Object.assign({}, raw) };
355
+ if (Array.isArray(raw.skipped) && raw.skipped.length > 0) value.skipped = raw.skipped;
356
+ delete value.status.skipped;
357
+ return { ok: true, value };
358
+ }
359
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
360
+ }
361
+
362
+ // IDEA 式逐块操作:scope=worktree 撤销此块(恢复成 index 版本);
363
+ // scope=staged 取消暂存此块(改动移回工作区,不丢内容)。
364
+ async hunkApply(request) {
365
+ try { validatePath(request.path); return { ok: true, value: { status: await applyHunk(request.path, request) } }; }
366
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
367
+ }
368
+
369
+ async commit(request) {
370
+ try { validatePath(request.path); return { ok: true, value: await commitStaged(request.path, request.message, !!request.amend) }; }
371
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
372
+ }
373
+
374
+ async branchCreate(request) {
375
+ try { validatePath(request.path); return { ok: true, value: { status: await createBranch(request.path, request.name, request.startPoint, !!request.checkout) } }; }
376
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
377
+ }
378
+
379
+ async checkout(request) {
380
+ try { validatePath(request.path); return { ok: true, value: { status: await switchBranch(request.path, request.name) } }; }
381
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
382
+ }
383
+
384
+ async branchDelete(request) {
385
+ try { validatePath(request.path); return { ok: true, value: { status: await deleteBranch(request.path, request.name, !!request.force) } }; }
386
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
387
+ }
388
+
389
+ async branchRename(request) {
390
+ try { validatePath(request.path); return { ok: true, value: { status: await renameBranch(request.path, request.oldName, request.newName) } }; }
391
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
392
+ }
393
+
394
+ async merge(request) {
395
+ try { validatePath(request.path); return { ok: true, value: await mergeBranch(request.path, request.branch, !!request.noFf) }; }
396
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
397
+ }
398
+
399
+ async mergeAbort(request) {
400
+ try { validatePath(request.path); return { ok: true, value: { status: await abortMerge(request.path) } }; }
401
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
402
+ }
403
+
404
+ async mergeContinue(request) {
405
+ try { validatePath(request.path); return { ok: true, value: { status: await continueMerge(request.path) } }; }
406
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
407
+ }
408
+
409
+ // cherry-pick 单个提交到当前分支。冲突不抛错:返回 picked:false + status
410
+ //(仓库进入 CHERRY_PICK_HEAD 态,走冲突页解决后 mergeContinue 完成 pick)。
411
+ async cherryPick(request) {
412
+ try { validatePath(request.path); return { ok: true, value: await cherryPickCommit(request.path, request.sha) }; }
413
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
414
+ }
415
+
416
+ async resolveConflict(request) {
417
+ try { validatePath(request.path); return { ok: true, value: { status: await resolveConflictFile(request.path, request.file, request.strategy, request.content) } }; }
418
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
419
+ }
420
+
421
+ async fetch(request) {
422
+ try { validatePath(request.path); return { ok: true, value: await fetchRemote(request.path, request.remote) }; }
423
+ catch (e) { return netErrorEnvelope(e, request.path); }
424
+ }
425
+
426
+ async pull(request) {
427
+ try { validatePath(request.path); return { ok: true, value: await pullBranch(request.path, request || {}) }; }
428
+ catch (e) { return netErrorEnvelope(e, request.path); }
429
+ }
430
+
431
+ async push(request) {
432
+ try { validatePath(request.path); return { ok: true, value: await pushBranch(request.path, request || {}) }; }
433
+ catch (e) { return netErrorEnvelope(e, request.path); }
434
+ }
435
+
436
+ async worktreeAdd(request) {
437
+ try { validatePath(request.path); return { ok: true, value: await addWorktree(request.path, request.worktreePath, request.newBranch, request.startPoint) }; }
438
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
439
+ }
440
+
441
+ async worktreeRemove(request) {
442
+ try { validatePath(request.path); return { ok: true, value: await removeWorktree(request.path, request.worktreePath, !!request.force) }; }
443
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
444
+ }
445
+
446
+ async worktreePrune(request) {
447
+ try { validatePath(request.path); return { ok: true, value: await pruneWorktrees(request.path) }; }
448
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
449
+ }
450
+
451
+ async init(request) {
452
+ try {
453
+ // init 接受不存在或为空目录 —— 不强制 validatePath
454
+ const path = request.path;
455
+ if (typeof path !== "string" || path.length === 0) {
456
+ return { ok: false, error: { code: "bad-path", message: "init 需要 path" } };
457
+ }
458
+ return { ok: true, value: { probe: await initRepo(path) } };
459
+ } catch (e) { return { ok: false, error: mapGitError(e) }; }
460
+ }
461
+
462
+ // === v2 §2.1/§2.2:stageHunk + stash 全家 + tag 全家 + reset/revert/blame/diffRange/reflog ===
463
+
464
+ // IDEA 式「暂存此块」:unstaged diff 的第 hunkIndex 块正向进 index(非危险,无确认)
465
+ async stageHunk(request) {
466
+ try { validatePath(request.path); return { ok: true, value: { status: await stageHunkFile(request.path, request) } }; }
467
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
468
+ }
469
+
470
+ async stashList(request) {
471
+ try { validatePath(request.path); return { ok: true, value: await getStashes(request.path) }; }
472
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
473
+ }
474
+
475
+ async stashPush(request) {
476
+ try { validatePath(request.path); return { ok: true, value: await stashPush(request.path, request.message, !!request.includeUntracked) }; }
477
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
478
+ }
479
+
480
+ // pop/apply 冲突不抛错(git-core 契约):返回 {status},status.conflicted 非空即
481
+ // 交冲突页处理;stash 条目保留,中止走 abortMerge 兜底(Lead 裁决 2026-09-29)。
482
+ async stashPop(request) {
483
+ try { validatePath(request.path); return { ok: true, value: { status: await stashPop(request.path, request.index) } }; }
484
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
485
+ }
486
+
487
+ async stashApply(request) {
488
+ try { validatePath(request.path); return { ok: true, value: { status: await stashApply(request.path, request.index) } }; }
489
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
490
+ }
491
+
492
+ async stashDrop(request) {
493
+ try { validatePath(request.path); return { ok: true, value: await stashDrop(request.path, request.index) }; }
494
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
495
+ }
496
+
497
+ async stashClear(request) {
498
+ try { validatePath(request.path); return { ok: true, value: await stashClear(request.path) }; }
499
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
500
+ }
501
+
502
+ async tags(request) {
503
+ try { validatePath(request.path); return { ok: true, value: await getTags(request.path) }; }
504
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
505
+ }
506
+
507
+ // message 非空 → annotated tag;name/sha 走白名单校验(防选项注入)
508
+ async tagCreate(request) {
509
+ try { validatePath(request.path); return { ok: true, value: await createTag(request.path, request.name, request.sha, request.message, !!request.force) }; }
510
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
511
+ }
512
+
513
+ async tagDelete(request) {
514
+ try { validatePath(request.path); return { ok: true, value: await deleteTag(request.path, request.name) }; }
515
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
516
+ }
517
+
518
+ // mode∈soft|mixed|hard、target 走白名单(sha/HEAD/HEAD~n/@{...});
519
+ // hard 不做 UI 确认(确认是 client 职责)
520
+ async reset(request) {
521
+ try { validatePath(request.path); return { ok: true, value: { status: await resetRepo(request.path, request.mode, request.target) } }; }
522
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
523
+ }
524
+
525
+ // 冲突不抛错:reverted=false + status,仓库进 REVERT_HEAD 态(与 cherryPick 同款)
526
+ async revert(request) {
527
+ try { validatePath(request.path); return { ok: true, value: await revertCommit(request.path, request.sha) }; }
528
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
529
+ }
530
+
531
+ async blame(request) {
532
+ try { validatePath(request.path); return { ok: true, value: await getBlame(request.path, request) }; }
533
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
534
+ }
535
+
536
+ async diffRange(request) {
537
+ try { validatePath(request.path); return { ok: true, value: await getDiffRange(request.path, request) }; }
538
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
539
+ }
540
+
541
+ async reflog(request) {
542
+ try { validatePath(request.path); return { ok: true, value: await getReflog(request.path, request) }; }
543
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
544
+ }
545
+
546
+ // === v2 §2.3 P2:remote 增删改 + config 读写(返回新列表,便于客户端刷新) ===
547
+
548
+ async remoteAdd(request) {
549
+ try { validatePath(request.path); return { ok: true, value: await addRemote(request.path, request.name, request.url, request.pushUrl) }; }
550
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
551
+ }
552
+
553
+ async remoteRemove(request) {
554
+ try { validatePath(request.path); return { ok: true, value: await removeRemote(request.path, request.name) }; }
555
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
556
+ }
557
+
558
+ async remoteRename(request) {
559
+ try { validatePath(request.path); return { ok: true, value: await renameRemote(request.path, request.oldName, request.newName) }; }
560
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
561
+ }
562
+
563
+ async configList(request) {
564
+ try { validatePath(request.path); return { ok: true, value: await getConfig(request.path, !!request.global) }; }
565
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
566
+ }
567
+
568
+ async configSet(request) {
569
+ try { validatePath(request.path); return { ok: true, value: await setConfig(request.path, request.key, request.value, !!request.global) }; }
570
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
571
+ }
572
+
573
+ async configUnset(request) {
574
+ try { validatePath(request.path); return { ok: true, value: await unsetConfig(request.path, request.key, !!request.global) }; }
575
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
576
+ }
577
+
578
+ // === P3-A §8.1/§8.3:rebase 全家 + 行级暂存/撤销 ===
579
+
580
+ // todo 候选:base..HEAD(不含 base),最旧在前(rebase todo 应用顺序)
581
+ async rebasePlan(request) {
582
+ try { validatePath(request.path); return { ok: true, value: await rebasePlan(request.path, request.base) }; }
583
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
584
+ }
585
+
586
+ // entries 条目 action 枚举 + sha 白名单校验(防 todo 注入);
587
+ // 冲突/ edit 停驻不抛错:done=false + status,进 rebase 态
588
+ async rebaseRun(request) {
589
+ try { validatePath(request.path); return { ok: true, value: await rebaseRun(request.path, request.base, request.entries) }; }
590
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
591
+ }
592
+
593
+ // 用暂存改动修正任意历史提交(改写历史,UI 二次确认);squash+message 替换合并信息
594
+ async fixupCommit(request) {
595
+ try { validatePath(request.path); return { ok: true, value: await fixupCommit(request.path, request.sha, request.mode, request.message) }; }
596
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
597
+ }
598
+
599
+ // 当前分支 rebase 到 branch 之上(二次确认同 merge 级;冲突走冲突页)
600
+ async rebaseBranch(request) {
601
+ try { validatePath(request.path); return { ok: true, value: await rebaseBranch(request.path, request.branch) }; }
602
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
603
+ }
604
+
605
+ // 行级操作:scope/mode 组合白名单(unstaged/stage、staged/unstage、unstaged/discard)
606
+ async lineApply(request) {
607
+ try { validatePath(request.path); return { ok: true, value: { status: await applyLine(request.path, request) } }; }
608
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
609
+ }
610
+
611
+ // === 子仓库支持 P0:仓库树发现 ===
612
+
613
+ // 主仓库 + 全部 submodule(含未初始化)+ 直接子目录里的嵌套独立仓库。
614
+ // 必须在 root toplevel 跑(git submodule status 对 cwd 敏感)——这里先 probeRepo
615
+ // 拿 toplevel 再传下去,client 传子目录 cwd 也能得到「主仓库视角」的树。
616
+ // 返回 { repos: RepoNode[] },root 恒在 [0],其余按 relPath 字典序。
617
+ async repoTree(request) {
618
+ try {
619
+ validatePath(request.path);
620
+ const probe = await probeRepo(request.path);
621
+ if (!probe.isRepo) throw new GitError("exit", "path 不是 git 仓库:" + request.path);
622
+ return { ok: true, value: await listSubRepos(probe.toplevel) };
623
+ }
624
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
625
+ }
626
+
627
+ // === 子仓库支持 P1:sha 存在性门控 ===
628
+ // 历史 Tab 的 gitlink 变更(指针 a→b)在跳进子仓库前先问一句「a、b 在这个
629
+ // 子仓库的本地对象库里吗」。入参只接受纯十六进制 sha(git-core 内部再拼
630
+ // ^{commit}),非法形态直接抛错——不吞成 ok:false,否则客户端会把「参数写错」
631
+ // 误读成「提交不可用」而静默禁用跳转。
632
+ async revExists(request) {
633
+ try {
634
+ validatePath(request.path);
635
+ return { ok: true, value: await revExists(request.path, request.revs) };
636
+ }
637
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
638
+ }
639
+
640
+ // === 子仓库支持 P2:submodule 管理五方法 ===
641
+ // 入参 subPath 一律是「相对仓库根的路径」(git 的 submodule 子命令收的是路径,
642
+ // 不是 .gitmodules 注册名),git-core 侧过 safeJoin 防护;deinit 必填 subPath
643
+ // (缺省会清空全部子模块,core 层抛错)。返回值形状与现有 mutation 一致:
644
+ // 前四个 { status },submoduleAdd 额外带新子仓库的 probe。
645
+
646
+ // 注册子模块(把 .gitmodules 条目落到本地 config,checkout 出工作副本)
647
+ async submoduleInit(request) {
648
+ try {
649
+ validatePath(request.path);
650
+ return { ok: true, value: await submoduleInit(request.path, request.subPath) };
651
+ }
652
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
653
+ }
654
+
655
+ // 拉取/检出子模块内容;首次会 clone(走网络,core 侧用 runGitNet)
656
+ async submoduleUpdate(request) {
657
+ try {
658
+ validatePath(request.path);
659
+ return {
660
+ ok: true,
661
+ value: await submoduleUpdate(request.path, {
662
+ subPath: request.subPath,
663
+ init: !!request.init,
664
+ recursive: !!request.recursive,
665
+ }),
666
+ };
667
+ }
668
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
669
+ }
670
+
671
+ // 把 .gitmodules 里的 url 同步回本地 submodule.<name>.url
672
+ async submoduleSync(request) {
673
+ try {
674
+ validatePath(request.path);
675
+ return { ok: true, value: await submoduleSync(request.path, request.subPath) };
676
+ }
677
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
678
+ }
679
+
680
+ // 危险操作(清空子模块工作区目录 + 注销本地注册,.gitmodules 保留):UI 二次确认,
681
+ // git 对有改动的子模块在不带 force 时会拒绝,这里照常把 GitError 映射出去。
682
+ async submoduleDeinit(request) {
683
+ try {
684
+ validatePath(request.path);
685
+ return { ok: true, value: await submoduleDeinit(request.path, request.subPath, !!request.force) };
686
+ }
687
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
688
+ }
689
+
690
+ // 新增子模块(写 .gitmodules + 暂存 gitlink + clone);返回父仓库 status 与
691
+ // 新子仓库 probe,客户端可直接把新节点插进仓库树。
692
+ async submoduleAdd(request) {
693
+ try {
694
+ validatePath(request.path);
695
+ return {
696
+ ok: true,
697
+ value: await submoduleAdd(request.path, {
698
+ url: request.url,
699
+ subPath: request.subPath,
700
+ name: request.name,
701
+ }),
702
+ };
703
+ }
704
+ catch (e) { return { ok: false, error: mapGitError(e) }; }
705
+ }
706
+ }
707
+
708
+ // cordis loader 的 unwrapExports 取 `exports.default ?? exports`:ESM 命名空间
709
+ // 对象不是合法 plugin(报 "invalid plugin, expect function or object with an
710
+ // apply method, received object")。必须 default 导出 Service 类——
711
+ // memory-manager / archive-manager 同款,方案文档此前漏掉了这一行。
712
+ export default GitManagerService;