@laoyuehanni/dsh-git-worktree 0.4.3 → 0.4.5

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/lib/routes.js CHANGED
@@ -3,10 +3,23 @@
3
3
  * shell (index.ts) owns req/res mechanics; everything testable lives here.
4
4
  */
5
5
  import { dirname, join, resolve } from 'node:path';
6
- import { mkdir, stat } from 'node:fs/promises';
7
- import { GitError, addWorktree, addWorktreeCutout, createBranch, cutoutBranchName, fetchAll, fsDirExists, inspectWorktree, isAbsoluteDir, probeRepo, probeWorkspaceGit, removeWorktree, switchBranch, updateBranch } from './git.js';
6
+ import { mkdir, readdir, rm, stat } from 'node:fs/promises';
7
+ import { GitError, addWorktree, addWorktreeCutout, createBranch, cutoutBranchName, deleteBranch, fetchAll, fsDirExists, inspectWorktree, isAbsoluteDir, probeRepo, probeWorkspaceGit, removeWorktree, renameBranch, resolveBranch, switchBranch, updateBranch } from './git.js';
8
8
  import { isAbsoluteConfigPath, sanitizeBranchDir } from './normalize.js';
9
9
  import { resolveRootDir } from './settings.js';
10
+ /** Real fs-backed one-level directory listing (names only). Plain FILES are
11
+ * filtered out — the storage root can carry strays (a legacy settings.json
12
+ * was seen in the wild), and the scan lists worktree SLOTS, not entries. */
13
+ export async function fsListDir(path) {
14
+ try {
15
+ const dirents = await readdir(path, { withFileTypes: true });
16
+ return dirents.filter(entry => entry.isDirectory()).map(entry => entry.name);
17
+ }
18
+ catch {
19
+ // A missing storage root is the "no worktrees yet" answer, not an error.
20
+ return [];
21
+ }
22
+ }
10
23
  /** Real fs-backed directory probe: exists and is a directory. */
11
24
  export async function fsStatDirectory(path) {
12
25
  try {
@@ -31,6 +44,12 @@ const GROUP_PATHS_LIMIT = 256;
31
44
  * the batch can ride wider than the old three-spawn shape without stacking
32
45
  * an unreasonable number of concurrent git processes. */
33
46
  const GROUP_BATCH_SIZE = 16;
47
+ /** Upper bound on children one /worktrees-all scan probes. The storage root
48
+ * is this plugin's own slot directory, so a healthy one stays far below;
49
+ * the cap exists so a root pointed at something enormous (a misconfigured
50
+ * rootDir, a home directory) cannot turn one dialog open into thousands of
51
+ * git spawns. Past it the scan answers its first slice plus `truncated`. */
52
+ const SCAN_CHILDREN_LIMIT = 512;
34
53
  /**
35
54
  * POST /group — git belonging facts for a batch of workspace directories.
36
55
  * Deduplicates, validates, probes in bounded batches, and answers 200 with
@@ -76,6 +95,94 @@ export async function handleGroupWorktrees(deps, body) {
76
95
  }
77
96
  return { status: 200, body: { facts } };
78
97
  }
98
+ /**
99
+ * POST /worktrees-all — git facts for every DIRECT child directory of the
100
+ * resolved worktree storage root (the slots this plugin plans as
101
+ * `<repoName>-<branch>`). The management dialog's data source: one scan
102
+ * answers for all repositories at once, orphan directories included (a
103
+ * child that probes as no git repository comes back with null facts rather
104
+ * than being dropped — the dialog shows it as unrecognized). A missing
105
+ * storage root answers an empty list; per-child probe failures degrade to
106
+ * null facts, never a 500 — the dialog must render, not error out. A root
107
+ * holding more than {@link SCAN_CHILDREN_LIMIT} children answers its first
108
+ * slice plus `truncated`, so a misconfigured rootDir cannot turn one dialog
109
+ * open into thousands of git spawns.
110
+ * @param deps - host dependencies.
111
+ */
112
+ export async function handleWorktreesAll(deps) {
113
+ const rootDir = resolveRootDir(deps.sectionRootDir(), deps.home(), deps.envHome());
114
+ const listDir = deps.listDir ?? fsListDir;
115
+ const listed = await listDir(rootDir);
116
+ const truncated = listed.length > SCAN_CHILDREN_LIMIT;
117
+ const children = truncated ? listed.slice(0, SCAN_CHILDREN_LIMIT) : listed;
118
+ const facts = [];
119
+ for (let start = 0; start < children.length; start += GROUP_BATCH_SIZE) {
120
+ const batch = children.slice(start, start + GROUP_BATCH_SIZE);
121
+ const probed = await Promise.all(batch.map(async (child) => {
122
+ const path = join(rootDir, child);
123
+ try {
124
+ const gitFacts = await probeWorkspaceGit(deps.exec, path);
125
+ return gitFacts === undefined
126
+ ? { path, repoName: null, branch: null }
127
+ : { path, repoName: gitFacts.repoName, branch: gitFacts.branch };
128
+ }
129
+ catch {
130
+ return { path, repoName: null, branch: null };
131
+ }
132
+ }));
133
+ facts.push(...probed);
134
+ }
135
+ return { status: 200, body: { worktrees: facts, ...truncated ? { truncated } : {} } };
136
+ }
137
+ /** Real fs-backed recursive force rm (Windows file-lock retries included). */
138
+ export async function fsRmRecursive(path) {
139
+ await rm(path, { recursive: true, force: true, maxRetries: 8, retryDelay: 100 });
140
+ }
141
+ /**
142
+ * POST /purge — delete a NON-git directory sitting DIRECTLY inside the
143
+ * resolved worktree storage root: the orphaned leftover a half-failed
144
+ * `worktree remove` can leave behind (git registration and the .git file
145
+ * already gone, contents stranded on disk). Triple-gated: one level below
146
+ * the root (resolved forms, same slot boundary as /ensure-directory), a
147
+ * real directory, and NOT a git repository — anything git still recognizes
148
+ * must go through /remove, where the removal carries the session-archive
149
+ * and unregistration half. Deletion is a plain recursive force rm.
150
+ * @param deps - host dependencies.
151
+ * @param body - parsed request body: `{ path }`.
152
+ */
153
+ export async function handlePurgeDirectory(deps, body) {
154
+ const parsed = readBody(body, ['path']);
155
+ if (isOutcome(parsed))
156
+ return parsed;
157
+ const { path } = parsed;
158
+ if (!isAbsoluteDir(path))
159
+ return fail(400, '"path" must be an absolute directory');
160
+ const configured = deps.sectionRootDir()?.trim();
161
+ if (configured !== undefined && configured !== '' && !isAbsoluteConfigPath(configured)) {
162
+ return fail(400, `configured rootDir "${String(deps.sectionRootDir())}" is not an absolute path`);
163
+ }
164
+ const rootDir = resolveRootDir(deps.sectionRootDir(), deps.home(), deps.envHome());
165
+ const canonical = resolve(path);
166
+ if (dirname(canonical) !== resolve(rootDir)) {
167
+ return fail(400, `"${canonical}" is outside the worktree storage root "${resolve(rootDir)}"`);
168
+ }
169
+ const statDirectory = deps.statDirectory ?? fsStatDirectory;
170
+ if (!(await statDirectory(canonical))) {
171
+ return fail(400, `"${canonical}" is not a directory`);
172
+ }
173
+ const gitFacts = await probeWorkspaceGit(deps.exec, canonical);
174
+ if (gitFacts !== undefined) {
175
+ return fail(400, `"${canonical}" is a git repository; remove it as a worktree instead`);
176
+ }
177
+ const rmRecursive = deps.rmRecursive ?? fsRmRecursive;
178
+ try {
179
+ await rmRecursive(canonical);
180
+ return { status: 200, body: { path: canonical, removed: true } };
181
+ }
182
+ catch (error) {
183
+ return fail(500, error instanceof Error ? error.message : String(error));
184
+ }
185
+ }
79
186
  /** GitError to outcome: usage-shaped failures are 400, the rest 500. */
80
187
  function gitFailure(error) {
81
188
  const usage = error.stderr.includes('usage:')
@@ -101,6 +208,11 @@ function gitFailure(error) {
101
208
  // texts (a remote that vanished mid-fetch: `Repository '...' not
102
209
  // found`) carry no quoted branch and stay 500 host faults.
103
210
  || error.stderr.includes('branch "')
211
+ // Deletion refusals from `git branch -d` are the user's repo state the
212
+ // same way: unmerged commits (the safe delete's whole point) and a
213
+ // branch checked out in some worktree.
214
+ || error.stderr.includes('is not fully merged')
215
+ || error.stderr.includes('Cannot delete branch')
104
216
  // Removal refusals from git.ts are caller-side state the same way: the
105
217
  // main worktree (git refuses it too, but the route answers a clean 400
106
218
  // before ever spawning git) and a path the repository never registered
@@ -127,6 +239,7 @@ export async function handleStatus(deps, path) {
127
239
  repo: true,
128
240
  repoName: facts.repoName,
129
241
  repoRoot: facts.repoRoot,
242
+ main: facts.main,
130
243
  currentBranch: facts.currentBranch,
131
244
  branches: facts.branches,
132
245
  worktrees: facts.worktrees,
@@ -193,6 +306,26 @@ export async function handleCreateWorktree(deps, body) {
193
306
  if (facts === undefined)
194
307
  return fail(400, `"${repoPath}" is not inside a git repository`);
195
308
  const rootDir = resolveRootDir(deps.sectionRootDir(), deps.home(), deps.envHome());
309
+ // Optional pre-create remote sync — but only when the branch actually
310
+ // consumes remote data: a LOCAL branch's worktree (or an existing twin)
311
+ // never reads the fetched refs, so the resolve runs first and the fetch
312
+ // fires just for the remote-only shape (the twin `worktree add -b`
313
+ // consumes it, the cutout's arbitrary base too). Deliberately NOT a
314
+ // gate: a network hiccup must not block the creation — the failure
315
+ // rides the successful response as `fetchWarning` for the client to
316
+ // toast.
317
+ let fetchWarning;
318
+ if (deps.sectionFetchBeforeCreate() === true) {
319
+ const resolved = await resolveBranch(deps.exec, facts.repoRoot, branch);
320
+ if (resolved?.remote !== undefined) {
321
+ try {
322
+ await fetchAll(deps.exec, facts.repoRoot);
323
+ }
324
+ catch (error) {
325
+ fetchWarning = error instanceof GitError ? error.stderr.trim() : error instanceof Error ? error.message : String(error);
326
+ }
327
+ }
328
+ }
196
329
  if (cutout === true) {
197
330
  // An explicit name skips the `-wt` suffix walk and is used verbatim —
198
331
  // both for the branch and the storage folder. A leading dash would
@@ -205,7 +338,7 @@ export async function handleCreateWorktree(deps, body) {
205
338
  const target = join(rootDir, `${facts.repoName}-${sanitizeBranchDir(custom)}`);
206
339
  await mkdir(rootDir, { recursive: true });
207
340
  await addWorktreeCutout(deps.exec, facts.repoRoot, branch, custom, target);
208
- return { status: 200, body: { path: target, created: true } };
341
+ return { status: 200, body: { path: target, created: true, ...fetchWarning === undefined ? {} : { fetchWarning } } };
209
342
  }
210
343
  // The new branch name must be known before the folder name can be
211
344
  // computed: the folder carries `<repoName>-<NEW branch>`. The name
@@ -218,7 +351,7 @@ export async function handleCreateWorktree(deps, body) {
218
351
  const target = join(rootDir, `${facts.repoName}-${sanitizeBranchDir(newBranch)}`);
219
352
  await mkdir(rootDir, { recursive: true });
220
353
  await addWorktreeCutout(deps.exec, facts.repoRoot, branch, newBranch, target);
221
- return { status: 200, body: { path: target, created: true } };
354
+ return { status: 200, body: { path: target, created: true, ...fetchWarning === undefined ? {} : { fetchWarning } } };
222
355
  }
223
356
  // The folder name carries the belonging itself: `<repoName>-<branch>` —
224
357
  // the sidebar group title (the folder basename) then reads as the parent
@@ -226,7 +359,7 @@ export async function handleCreateWorktree(deps, body) {
226
359
  const target = join(rootDir, `${facts.repoName}-${sanitizeBranchDir(branch)}`);
227
360
  await mkdir(rootDir, { recursive: true });
228
361
  const result = await addWorktree(deps.exec, facts.repoRoot, branch, target, deps.dirExists);
229
- return { status: 200, body: result };
362
+ return { status: 200, body: { ...result, ...fetchWarning === undefined ? {} : { fetchWarning } } };
230
363
  }
231
364
  catch (error) {
232
365
  if (error instanceof GitError)
@@ -263,18 +396,23 @@ export async function handleSwitch(deps, body) {
263
396
  }
264
397
  }
265
398
  /**
266
- * POST /branch 鈥?create a NEW branch from the queried directory's current
267
- * checkout (whatever its HEAD points at, detached included) and check it out
268
- * in place. Git validates the name; the client pre-flights the same rules
399
+ * POST /branch — create a NEW branch. Without `from`, from the queried
400
+ * directory's current checkout (whatever its HEAD points at, detached
401
+ * included), checked out in place. With `from` (a local branch or a remote
402
+ * tracking ref): checked out here too when `checkout` is set
403
+ * (`git switch -c <name> <from>`), created with no checkout touched
404
+ * otherwise (`git branch`) — a create aimed at another branch must not
405
+ * silently move the session's HEAD unless the caller asked for exactly
406
+ * that. Git validates both names; the client pre-flights the same rules
269
407
  * and only sends names it already accepts.
270
408
  * @param deps - host dependencies.
271
409
  * @param body - parsed request body.
272
410
  */
273
411
  export async function handleCreateBranch(deps, body) {
274
- const parsed = readBody(body, ['repoPath', 'name']);
412
+ const parsed = readBody(body, ['repoPath', 'name'], ['checkout'], ['from']);
275
413
  if (isOutcome(parsed))
276
414
  return parsed;
277
- const { repoPath, name } = parsed;
415
+ const { repoPath, name, from, checkout } = parsed;
278
416
  if (!isAbsoluteDir(repoPath))
279
417
  return fail(400, '"repoPath" must be an absolute directory');
280
418
  if (name.trim() === '')
@@ -283,11 +421,25 @@ export async function handleCreateBranch(deps, body) {
283
421
  // exec, not as an "unknown switch" GitError.
284
422
  if (name.startsWith('-'))
285
423
  return fail(400, '"name" must not start with "-"');
424
+ // The same flag-ride guard for the start point, which lands on
425
+ // `git branch <name> <from>` verbatim (a remote ref like `origin/x` is
426
+ // the caller's legitimate shape — passed through untouched).
427
+ if (from !== undefined) {
428
+ if (from.trim() === '')
429
+ return fail(400, '"from" must be non-empty when present');
430
+ if (from.startsWith('-'))
431
+ return fail(400, '"from" must not start with "-"');
432
+ }
286
433
  try {
287
434
  const facts = await probeRepo(deps.exec, repoPath, deps.dirExists);
288
435
  if (facts === undefined)
289
436
  return fail(400, `"${repoPath}" is not inside a git repository`);
290
- const created = await createBranch(deps.exec, facts.repoRoot, name);
437
+ // The session's OWN worktree: the no-`from` shape cuts from — and checks
438
+ // out in — the queried directory's HEAD, and `from` + `checkout` checks
439
+ // out HERE. `repoRoot` would move the main checkout instead of the
440
+ // worktree the session sits in (identical directory when it IS the main
441
+ // checkout).
442
+ const created = await createBranch(deps.exec, facts.toplevel, name, from, checkout === true);
291
443
  const result = { branch: created };
292
444
  return { status: 200, body: result };
293
445
  }
@@ -297,6 +449,79 @@ export async function handleCreateBranch(deps, body) {
297
449
  return fail(500, error instanceof Error ? error.message : String(error));
298
450
  }
299
451
  }
452
+ /**
453
+ * POST /branch-rename — rename a LOCAL branch (`git branch -m`). Runs at the
454
+ * repository root: branch refs are repository-wide and worktree HEADs
455
+ * pointing at the old name follow the ref. Git validates the new name and
456
+ * refuses a target that already exists; both map to 400 through
457
+ * {@link gitFailure}.
458
+ * @param deps - host dependencies.
459
+ * @param body - parsed request body.
460
+ */
461
+ export async function handleRenameBranch(deps, body) {
462
+ const parsed = readBody(body, ['repoPath', 'name', 'newName']);
463
+ if (isOutcome(parsed))
464
+ return parsed;
465
+ const { repoPath, name, newName } = parsed;
466
+ if (!isAbsoluteDir(repoPath))
467
+ return fail(400, '"repoPath" must be an absolute directory');
468
+ if (name.trim() === '')
469
+ return fail(400, '"name" must be non-empty');
470
+ if (newName.trim() === '')
471
+ return fail(400, '"newName" must be non-empty');
472
+ // Same flag-ride guard as the create route: the new name lands on
473
+ // `git branch -m <name> <newName>` verbatim.
474
+ if (newName.startsWith('-'))
475
+ return fail(400, '"newName" must not start with "-"');
476
+ try {
477
+ const facts = await probeRepo(deps.exec, repoPath, deps.dirExists);
478
+ if (facts === undefined)
479
+ return fail(400, `"${repoPath}" is not inside a git repository`);
480
+ const renamed = await renameBranch(deps.exec, facts.repoRoot, name, newName);
481
+ const result = { branch: renamed };
482
+ return { status: 200, body: result };
483
+ }
484
+ catch (error) {
485
+ if (error instanceof GitError)
486
+ return gitFailure(error);
487
+ return fail(500, error instanceof Error ? error.message : String(error));
488
+ }
489
+ }
490
+ /**
491
+ * POST /branch-delete — delete a LOCAL branch with the SAFE form
492
+ * (`git branch -d`): git refuses unmerged commits and branches checked out
493
+ * in any worktree, both mapping to 400 through {@link gitFailure}. No
494
+ * force variant — a menu click must never discard commits.
495
+ * @param deps - host dependencies.
496
+ * @param body - parsed request body.
497
+ */
498
+ export async function handleDeleteBranch(deps, body) {
499
+ const parsed = readBody(body, ['repoPath', 'name']);
500
+ if (isOutcome(parsed))
501
+ return parsed;
502
+ const { repoPath, name } = parsed;
503
+ if (!isAbsoluteDir(repoPath))
504
+ return fail(400, '"repoPath" must be an absolute directory');
505
+ if (name.trim() === '')
506
+ return fail(400, '"name" must be non-empty');
507
+ // Same flag-ride guard as its sibling routes: the name lands on
508
+ // `git branch -d <name>` verbatim.
509
+ if (name.startsWith('-'))
510
+ return fail(400, '"name" must not start with "-"');
511
+ try {
512
+ const facts = await probeRepo(deps.exec, repoPath, deps.dirExists);
513
+ if (facts === undefined)
514
+ return fail(400, `"${repoPath}" is not inside a git repository`);
515
+ const deleted = await deleteBranch(deps.exec, facts.repoRoot, name);
516
+ const result = { branch: deleted };
517
+ return { status: 200, body: result };
518
+ }
519
+ catch (error) {
520
+ if (error instanceof GitError)
521
+ return gitFailure(error);
522
+ return fail(500, error instanceof Error ? error.message : String(error));
523
+ }
524
+ }
300
525
  /**
301
526
  * POST /fetch 鈥?sync remote-tracking refs for the repository: fetch every
302
527
  * remote and prune tracking branches the remotes no longer carry. Pure
@@ -345,7 +570,11 @@ export async function handleUpdate(deps, body) {
345
570
  const facts = await probeRepo(deps.exec, repoPath, deps.dirExists);
346
571
  if (facts === undefined)
347
572
  return fail(400, `"${repoPath}" is not inside a git repository`);
348
- const outcome = await updateBranch(deps.exec, facts.repoRoot);
573
+ // The session's OWN worktree, not the main checkout: in a linked-worktree
574
+ // session "update current branch" means the branch that worktree holds.
575
+ // `repoRoot` would fast-forward the main checkout behind the user's back
576
+ // (both are the same directory when the session IS the main checkout).
577
+ const outcome = await updateBranch(deps.exec, facts.toplevel);
349
578
  const result = { branch: outcome.branch, updated: outcome.updated };
350
579
  return { status: 200, body: result };
351
580
  }
package/lib/wire.d.ts CHANGED
@@ -12,8 +12,15 @@ export declare const ROUTE_STATUS = "/plugin/git-worktree/status";
12
12
  export declare const ROUTE_WORKTREE = "/plugin/git-worktree/worktree";
13
13
  /** POST ROUTE_PREFIX/switch — in-place branch switch of the main checkout. */
14
14
  export declare const ROUTE_SWITCH = "/plugin/git-worktree/switch";
15
- /** POST ROUTE_PREFIX/branch — create a NEW branch from the current checkout and switch to it. */
15
+ /** POST ROUTE_PREFIX/branch — create a NEW branch (at the current checkout
16
+ * or an explicit start point, with or without checking it out). */
16
17
  export declare const ROUTE_BRANCH = "/plugin/git-worktree/branch";
18
+ /** POST ROUTE_PREFIX/branch-rename — rename a LOCAL branch (refs are
19
+ * repository-wide; worktree HEADs pointing at it follow). */
20
+ export declare const ROUTE_BRANCH_RENAME = "/plugin/git-worktree/branch-rename";
21
+ /** POST ROUTE_PREFIX/branch-delete — delete a LOCAL branch (`-d`, merged
22
+ * only; git refuses the rest). */
23
+ export declare const ROUTE_BRANCH_DELETE = "/plugin/git-worktree/branch-delete";
17
24
  /** POST ROUTE_PREFIX/fetch — sync remote-tracking refs (fetch every remote + prune). */
18
25
  export declare const ROUTE_FETCH = "/plugin/git-worktree/fetch";
19
26
  /** POST ROUTE_PREFIX/update — fast-forward the current branch to its upstream. */
@@ -28,6 +35,12 @@ export declare const ROUTE_REMOVE = "/plugin/git-worktree/remove";
28
35
  export declare const ROUTE_EXISTS = "/plugin/git-worktree/exists";
29
36
  /** POST ROUTE_PREFIX/ensure-directory — mkdir -p a missing worktree storage slot. */
30
37
  export declare const ROUTE_ENSURE_DIRECTORY = "/plugin/git-worktree/ensure-directory";
38
+ /** POST ROUTE_PREFIX/worktrees-all — scan the storage root: git facts for
39
+ * every direct child directory (the plugin-planned worktree slots). */
40
+ export declare const ROUTE_WORKTREES_ALL = "/plugin/git-worktree/worktrees-all";
41
+ /** POST ROUTE_PREFIX/purge — delete a NON-git directory sitting DIRECTLY in
42
+ * the storage root (an orphaned leftover whose .git is already gone). */
43
+ export declare const ROUTE_PURGE = "/plugin/git-worktree/purge";
31
44
  /** One selectable branch row. */
32
45
  export interface BranchEntry {
33
46
  /** Display name: a bare local name (`main`) or `<remote>/<name>`. */
@@ -57,6 +70,11 @@ export type RepoStatus = {
57
70
  repoName: string;
58
71
  /** Absolute path of the main worktree directory. */
59
72
  repoRoot: string;
73
+ /** True when the queried directory IS the main worktree (its own
74
+ * toplevel holds the shared `.git`). The client's "session sits in a
75
+ * linked worktree" trigger — `repoRoot` cannot answer it, since it
76
+ * names the main checkout from every worktree by construction. */
77
+ main: boolean;
60
78
  /** Branch checked out by the directory the client asked about. */
61
79
  currentBranch: string;
62
80
  /** Local branches plus every remote's remote-only branches (those
@@ -87,6 +105,11 @@ export interface CreateWorktreeResult {
87
105
  path: string;
88
106
  /** False when an existing worktree for the branch was reused. */
89
107
  created: boolean;
108
+ /** Present only when the pre-create remote sync (`fetchBeforeCreate`) was
109
+ * enabled AND failed: the creation went ahead anyway (a local worktree
110
+ * never consumes the fetch), this carries git's stderr summary for the
111
+ * client to toast. */
112
+ fetchWarning?: string;
90
113
  }
91
114
  /** POST switch request body. */
92
115
  export interface SwitchBody {
@@ -102,15 +125,52 @@ export interface SwitchResult {
102
125
  }
103
126
  /** POST branch request body. */
104
127
  export interface CreateBranchBody {
105
- /** Any directory inside the repository (workspace cwd) — the new branch is
106
- * cut from whatever this directory's HEAD points at and checked out HERE. */
128
+ /** Any directory inside the repository (workspace cwd). Without `from` the
129
+ * new branch is cut from whatever this directory's HEAD points at and
130
+ * checked out HERE. */
107
131
  repoPath: string;
108
132
  /** User-typed name of the branch to create (a local name, verbatim). */
109
133
  name: string;
134
+ /** Optional start point (a local branch or a remote-tracking ref like
135
+ * `origin/feat-x`). Absent keeps the original in-place semantics: create
136
+ * AND check out here. */
137
+ from?: string;
138
+ /** With `from`, ALSO check the new branch out in the queried directory
139
+ * (`git switch -c <name> <from>`) instead of leaving every checkout
140
+ * untouched (`git branch <name> <from>`). Ignored without `from` —
141
+ * the no-`from` shape always checks out. */
142
+ checkout?: boolean;
110
143
  }
111
144
  /** POST branch response body. */
112
145
  export interface CreateBranchResult {
113
- /** The branch created and now checked out. */
146
+ /** The branch created; checked out in the queried directory unless
147
+ * created at a start point with `checkout` left false. */
148
+ branch: string;
149
+ }
150
+ /** POST branch-rename request body. */
151
+ export interface RenameBranchBody {
152
+ /** Any directory inside the repository (branch refs are repository-wide). */
153
+ repoPath: string;
154
+ /** The branch to rename (a LOCAL name; remote branches are not ours). */
155
+ name: string;
156
+ /** User-typed new name (a local name, verbatim). */
157
+ newName: string;
158
+ }
159
+ /** POST branch-rename response body. */
160
+ export interface RenameBranchResult {
161
+ /** The branch's new name. */
162
+ branch: string;
163
+ }
164
+ /** POST branch-delete request body. */
165
+ export interface DeleteBranchBody {
166
+ /** Any directory inside the repository (branch refs are repository-wide). */
167
+ repoPath: string;
168
+ /** The branch to delete (a LOCAL name; remote branches are not ours). */
169
+ name: string;
170
+ }
171
+ /** POST branch-delete response body. */
172
+ export interface DeleteBranchResult {
173
+ /** The branch that was deleted. */
114
174
  branch: string;
115
175
  }
116
176
  /** POST fetch request body. */
@@ -224,3 +284,36 @@ export interface EnsureDirectoryResult {
224
284
  * present as a directory). */
225
285
  created: boolean;
226
286
  }
287
+ /** One direct child directory of the worktree storage root. */
288
+ export interface WorktreeScanEntry {
289
+ /** Absolute directory path. */
290
+ path: string;
291
+ /** Main repository directory basename; null = the directory is not inside
292
+ * a git repository (an orphan or foreign folder — shown as unrecognized,
293
+ * never deleted, never counted against the prune cap). */
294
+ repoName: string | null;
295
+ /** Branch checked out by the directory; null = unrecognized, detached, or
296
+ * unborn HEAD. */
297
+ branch: string | null;
298
+ }
299
+ /** POST worktrees-all response body — one entry per DIRECT child of the
300
+ * resolved storage root; the root's absence answers an empty list. */
301
+ export interface WorktreesAllResult {
302
+ worktrees: WorktreeScanEntry[];
303
+ /** Present and true when the storage root held more children than the
304
+ * scan probes: the list is the first slice, and the client says so rather
305
+ * than presenting a truncated scan as the whole picture. */
306
+ truncated?: boolean;
307
+ }
308
+ /** POST purge request body. */
309
+ export interface PurgeDirectoryBody {
310
+ /** The leftover directory to delete (absolute; must sit directly inside
311
+ * the worktree storage root AND have no git identity). */
312
+ path: string;
313
+ }
314
+ /** POST purge response body. */
315
+ export interface PurgeDirectoryResult {
316
+ /** Always true on 200 — the directory (contents included) is gone. */
317
+ path: string;
318
+ removed: boolean;
319
+ }
package/lib/wire.js CHANGED
@@ -12,8 +12,15 @@ export const ROUTE_STATUS = `${ROUTE_PREFIX}/status`;
12
12
  export const ROUTE_WORKTREE = `${ROUTE_PREFIX}/worktree`;
13
13
  /** POST ROUTE_PREFIX/switch — in-place branch switch of the main checkout. */
14
14
  export const ROUTE_SWITCH = `${ROUTE_PREFIX}/switch`;
15
- /** POST ROUTE_PREFIX/branch — create a NEW branch from the current checkout and switch to it. */
15
+ /** POST ROUTE_PREFIX/branch — create a NEW branch (at the current checkout
16
+ * or an explicit start point, with or without checking it out). */
16
17
  export const ROUTE_BRANCH = `${ROUTE_PREFIX}/branch`;
18
+ /** POST ROUTE_PREFIX/branch-rename — rename a LOCAL branch (refs are
19
+ * repository-wide; worktree HEADs pointing at it follow). */
20
+ export const ROUTE_BRANCH_RENAME = `${ROUTE_PREFIX}/branch-rename`;
21
+ /** POST ROUTE_PREFIX/branch-delete — delete a LOCAL branch (`-d`, merged
22
+ * only; git refuses the rest). */
23
+ export const ROUTE_BRANCH_DELETE = `${ROUTE_PREFIX}/branch-delete`;
17
24
  /** POST ROUTE_PREFIX/fetch — sync remote-tracking refs (fetch every remote + prune). */
18
25
  export const ROUTE_FETCH = `${ROUTE_PREFIX}/fetch`;
19
26
  /** POST ROUTE_PREFIX/update — fast-forward the current branch to its upstream. */
@@ -28,3 +35,9 @@ export const ROUTE_REMOVE = `${ROUTE_PREFIX}/remove`;
28
35
  export const ROUTE_EXISTS = `${ROUTE_PREFIX}/exists`;
29
36
  /** POST ROUTE_PREFIX/ensure-directory — mkdir -p a missing worktree storage slot. */
30
37
  export const ROUTE_ENSURE_DIRECTORY = `${ROUTE_PREFIX}/ensure-directory`;
38
+ /** POST ROUTE_PREFIX/worktrees-all — scan the storage root: git facts for
39
+ * every direct child directory (the plugin-planned worktree slots). */
40
+ export const ROUTE_WORKTREES_ALL = `${ROUTE_PREFIX}/worktrees-all`;
41
+ /** POST ROUTE_PREFIX/purge — delete a NON-git directory sitting DIRECTLY in
42
+ * the storage root (an orphaned leftover whose .git is already gone). */
43
+ export const ROUTE_PURGE = `${ROUTE_PREFIX}/purge`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@laoyuehanni/dsh-git-worktree",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "description": "DSH plugin: branch visibility and git worktree isolation for Web UI sessions",
5
5
  "keywords": [
6
6
  "dsh",
@@ -40,6 +40,7 @@
40
40
  },
41
41
  "files": [
42
42
  "lib",
43
+ "!lib/**/*.map",
43
44
  "cordis.patch.yml",
44
45
  "README.md",
45
46
  "README.zh.md"
@@ -55,11 +56,11 @@
55
56
  "client": {
56
57
  "platform": "web",
57
58
  "inject": [
58
- "@deepseek-ai/dsh-client-runtime",
59
59
  "@deepseek-ai/dsh-client-ui-conversation",
60
60
  "@deepseek-ai/dsh-client-ui-sidebar",
61
61
  "@deepseek-ai/dsh-client-ui-settings",
62
- "@deepseek-ai/dsh-client-ui-settings-plugins"
62
+ "@deepseek-ai/dsh-client-ui-settings-plugins",
63
+ "@deepseek-ai/dsh-client-ui-workspace"
63
64
  ]
64
65
  }
65
66
  },
@@ -74,27 +75,40 @@
74
75
  },
75
76
  "peerDependencies": {
76
77
  "@deepseek-ai/cordis": "^4.0.1",
77
- "@deepseek-ai/dsh-host-webserver": "^0.1.0-rc.7",
78
- "@deepseek-ai/dsh-settings": "^0.1.0-rc.7 || ^0.1.1-rc.2",
78
+ "@deepseek-ai/dsh-host-webserver": "^0.1.2-rc.1",
79
+ "@deepseek-ai/dsh-settings": "^0.1.2-rc.1",
79
80
  "@deepseek-ai/schemastery": "^3.18.1"
80
81
  },
81
82
  "devDependencies": {
82
- "@deepseek-ai/cordis": "^4.0.1",
83
- "@deepseek-ai/dsh-api-remotes": "^0.1.0-rc.7",
84
- "@deepseek-ai/dsh-client-locale": "^0.1.0-rc.7",
85
- "@deepseek-ai/dsh-client-runtime": "^0.1.0-rc.7",
86
- "@deepseek-ai/dsh-client-ui-conversation": "^0.1.0-rc.7",
87
- "@deepseek-ai/dsh-client-ui-primitives": "^0.1.0-rc.7",
88
- "@deepseek-ai/dsh-client-ui-settings": "^0.1.0-rc.7",
89
- "@deepseek-ai/dsh-client-ui-settings-plugins": "^0.1.0-rc.7",
90
- "@deepseek-ai/dsh-client-ui-sidebar": "^0.1.0-rc.7",
91
- "@deepseek-ai/dsh-client-ui-slots": "^0.1.0-rc.7",
92
- "@deepseek-ai/dsh-host-webserver": "^0.1.0-rc.7",
83
+ "@deepseek-ai/cordis": "^4.0.2",
84
+ "@deepseek-ai/dsh-api-gateway": "^0.1.2-rc.1",
85
+ "@deepseek-ai/dsh-api-remotes": "^0.1.2-rc.1",
86
+ "@deepseek-ai/dsh-api-session-controller": "^0.1.2-rc.1",
87
+ "@deepseek-ai/dsh-api-workspace-controller": "^0.1.2-rc.1",
88
+ "@deepseek-ai/dsh-client-locale": "^0.1.2-rc.1",
89
+ "@deepseek-ai/dsh-client-connection": "^0.1.2-rc.1",
90
+ "@deepseek-ai/dsh-client-store": "^0.1.2-rc.1",
91
+ "@deepseek-ai/dsh-client-ui-conversation": "^0.1.2-rc.1",
92
+ "@deepseek-ai/dsh-client-ui-primitives": "^0.1.2-rc.1",
93
+ "@deepseek-ai/dsh-client-ui-renderer": "^0.1.2-rc.1",
94
+ "@deepseek-ai/dsh-client-ui-settings": "^0.1.2-rc.1",
95
+ "@deepseek-ai/dsh-client-ui-settings-plugins": "^0.1.2-rc.1",
96
+ "@deepseek-ai/dsh-client-ui-session": "^0.1.2-rc.1",
97
+ "@deepseek-ai/dsh-client-ui-sidebar": "^0.1.2-rc.1",
98
+ "@deepseek-ai/dsh-client-ui-slots": "^0.1.2-rc.1",
99
+ "@deepseek-ai/dsh-client-ui-workspace": "^0.1.2-rc.1",
100
+ "@deepseek-ai/dsh-host-webserver": "^0.1.2-rc.1",
101
+ "@deepseek-ai/dsh-session": "^0.1.2-rc.1",
102
+ "@deepseek-ai/dsh-settings": "^0.1.2-rc.1",
103
+ "@deepseek-ai/dsh-util-workspace-path": "^0.1.2-rc.1",
104
+ "@deepseek-ai/dsh-workspace": "^0.1.2-rc.1",
105
+ "@deepseek-ai/schemastery": "^3.18.1",
93
106
  "@types/node": "^24.13.3",
94
107
  "@types/react": "~18.3.1",
95
108
  "@types/react-dom": "~18.3.1",
96
109
  "playwright-core": "^1.55.1",
97
110
  "react": "^18.2.0",
111
+ "react-dom": "^18.2.0",
98
112
  "tsdown": "^0.14.0",
99
113
  "typescript": "^5.9.3",
100
114
  "vitest": "^3.2.4"