@laoyuehanni/dsh-git-worktree 0.4.13 → 0.4.15
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.md +128 -49
- package/README.zh.md +128 -49
- package/docs/DETAIL.md +18 -0
- package/docs/DETAIL.zh.md +18 -0
- package/icon.svg +22 -0
- package/lib/client.js +3063 -674
- package/lib/deferred-worktree.d.ts +104 -0
- package/lib/deferred-worktree.js +292 -0
- package/lib/git.d.ts +19 -0
- package/lib/git.js +81 -4
- package/lib/index.d.ts +13 -0
- package/lib/index.js +162 -4
- package/lib/normalize.d.ts +13 -0
- package/lib/normalize.js +51 -0
- package/lib/routes.d.ts +33 -2
- package/lib/routes.js +250 -79
- package/lib/wire.d.ts +99 -3
- package/lib/wire.js +11 -0
- package/locale/en.json +5 -1
- package/locale/zh.json +2 -1
- package/package.json +8 -3
package/lib/routes.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { basename, dirname, join, resolve } from 'node:path';
|
|
6
6
|
import { mkdir, readdir, rm, stat } from 'node:fs/promises';
|
|
7
7
|
import { runPostCreateCopy } from './copy-files.js';
|
|
8
|
-
import { appendWorktreeExclude, fsExcludeSeams, CancelledError, GitError, addWorktree, addWorktreeCutout, createBranch, cutoutBranchName, deleteBranch, fetchAll, fsDirExists, inspectWorktree, isAbsoluteDir, probeRepo, probeWorkspaceGit, removeWorktree, renameBranch, resolveBranch, switchBranch, updateBranch } from './git.js';
|
|
8
|
+
import { appendWorktreeExclude, fsExcludeSeams, CancelledError, GitError, addWorktree, addWorktreeCutout, createBranch, cutoutBranchName, deleteBranch, fetchAll, fsDirExists, inspectWorktree, isAbsoluteDir, probeRepo, probeWorkspaceGit, randomCutoutBranchName, removeWorktree, renameBranch, resolveBranch, switchBranch, updateBranch, writeWorktreeBaseBranch } from './git.js';
|
|
9
9
|
import { readCheckedOutBranch } from './head.js';
|
|
10
10
|
import { invalidateAuthorCache, readAuthors, readHistory, readCommit } from './history.js';
|
|
11
11
|
import { openPush, previewPush, pushBranch } from './push.js';
|
|
@@ -102,6 +102,8 @@ export async function handleGroupWorktrees(deps, body) {
|
|
|
102
102
|
}
|
|
103
103
|
/** The project-internal layout directory name every repository uses. */
|
|
104
104
|
const PROJECT_LAYOUT_DIR = join('.dsh', 'gitworktree');
|
|
105
|
+
/** The official DSH worktree layout directory inside a repository. */
|
|
106
|
+
const OFFICIAL_LAYOUT_DIR = join('.agents', 'worktrees');
|
|
105
107
|
/**
|
|
106
108
|
* Whether `canonical` sits DIRECTLY inside a `<repo>/.dsh/gitworktree`
|
|
107
109
|
* layout root whose repository actually exists on disk. The layout names its
|
|
@@ -117,6 +119,16 @@ function insideProjectLayout(canonical, dirExists) {
|
|
|
117
119
|
return false;
|
|
118
120
|
return dirExists(join(dirname(dsh), '.git'));
|
|
119
121
|
}
|
|
122
|
+
/** Whether `canonical` sits DIRECTLY inside a `<repo>/.agents/worktrees` directory. */
|
|
123
|
+
function insideOfficialLayout(canonical, dirExists) {
|
|
124
|
+
const layout = dirname(canonical);
|
|
125
|
+
if (basename(layout) !== 'worktrees')
|
|
126
|
+
return false;
|
|
127
|
+
const agents = dirname(layout);
|
|
128
|
+
if (basename(agents) !== '.agents')
|
|
129
|
+
return false;
|
|
130
|
+
return dirExists(join(dirname(agents), '.git'));
|
|
131
|
+
}
|
|
120
132
|
/**
|
|
121
133
|
* POST /worktrees-all — git facts for every DIRECT child directory of the two
|
|
122
134
|
* storage locations the plugin has ever used, merged:
|
|
@@ -175,7 +187,7 @@ export async function handleWorktreesAll(deps, body) {
|
|
|
175
187
|
const gitFacts = await probeWorkspaceGit(deps.exec, path);
|
|
176
188
|
return gitFacts === undefined
|
|
177
189
|
? { path, repoName: null, branch: null, source }
|
|
178
|
-
: { path, repoName: gitFacts.repoName, branch: gitFacts.branch, source };
|
|
190
|
+
: { path, repoRoot: gitFacts.repoRoot, repoName: gitFacts.repoName, branch: gitFacts.branch, source };
|
|
179
191
|
}
|
|
180
192
|
catch {
|
|
181
193
|
return { path, repoName: null, branch: null, source };
|
|
@@ -206,8 +218,12 @@ export async function handleWorktreesAll(deps, body) {
|
|
|
206
218
|
repoRoots.add(workspaceFacts.repoRoot);
|
|
207
219
|
}
|
|
208
220
|
}
|
|
221
|
+
const includeOfficial = deps.sectionManageOfficialWorktrees?.() === true;
|
|
209
222
|
for (const repoRoot of repoRoots) {
|
|
210
223
|
await scanLocation(join(repoRoot, PROJECT_LAYOUT_DIR), 'project');
|
|
224
|
+
if (includeOfficial) {
|
|
225
|
+
await scanLocation(join(repoRoot, OFFICIAL_LAYOUT_DIR), 'official');
|
|
226
|
+
}
|
|
211
227
|
}
|
|
212
228
|
return { status: 200, body: { worktrees: facts, legacyRoot, legacyRootExists, ...truncated ? { truncated } : {} } };
|
|
213
229
|
}
|
|
@@ -246,7 +262,11 @@ export async function handlePurgeDirectory(deps, body) {
|
|
|
246
262
|
}
|
|
247
263
|
const rootDir = resolveRootDir(deps.sectionRootDir(), deps.home(), deps.envHome());
|
|
248
264
|
const canonical = resolve(path);
|
|
249
|
-
|
|
265
|
+
const allowOfficialPurge = deps.sectionManageOfficialWorktrees?.() === true;
|
|
266
|
+
const dirExists = deps.dirExists ?? fsDirExists;
|
|
267
|
+
if (dirname(canonical) !== resolve(rootDir)
|
|
268
|
+
&& !insideProjectLayout(canonical, dirExists)
|
|
269
|
+
&& !(allowOfficialPurge && insideOfficialLayout(canonical, dirExists))) {
|
|
250
270
|
return fail(400, `"${canonical}" is outside the plugin's worktree storage locations`);
|
|
251
271
|
}
|
|
252
272
|
const statDirectory = deps.statDirectory ?? fsStatDirectory;
|
|
@@ -434,94 +454,139 @@ export async function handleCommit(deps, body) {
|
|
|
434
454
|
* @param deps - host dependencies.
|
|
435
455
|
* @param body - parsed request body.
|
|
436
456
|
*/
|
|
457
|
+
/**
|
|
458
|
+
* Shared worktree creation execution pipeline used by both immediate
|
|
459
|
+
* `POST /worktree` and deferred first-turn creation.
|
|
460
|
+
*/
|
|
461
|
+
export async function executeCreateWorktree(deps, spec, onPhase) {
|
|
462
|
+
const { repoPath, branch, cutout, random, name } = spec;
|
|
463
|
+
const facts = await probeRepo(deps.exec, repoPath, deps.dirExists);
|
|
464
|
+
if (facts === undefined) {
|
|
465
|
+
throw Object.assign(new Error(`"${repoPath}" is not inside a git repository`), { httpStatus: 400 });
|
|
466
|
+
}
|
|
467
|
+
const excludeWarning = await appendWorktreeExclude(deps.excludeSeams ?? fsExcludeSeams, facts.repoRoot);
|
|
468
|
+
const layoutRoot = join(facts.repoRoot, PROJECT_LAYOUT_DIR);
|
|
469
|
+
let fetchWarning;
|
|
470
|
+
let resolvedBranch;
|
|
471
|
+
let branchResolved = false;
|
|
472
|
+
if (deps.sectionFetchBeforeCreate() === true) {
|
|
473
|
+
resolvedBranch = await resolveBranch(deps.exec, facts.repoRoot, branch);
|
|
474
|
+
branchResolved = true;
|
|
475
|
+
if (resolvedBranch?.remote !== undefined) {
|
|
476
|
+
onPhase?.('fetch');
|
|
477
|
+
try {
|
|
478
|
+
await fetchAll(deps.exec, facts.repoRoot);
|
|
479
|
+
}
|
|
480
|
+
catch (error) {
|
|
481
|
+
fetchWarning = error instanceof GitError ? error.stderr.trim() : error instanceof Error ? error.message : String(error);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
const warnings = {
|
|
486
|
+
...(fetchWarning === undefined ? {} : { fetchWarning }),
|
|
487
|
+
...(excludeWarning === undefined ? {} : { excludeWarning }),
|
|
488
|
+
};
|
|
489
|
+
if (cutout) {
|
|
490
|
+
const custom = name?.trim();
|
|
491
|
+
if (custom !== undefined && custom !== '') {
|
|
492
|
+
if (custom.startsWith('-')) {
|
|
493
|
+
throw Object.assign(new Error('"name" must not start with "-"'), { httpStatus: 400 });
|
|
494
|
+
}
|
|
495
|
+
onPhase?.('create', custom);
|
|
496
|
+
const target = join(layoutRoot, sanitizeBranchDir(custom));
|
|
497
|
+
await mkdirSlot(deps, layoutRoot);
|
|
498
|
+
await addWorktreeCutout(deps.exec, facts.repoRoot, branch, custom, target);
|
|
499
|
+
await writeWorktreeBaseBranch(target, branch);
|
|
500
|
+
onPhase?.('copy', custom);
|
|
501
|
+
const copyWarning = await runPostCreateCopy(deps, facts.repoRoot, target);
|
|
502
|
+
return {
|
|
503
|
+
path: target,
|
|
504
|
+
branch: custom,
|
|
505
|
+
created: true,
|
|
506
|
+
...warnings,
|
|
507
|
+
...(copyWarning === undefined ? {} : { copyWarning }),
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
const dirExists = deps.dirExists ?? fsDirExists;
|
|
511
|
+
const folderTaken = (candidate) => dirExists(join(layoutRoot, sanitizeBranchDir(candidate)));
|
|
512
|
+
const newBranch = random === true
|
|
513
|
+
? await randomCutoutBranchName(deps.exec, facts.repoRoot, branch, folderTaken, deps.randomHex)
|
|
514
|
+
: await cutoutBranchName(deps.exec, facts.repoRoot, branch, folderTaken);
|
|
515
|
+
onPhase?.('create', newBranch);
|
|
516
|
+
const target = join(layoutRoot, sanitizeBranchDir(newBranch));
|
|
517
|
+
await mkdirSlot(deps, layoutRoot);
|
|
518
|
+
await addWorktreeCutout(deps.exec, facts.repoRoot, branch, newBranch, target);
|
|
519
|
+
await writeWorktreeBaseBranch(target, branch);
|
|
520
|
+
onPhase?.('copy', newBranch);
|
|
521
|
+
const copyWarning = await runPostCreateCopy(deps, facts.repoRoot, target);
|
|
522
|
+
return {
|
|
523
|
+
path: target,
|
|
524
|
+
branch: newBranch,
|
|
525
|
+
created: true,
|
|
526
|
+
...warnings,
|
|
527
|
+
...(copyWarning === undefined ? {} : { copyWarning }),
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
if (!branchResolved) {
|
|
531
|
+
resolvedBranch = await resolveBranch(deps.exec, facts.repoRoot, branch);
|
|
532
|
+
}
|
|
533
|
+
const isRemote = resolvedBranch?.remote !== undefined;
|
|
534
|
+
const localName = resolvedBranch?.local ?? branch;
|
|
535
|
+
onPhase?.('create', localName);
|
|
536
|
+
const target = join(layoutRoot, sanitizeBranchDir(branch));
|
|
537
|
+
await mkdirSlot(deps, layoutRoot);
|
|
538
|
+
const result = await addWorktree(deps.exec, facts.repoRoot, branch, target, deps.dirExists);
|
|
539
|
+
let copyWarning;
|
|
540
|
+
if (result.created) {
|
|
541
|
+
if (isRemote)
|
|
542
|
+
await writeWorktreeBaseBranch(result.path, branch);
|
|
543
|
+
onPhase?.('copy', localName);
|
|
544
|
+
copyWarning = await runPostCreateCopy(deps, facts.repoRoot, result.path);
|
|
545
|
+
}
|
|
546
|
+
return {
|
|
547
|
+
path: result.path,
|
|
548
|
+
branch: localName,
|
|
549
|
+
created: result.created,
|
|
550
|
+
...warnings,
|
|
551
|
+
...(copyWarning === undefined ? {} : { copyWarning }),
|
|
552
|
+
};
|
|
553
|
+
}
|
|
437
554
|
export async function handleCreateWorktree(deps, body) {
|
|
438
|
-
const parsed = readBody(body, ['repoPath', 'branch'], ['cutout'], ['name']);
|
|
555
|
+
const parsed = readBody(body, ['repoPath', 'branch'], ['cutout', 'random'], ['name']);
|
|
439
556
|
if (isOutcome(parsed))
|
|
440
557
|
return parsed;
|
|
441
|
-
const { repoPath, branch, cutout, name } = parsed;
|
|
558
|
+
const { repoPath, branch, cutout, random, name } = parsed;
|
|
442
559
|
if (!isAbsoluteDir(repoPath))
|
|
443
560
|
return fail(400, '"repoPath" must be an absolute directory');
|
|
444
561
|
if (branch.trim() === '')
|
|
445
562
|
return fail(400, '"branch" must be non-empty');
|
|
446
563
|
try {
|
|
447
|
-
const
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
// rides the successful response as `fetchWarning` for the client to
|
|
465
|
-
// toast.
|
|
466
|
-
let fetchWarning;
|
|
467
|
-
if (deps.sectionFetchBeforeCreate() === true) {
|
|
468
|
-
const resolved = await resolveBranch(deps.exec, facts.repoRoot, branch);
|
|
469
|
-
if (resolved?.remote !== undefined) {
|
|
470
|
-
try {
|
|
471
|
-
await fetchAll(deps.exec, facts.repoRoot);
|
|
472
|
-
}
|
|
473
|
-
catch (error) {
|
|
474
|
-
fetchWarning = error instanceof GitError ? error.stderr.trim() : error instanceof Error ? error.message : String(error);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
const warnings = { ...fetchWarning === undefined ? {} : { fetchWarning }, ...excludeWarning === undefined ? {} : { excludeWarning } };
|
|
479
|
-
if (cutout === true) {
|
|
480
|
-
// An explicit name skips the `-wt` suffix walk and is used verbatim —
|
|
481
|
-
// both for the branch and the storage folder. A leading dash would
|
|
482
|
-
// ride `worktree add -b` as a flag; any other git-invalid name
|
|
483
|
-
// surfaces through the error envelope (the client pre-flights).
|
|
484
|
-
const custom = name?.trim();
|
|
485
|
-
if (custom !== undefined && custom !== '') {
|
|
486
|
-
if (custom.startsWith('-'))
|
|
487
|
-
return fail(400, '"name" must not start with "-"');
|
|
488
|
-
const target = join(layoutRoot, sanitizeBranchDir(custom));
|
|
489
|
-
await mkdirSlot(deps, layoutRoot);
|
|
490
|
-
await addWorktreeCutout(deps.exec, facts.repoRoot, branch, custom, target);
|
|
491
|
-
const copyWarning = await runPostCreateCopy(deps, facts.repoRoot, target);
|
|
492
|
-
const copyWarnObj = copyWarning !== undefined ? { copyWarning } : {};
|
|
493
|
-
return { status: 200, body: { path: target, created: true, ...warnings, ...copyWarnObj } };
|
|
494
|
-
}
|
|
495
|
-
// The new branch name must be known before the folder name can be
|
|
496
|
-
// computed: the folder carries the NEW branch's directory name. The
|
|
497
|
-
// name must be free in BOTH namespaces — the branch table and the
|
|
498
|
-
// storage folder: a leftover folder of a since-deleted branch would
|
|
499
|
-
// otherwise fail `worktree add` with a bare "already exists", so the
|
|
500
|
-
// suffix walk probes the folder too.
|
|
501
|
-
const dirExists = deps.dirExists ?? fsDirExists;
|
|
502
|
-
const newBranch = await cutoutBranchName(deps.exec, facts.repoRoot, branch, (candidate) => dirExists(join(layoutRoot, sanitizeBranchDir(candidate))));
|
|
503
|
-
const target = join(layoutRoot, sanitizeBranchDir(newBranch));
|
|
504
|
-
await mkdirSlot(deps, layoutRoot);
|
|
505
|
-
await addWorktreeCutout(deps.exec, facts.repoRoot, branch, newBranch, target);
|
|
506
|
-
const copyWarning = await runPostCreateCopy(deps, facts.repoRoot, target);
|
|
507
|
-
const copyWarnObj = copyWarning !== undefined ? { copyWarning } : {};
|
|
508
|
-
return { status: 200, body: { path: target, created: true, ...warnings, ...copyWarnObj } };
|
|
509
|
-
}
|
|
510
|
-
// The folder name carries the branch: `<repo>/.dsh/gitworktree/<branch>`.
|
|
511
|
-
const target = join(layoutRoot, sanitizeBranchDir(branch));
|
|
512
|
-
await mkdirSlot(deps, layoutRoot);
|
|
513
|
-
const result = await addWorktree(deps.exec, facts.repoRoot, branch, target, deps.dirExists);
|
|
514
|
-
let copyWarning;
|
|
515
|
-
if (result.created) {
|
|
516
|
-
copyWarning = await runPostCreateCopy(deps, facts.repoRoot, result.path);
|
|
517
|
-
}
|
|
518
|
-
const copyWarnObj = copyWarning !== undefined ? { copyWarning } : {};
|
|
519
|
-
return { status: 200, body: { ...result, ...warnings, ...copyWarnObj } };
|
|
564
|
+
const outcome = await executeCreateWorktree(deps, {
|
|
565
|
+
repoPath,
|
|
566
|
+
branch,
|
|
567
|
+
cutout: cutout === true,
|
|
568
|
+
...(name === undefined ? {} : { name }),
|
|
569
|
+
...(random === undefined ? {} : { random }),
|
|
570
|
+
});
|
|
571
|
+
return {
|
|
572
|
+
status: 200,
|
|
573
|
+
body: {
|
|
574
|
+
path: outcome.path,
|
|
575
|
+
created: outcome.created,
|
|
576
|
+
...(outcome.fetchWarning === undefined ? {} : { fetchWarning: outcome.fetchWarning }),
|
|
577
|
+
...(outcome.excludeWarning === undefined ? {} : { excludeWarning: outcome.excludeWarning }),
|
|
578
|
+
...(outcome.copyWarning === undefined ? {} : { copyWarning: outcome.copyWarning }),
|
|
579
|
+
},
|
|
580
|
+
};
|
|
520
581
|
}
|
|
521
582
|
catch (error) {
|
|
522
583
|
if (error instanceof GitError)
|
|
523
584
|
return gitFailure(error);
|
|
524
|
-
|
|
585
|
+
const status = typeof error === 'object' && error !== null && 'httpStatus' in error
|
|
586
|
+
&& typeof error.httpStatus === 'number'
|
|
587
|
+
? error.httpStatus
|
|
588
|
+
: 500;
|
|
589
|
+
return fail(status, error instanceof Error ? error.message : String(error));
|
|
525
590
|
}
|
|
526
591
|
}
|
|
527
592
|
/**
|
|
@@ -1005,3 +1070,109 @@ export async function handleEnsureDirectory(deps, body) {
|
|
|
1005
1070
|
return fail(500, error instanceof Error ? error.message : String(error));
|
|
1006
1071
|
}
|
|
1007
1072
|
}
|
|
1073
|
+
/**
|
|
1074
|
+
* POST /session-cwd — move a session's working directory to an existing
|
|
1075
|
+
* directory via the host `workingDirectory` service. Used by the shared
|
|
1076
|
+
* workspace mode (`switchSessionDirectory`) so a blank session stays under
|
|
1077
|
+
* its parent repository workspace while working inside a linked worktree.
|
|
1078
|
+
* @param deps - host dependencies.
|
|
1079
|
+
* @param body - parsed request body: `{ sessionId, path }`.
|
|
1080
|
+
*/
|
|
1081
|
+
export async function handleSetSessionDirectory(deps, body) {
|
|
1082
|
+
const parsed = readBody(body, ['sessionId', 'path']);
|
|
1083
|
+
if (isOutcome(parsed))
|
|
1084
|
+
return parsed;
|
|
1085
|
+
const sessionId = parsed.sessionId.trim();
|
|
1086
|
+
const { path } = parsed;
|
|
1087
|
+
if (sessionId === '')
|
|
1088
|
+
return fail(400, '"sessionId" must be non-empty');
|
|
1089
|
+
if (!isAbsoluteDir(path))
|
|
1090
|
+
return fail(400, '"path" must be an absolute directory');
|
|
1091
|
+
if (deps.setSessionDirectory === undefined) {
|
|
1092
|
+
return fail(501, 'Session working directory switching is not supported by this host');
|
|
1093
|
+
}
|
|
1094
|
+
const canonical = resolve(path);
|
|
1095
|
+
const statDirectory = deps.statDirectory ?? fsStatDirectory;
|
|
1096
|
+
if (!(await statDirectory(canonical))) {
|
|
1097
|
+
return fail(400, `"${canonical}" is not an existing directory`);
|
|
1098
|
+
}
|
|
1099
|
+
try {
|
|
1100
|
+
const committed = await deps.setSessionDirectory(sessionId, canonical);
|
|
1101
|
+
return { status: 200, body: { sessionId, path: committed } };
|
|
1102
|
+
}
|
|
1103
|
+
catch (error) {
|
|
1104
|
+
const status = typeof error === 'object' && error !== null && 'httpStatus' in error
|
|
1105
|
+
&& typeof error.httpStatus === 'number'
|
|
1106
|
+
? error.httpStatus
|
|
1107
|
+
: 500;
|
|
1108
|
+
return fail(status, error instanceof Error ? error.message : String(error));
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
/** GET /deferred?sessionId=<id> — read a session's current deferred worktree state. */
|
|
1112
|
+
export async function handleGetDeferredWorktree(deps, sessionId) {
|
|
1113
|
+
if (sessionId === undefined || sessionId.trim() === '') {
|
|
1114
|
+
return fail(400, 'query parameter "sessionId" must be non-empty');
|
|
1115
|
+
}
|
|
1116
|
+
const state = deps.deferredWorktrees?.get(sessionId.trim()) ?? null;
|
|
1117
|
+
return { status: 200, body: { state } };
|
|
1118
|
+
}
|
|
1119
|
+
/** POST /deferred/arm — arm a deferred worktree creation for a blank session. */
|
|
1120
|
+
export async function handleArmDeferredWorktree(deps, body) {
|
|
1121
|
+
const parsed = readBody(body, ['sessionId', 'repoPath', 'branch'], ['cutout', 'random'], ['name']);
|
|
1122
|
+
if (isOutcome(parsed))
|
|
1123
|
+
return parsed;
|
|
1124
|
+
const sessionId = parsed.sessionId.trim();
|
|
1125
|
+
if (sessionId === '')
|
|
1126
|
+
return fail(400, '"sessionId" must be non-empty');
|
|
1127
|
+
if (!isAbsoluteDir(parsed.repoPath))
|
|
1128
|
+
return fail(400, '"repoPath" must be an absolute directory');
|
|
1129
|
+
if (parsed.branch.trim() === '')
|
|
1130
|
+
return fail(400, '"branch" must be non-empty');
|
|
1131
|
+
const custom = parsed.name?.trim();
|
|
1132
|
+
if (custom !== undefined && custom !== '' && custom.startsWith('-')) {
|
|
1133
|
+
return fail(400, '"name" must not start with "-"');
|
|
1134
|
+
}
|
|
1135
|
+
if (deps.deferredWorktrees === undefined) {
|
|
1136
|
+
return fail(501, 'Deferred worktree creation is not supported by this host');
|
|
1137
|
+
}
|
|
1138
|
+
try {
|
|
1139
|
+
const state = deps.deferredWorktrees.arm(sessionId, {
|
|
1140
|
+
repoPath: parsed.repoPath,
|
|
1141
|
+
branch: parsed.branch,
|
|
1142
|
+
cutout: parsed.cutout === true,
|
|
1143
|
+
...(custom !== undefined && custom !== '' ? { name: custom } : {}),
|
|
1144
|
+
...(parsed.random === true ? { random: true } : {}),
|
|
1145
|
+
});
|
|
1146
|
+
return { status: 200, body: { state } };
|
|
1147
|
+
}
|
|
1148
|
+
catch (error) {
|
|
1149
|
+
const status = typeof error === 'object' && error !== null && 'httpStatus' in error
|
|
1150
|
+
&& typeof error.httpStatus === 'number'
|
|
1151
|
+
? error.httpStatus
|
|
1152
|
+
: 500;
|
|
1153
|
+
return fail(status, error instanceof Error ? error.message : String(error));
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
/** POST /deferred/action — cancel, retry, or withdraw a deferred worktree. */
|
|
1157
|
+
export async function handleDeferredWorktreeAction(deps, body) {
|
|
1158
|
+
const parsed = readBody(body, ['sessionId', 'action']);
|
|
1159
|
+
if (isOutcome(parsed))
|
|
1160
|
+
return parsed;
|
|
1161
|
+
const sessionId = parsed.sessionId.trim();
|
|
1162
|
+
if (sessionId === '')
|
|
1163
|
+
return fail(400, '"sessionId" must be non-empty');
|
|
1164
|
+
const action = parsed.action;
|
|
1165
|
+
if (action !== 'cancel' && action !== 'retry' && action !== 'withdraw') {
|
|
1166
|
+
return fail(400, '"action" must be "cancel", "retry", or "withdraw"');
|
|
1167
|
+
}
|
|
1168
|
+
if (deps.deferredWorktrees === undefined) {
|
|
1169
|
+
return fail(501, 'Deferred worktree creation is not supported by this host');
|
|
1170
|
+
}
|
|
1171
|
+
if (action === 'cancel')
|
|
1172
|
+
deps.deferredWorktrees.cancel(sessionId);
|
|
1173
|
+
else if (action === 'retry')
|
|
1174
|
+
deps.deferredWorktrees.retry(sessionId);
|
|
1175
|
+
else
|
|
1176
|
+
deps.deferredWorktrees.withdraw(sessionId);
|
|
1177
|
+
return { status: 200, body: { state: deps.deferredWorktrees.get(sessionId) } };
|
|
1178
|
+
}
|
package/lib/wire.d.ts
CHANGED
|
@@ -107,6 +107,17 @@ export declare const ROUTE_WORKTREES_ALL = "/plugin/git-worktree/worktrees-all";
|
|
|
107
107
|
/** POST ROUTE_PREFIX/purge — delete a NON-git directory sitting DIRECTLY in
|
|
108
108
|
* the storage root (an orphaned leftover whose .git is already gone). */
|
|
109
109
|
export declare const ROUTE_PURGE = "/plugin/git-worktree/purge";
|
|
110
|
+
/** POST ROUTE_PREFIX/session-cwd — move an active blank session's working
|
|
111
|
+
* directory to a worktree directory via the host `workingDirectory` service. */
|
|
112
|
+
export declare const ROUTE_SESSION_CWD = "/plugin/git-worktree/session-cwd";
|
|
113
|
+
/** GET ROUTE_PREFIX/deferred?sessionId=<id> — read a session's deferred worktree state. */
|
|
114
|
+
export declare const ROUTE_DEFERRED = "/plugin/git-worktree/deferred";
|
|
115
|
+
/** GET ROUTE_PREFIX/deferred/events?sessionId=<id> — SSE stream of deferred worktree updates. */
|
|
116
|
+
export declare const ROUTE_DEFERRED_EVENTS = "/plugin/git-worktree/deferred/events";
|
|
117
|
+
/** POST ROUTE_PREFIX/deferred/arm — arm a deferred worktree creation for a blank session. */
|
|
118
|
+
export declare const ROUTE_DEFERRED_ARM = "/plugin/git-worktree/deferred/arm";
|
|
119
|
+
/** POST ROUTE_PREFIX/deferred/action — cancel, retry, or withdraw a deferred worktree. */
|
|
120
|
+
export declare const ROUTE_DEFERRED_ACTION = "/plugin/git-worktree/deferred/action";
|
|
110
121
|
/** One selectable branch row. */
|
|
111
122
|
export interface BranchEntry {
|
|
112
123
|
/** Display name: a bare local name (`main`) or `<remote>/<name>`. */
|
|
@@ -231,8 +242,12 @@ export interface CreateWorktreeBody {
|
|
|
231
242
|
* worktree. The storage folder is named after the new branch. */
|
|
232
243
|
cutout?: boolean;
|
|
233
244
|
/** Cutout only: explicit name for the NEW branch. Default (absent):
|
|
234
|
-
* derived `<branch>-wt`, suffixing past taken names
|
|
245
|
+
* derived `<branch>-wt`, suffixing past taken names (or random `<stem>-<4hex>`
|
|
246
|
+
* when `random` is true). */
|
|
235
247
|
name?: string;
|
|
248
|
+
/** Cutout only (when `name` is omitted): generate a random `<stem>-<4hex>`
|
|
249
|
+
* branch name instead of the sequential `<branch>-wt` suffix. */
|
|
250
|
+
random?: boolean;
|
|
236
251
|
}
|
|
237
252
|
/** POST worktree response body. */
|
|
238
253
|
export interface CreateWorktreeResult {
|
|
@@ -387,6 +402,8 @@ export interface WorkspaceGitFacts {
|
|
|
387
402
|
branch: string | null;
|
|
388
403
|
/** True when this directory IS the main worktree (holds the shared .git). */
|
|
389
404
|
main: boolean;
|
|
405
|
+
/** Optional base branch recorded when this linked worktree was created from another branch. */
|
|
406
|
+
baseBranch?: string;
|
|
390
407
|
}
|
|
391
408
|
/** POST group request body. */
|
|
392
409
|
export interface GroupWorkspacesBody {
|
|
@@ -470,6 +487,9 @@ export interface WorktreesAllBody {
|
|
|
470
487
|
export interface WorktreeScanEntry {
|
|
471
488
|
/** Absolute directory path. */
|
|
472
489
|
path: string;
|
|
490
|
+
/** Canonical main repository root directory; absent for unrecognized/orphan
|
|
491
|
+
* directories or older host builds. */
|
|
492
|
+
repoRoot?: string;
|
|
473
493
|
/** Main repository directory basename; null = the directory is not inside
|
|
474
494
|
* a git repository (an orphan or foreign folder — shown as unrecognized,
|
|
475
495
|
* never deleted, never counted against the prune cap). */
|
|
@@ -479,8 +499,10 @@ export interface WorktreeScanEntry {
|
|
|
479
499
|
branch: string | null;
|
|
480
500
|
/** Where the directory lives: `project` = inside `<repo>/.dsh/gitworktree`
|
|
481
501
|
* (the current layout, auto-nested under its repository by the workspace
|
|
482
|
-
* tree), `
|
|
483
|
-
|
|
502
|
+
* tree), `official` = inside `<repo>/.agents/worktrees` (when official
|
|
503
|
+
* worktree management is enabled), `legacy` = inside the historical central
|
|
504
|
+
* storage root. */
|
|
505
|
+
source: 'legacy' | 'project' | 'official';
|
|
484
506
|
}
|
|
485
507
|
/** POST worktrees-all response body — one entry per DIRECT child of each
|
|
486
508
|
* scanned storage location; a missing location answers its half as an empty
|
|
@@ -512,3 +534,77 @@ export interface PurgeDirectoryResult {
|
|
|
512
534
|
path: string;
|
|
513
535
|
removed: boolean;
|
|
514
536
|
}
|
|
537
|
+
/** POST session-cwd request body. */
|
|
538
|
+
export interface SetSessionDirectoryBody {
|
|
539
|
+
/** Target session id whose working directory should move. */
|
|
540
|
+
sessionId: string;
|
|
541
|
+
/** Existing absolute directory path (typically a created or reused worktree). */
|
|
542
|
+
path: string;
|
|
543
|
+
}
|
|
544
|
+
/** POST session-cwd response body. */
|
|
545
|
+
export interface SetSessionDirectoryResult {
|
|
546
|
+
/** Target session id. */
|
|
547
|
+
sessionId: string;
|
|
548
|
+
/** Canonical absolute directory committed by the host workingDirectory service. */
|
|
549
|
+
path: string;
|
|
550
|
+
}
|
|
551
|
+
/** Phases reported while a deferred worktree creation runs on the first user turn. */
|
|
552
|
+
export type DeferredWorktreePhase = 'fetch' | 'create' | 'copy' | 'switch';
|
|
553
|
+
/** Specification of a deferred worktree creation armed on a blank session. */
|
|
554
|
+
export interface DeferredWorktreeSpec {
|
|
555
|
+
/** Main repository path (or any path inside the repository). */
|
|
556
|
+
readonly repoPath: string;
|
|
557
|
+
/** Target branch or base branch (for cutout). */
|
|
558
|
+
readonly branch: string;
|
|
559
|
+
/** Whether to cut a new branch out of `branch`. */
|
|
560
|
+
readonly cutout: boolean;
|
|
561
|
+
/** Explicit new branch name (cutout only); absent when auto/random. */
|
|
562
|
+
readonly name?: string;
|
|
563
|
+
/** Generate a random `<stem>-<4hex>` branch name when `cutout` is true and `name` is absent. */
|
|
564
|
+
readonly random?: boolean;
|
|
565
|
+
}
|
|
566
|
+
/** Live state of a deferred worktree for one session. */
|
|
567
|
+
export type DeferredWorktreeState = {
|
|
568
|
+
readonly kind: 'armed';
|
|
569
|
+
readonly spec: DeferredWorktreeSpec;
|
|
570
|
+
} | {
|
|
571
|
+
readonly kind: 'running';
|
|
572
|
+
readonly spec: DeferredWorktreeSpec;
|
|
573
|
+
readonly phase: DeferredWorktreePhase;
|
|
574
|
+
/** Resolved branch name once known. */
|
|
575
|
+
readonly resolvedBranch?: string;
|
|
576
|
+
} | {
|
|
577
|
+
readonly kind: 'failed';
|
|
578
|
+
readonly spec: DeferredWorktreeSpec;
|
|
579
|
+
readonly phase: DeferredWorktreePhase;
|
|
580
|
+
readonly message: string;
|
|
581
|
+
readonly resolvedBranch?: string;
|
|
582
|
+
};
|
|
583
|
+
/** One-shot completion notification emitted when a deferred creation succeeds. */
|
|
584
|
+
export interface DeferredWorktreeCompleted {
|
|
585
|
+
readonly path: string;
|
|
586
|
+
readonly branch: string;
|
|
587
|
+
readonly created: boolean;
|
|
588
|
+
readonly fetchWarning?: string;
|
|
589
|
+
readonly excludeWarning?: string;
|
|
590
|
+
readonly copyWarning?: string;
|
|
591
|
+
}
|
|
592
|
+
/** Snapshot / SSE payload pushed for a session's deferred worktree. */
|
|
593
|
+
export interface DeferredWorktreeSnapshot {
|
|
594
|
+
readonly state: DeferredWorktreeState | null;
|
|
595
|
+
readonly completed?: DeferredWorktreeCompleted;
|
|
596
|
+
}
|
|
597
|
+
/** POST deferred/arm request body. */
|
|
598
|
+
export interface ArmDeferredWorktreeBody {
|
|
599
|
+
readonly sessionId: string;
|
|
600
|
+
readonly repoPath: string;
|
|
601
|
+
readonly branch: string;
|
|
602
|
+
readonly cutout?: boolean;
|
|
603
|
+
readonly name?: string;
|
|
604
|
+
readonly random?: boolean;
|
|
605
|
+
}
|
|
606
|
+
/** POST deferred/action request body. */
|
|
607
|
+
export interface DeferredWorktreeActionBody {
|
|
608
|
+
readonly sessionId: string;
|
|
609
|
+
readonly action: 'cancel' | 'retry' | 'withdraw';
|
|
610
|
+
}
|
package/lib/wire.js
CHANGED
|
@@ -59,5 +59,16 @@ export const ROUTE_WORKTREES_ALL = `${ROUTE_PREFIX}/worktrees-all`;
|
|
|
59
59
|
/** POST ROUTE_PREFIX/purge — delete a NON-git directory sitting DIRECTLY in
|
|
60
60
|
* the storage root (an orphaned leftover whose .git is already gone). */
|
|
61
61
|
export const ROUTE_PURGE = `${ROUTE_PREFIX}/purge`;
|
|
62
|
+
/** POST ROUTE_PREFIX/session-cwd — move an active blank session's working
|
|
63
|
+
* directory to a worktree directory via the host `workingDirectory` service. */
|
|
64
|
+
export const ROUTE_SESSION_CWD = `${ROUTE_PREFIX}/session-cwd`;
|
|
65
|
+
/** GET ROUTE_PREFIX/deferred?sessionId=<id> — read a session's deferred worktree state. */
|
|
66
|
+
export const ROUTE_DEFERRED = `${ROUTE_PREFIX}/deferred`;
|
|
67
|
+
/** GET ROUTE_PREFIX/deferred/events?sessionId=<id> — SSE stream of deferred worktree updates. */
|
|
68
|
+
export const ROUTE_DEFERRED_EVENTS = `${ROUTE_PREFIX}/deferred/events`;
|
|
69
|
+
/** POST ROUTE_PREFIX/deferred/arm — arm a deferred worktree creation for a blank session. */
|
|
70
|
+
export const ROUTE_DEFERRED_ARM = `${ROUTE_PREFIX}/deferred/arm`;
|
|
71
|
+
/** POST ROUTE_PREFIX/deferred/action — cancel, retry, or withdraw a deferred worktree. */
|
|
72
|
+
export const ROUTE_DEFERRED_ACTION = `${ROUTE_PREFIX}/deferred/action`;
|
|
62
73
|
/** Content type of a streamed route: one JSON {@link SyncFrame} per line. */
|
|
63
74
|
export const NDJSON_TYPE = 'application/x-ndjson';
|
package/locale/en.json
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"meta": {
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Git worktree",
|
|
4
|
+
"description": "A branch menu in the composer tool row: inspect and switch branches in place, right-click to create, rename, delete, or push, and browse history in the commit graph; tick Worktree on a blank session to start it inside its own Git worktree in the repository.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
3
7
|
}
|
package/locale/zh.json
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@laoyuehanni/dsh-git-worktree",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.15",
|
|
4
4
|
"description": "DSH plugin: branch visibility and git worktree isolation for Web UI sessions",
|
|
5
|
+
"icon": "./icon.svg",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"dsh",
|
|
7
8
|
"deepseek",
|
|
@@ -42,10 +43,13 @@
|
|
|
42
43
|
"files": [
|
|
43
44
|
"lib",
|
|
44
45
|
"locale",
|
|
46
|
+
"icon.svg",
|
|
45
47
|
"!lib/**/*.map",
|
|
46
48
|
"cordis.patch.yml",
|
|
47
49
|
"README.md",
|
|
48
|
-
"README.zh.md"
|
|
50
|
+
"README.zh.md",
|
|
51
|
+
"docs/DETAIL.md",
|
|
52
|
+
"docs/DETAIL.zh.md"
|
|
49
53
|
],
|
|
50
54
|
"publishConfig": {
|
|
51
55
|
"registry": "https://registry.npmjs.org/",
|
|
@@ -136,7 +140,8 @@
|
|
|
136
140
|
"zustand": "~4.4.7"
|
|
137
141
|
},
|
|
138
142
|
"engines": {
|
|
139
|
-
"node": ">=22.19"
|
|
143
|
+
"node": ">=22.19",
|
|
144
|
+
"dsh": ">=0.2.0-rc.1"
|
|
140
145
|
},
|
|
141
146
|
"packageManager": "pnpm@11.8.0"
|
|
142
147
|
}
|