@narumitw/pi-worktree 0.25.1 → 0.27.1

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.
Files changed (3) hide show
  1. package/README.md +7 -4
  2. package/package.json +1 -1
  3. package/src/command.ts +111 -33
package/README.md CHANGED
@@ -14,8 +14,9 @@ Pi cannot change its parent process working directory with `cd`. This extension
14
14
  - Suggests a sibling path such as `/workspace/project-feat-login` for `feat/login`.
15
15
  - Optionally switches Pi into a newly created worktree while continuing the current conversation.
16
16
  - Switches among existing registered worktrees through Pi's public session replacement API.
17
- - Removes only clean, unlocked, non-current linked worktrees and preserves their branches.
18
- - Refuses removal when tracked, untracked, ignored, manually index-flagged, submodule, or current unreachable detached-commit data may be lost.
17
+ - Removes unlocked, non-current linked worktrees and preserves their branches.
18
+ - Refuses removal when tracked, untracked, manually index-flagged, submodule, or current unreachable detached-commit data may be lost.
19
+ - Allows ignored-only data such as `node_modules/` after listing it in the destructive confirmation.
19
20
  - Names recovery-only administrative commits in the destructive confirmation instead of making ordinary rebase/reset history block cleanup forever.
20
21
  - Always previews stale metadata before pruning it and revalidates the preview after confirmation.
21
22
  - Runs Git through argv-based subprocess calls, without interpolating user input into shell commands.
@@ -51,7 +52,7 @@ Choose one action:
51
52
 
52
53
  - **Add worktree** — enter a branch, optional start point, and optional path; confirm creation and optionally switch.
53
54
  - **Switch worktree** — select another existing worktree and continue this Pi conversation there.
54
- - **Remove worktree** — remove a confirmed clean linked worktree without deleting its branch.
55
+ - **Remove worktree** — remove a linked worktree without deleting its branch; ignored-only data is listed for explicit confirmation.
55
56
  - **Prune stale metadata** — inspect Git's dry-run output, then optionally run the matching prune.
56
57
 
57
58
  `/worktree` intentionally does not accept text subcommands in this version. Every mutation is initiated and confirmed through the interactive UI.
@@ -89,7 +90,8 @@ A successfully created Git worktree is never rolled back merely because Pi sessi
89
90
 
90
91
  - The main worktree and current worktree cannot be removed.
91
92
  - Locked or stale worktrees cannot be removed through this extension.
92
- - Dirty, untracked, ignored, initialized-submodule, and intentional `assume-unchanged`/`skip-worktree` index state causes removal to fail closed. Sparse-checkout-managed `skip-worktree` entries outside the active sparsity rules are allowed when Git's rule checker can confirm them; clear other intentional index flags before removing the worktree.
93
+ - Dirty, untracked, initialized-submodule, and intentional `assume-unchanged`/`skip-worktree` index state causes removal to fail closed. Sparse-checkout-managed `skip-worktree` entries outside the active sparsity rules are allowed when Git's rule checker can confirm them; clear other intentional index flags before removing the worktree.
94
+ - Ignored-only files and directories do not block removal. The confirmation lists them, and the extension rechecks the exact ignored inventory before Git deletes the worktree.
93
95
  - A detached HEAD must be reachable from a local branch, tag, or remote ref before removal or prune.
94
96
  - Removal and prune inspect reflogs, pseudorefs, per-worktree refs, and `FETCH_HEAD`. Historical commits reachable only through this administrative recovery state are listed by full OID in the destructive confirmation; approval removes those recovery pointers, so Git may later garbage-collect the commits. Create a branch or tag instead when any listed commit should survive.
95
97
  - Staged-only administrative index state, a missing attached branch ref, or an unreachable current detached HEAD still blocks prune without an override.
@@ -119,6 +121,7 @@ extensions/pi-worktree/
119
121
  │ ├── command.test.ts
120
122
  │ ├── git.integration.test.ts
121
123
  │ ├── git.test.ts
124
+ │ ├── remove-ignored-command.test.ts
122
125
  │ └── session.test.ts
123
126
  ├── package.json
124
127
  ├── README.md
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-worktree",
3
- "version": "0.25.1",
3
+ "version": "0.27.1",
4
4
  "description": "Pi extension for safe interactive Git worktree management and workspace switching.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/command.ts CHANGED
@@ -232,12 +232,7 @@ async function removeFlow(
232
232
  const candidates = records.filter(
233
233
  (record) => !record.isMain && !record.bare && !pathsEqual(record.path, currentPath),
234
234
  );
235
- const selected = await selectWorktree(
236
- ctx,
237
- "Remove clean linked worktree",
238
- candidates,
239
- currentPath,
240
- );
235
+ const selected = await selectWorktree(ctx, "Remove linked worktree", candidates, currentPath);
241
236
  if (!selected) return;
242
237
  if (selected.lockedReason !== undefined) {
243
238
  throw new Error(
@@ -248,10 +243,12 @@ async function removeFlow(
248
243
  throw new Error("The selected worktree path is stale. Use prune instead of remove.");
249
244
  }
250
245
 
251
- const inventory = await worktreeInventory(pi, selected.path, ctx.signal);
252
- if (inventory.length > 0) {
246
+ const inventory = classifyRemovalInventory(
247
+ await worktreeInventory(pi, selected.path, ctx.signal),
248
+ );
249
+ if (inventory.protected.length > 0) {
253
250
  throw new Error(
254
- `Removal refused because ${selected.path} contains tracked, untracked, ignored, or submodule data:\n${inventory.join("\n")}`,
251
+ `Removal refused because ${selected.path} contains tracked, untracked, index-flagged, or submodule data:\n${inventory.protected.join("\n")}`,
255
252
  );
256
253
  }
257
254
  await assertDetachedHeadIsDurable(pi, ctx, selected);
@@ -261,17 +258,36 @@ async function removeFlow(
261
258
  await unreachableAdministrativeHistoryOids(pi, ctx, administrativePath),
262
259
  );
263
260
  const recoveryWarning = formatAdministrativeRecoveryWarning(approvedHistoryRisks);
261
+ const ignoredWarning = formatIgnoredDataWarning(inventory.ignored);
262
+ const removalWarning =
263
+ ignoredWarning && recoveryWarning
264
+ ? `${ignoredWarning}\n${recoveryWarning.trimStart()}`
265
+ : `${ignoredWarning}${recoveryWarning}`;
266
+ const confirmationTitle =
267
+ inventory.ignored.length > 0
268
+ ? recoveryWarning
269
+ ? "Remove worktree and discard local/recovery data"
270
+ : "Remove worktree and delete ignored files"
271
+ : recoveryWarning
272
+ ? "Remove worktree and discard recovery history"
273
+ : "Remove Git worktree";
264
274
  if (
265
275
  !(await ctx.ui.confirm(
266
- recoveryWarning ? "Remove worktree and discard recovery history" : "Remove Git worktree",
267
- stripTerminalControls(
268
- `Delete the clean worktree directory ${selected.path}? The branch will be preserved.${recoveryWarning}`,
269
- ),
276
+ confirmationTitle,
277
+ `Delete the worktree directory ${stripTerminalControls(selected.path)}? The branch will be preserved.${removalWarning}`,
270
278
  ))
271
279
  ) {
272
280
  return;
273
281
  }
274
282
 
283
+ await assertAdministrativeHistoryUnchanged(
284
+ pi,
285
+ ctx,
286
+ selected.path,
287
+ administrativePath,
288
+ approvedHistoryRisks,
289
+ );
290
+
275
291
  const beforeRemoval = await listWorktrees(pi, ctx.cwd, ctx.signal);
276
292
  const latest = beforeRemoval.find((record) => pathsEqual(record.path, selected.path));
277
293
  if (!latest) throw new Error(`Worktree ${selected.path} is no longer registered.`);
@@ -283,30 +299,27 @@ async function removeFlow(
283
299
  `Worktree ${selected.path} changed state after confirmation; removal was refused.`,
284
300
  );
285
301
  }
286
- const latestInventory = await worktreeInventory(pi, latest.path, ctx.signal);
287
- if (latestInventory.length > 0) {
302
+ const latestInventory = classifyRemovalInventory(
303
+ await worktreeInventory(pi, latest.path, ctx.signal),
304
+ );
305
+ if (latestInventory.protected.length > 0) {
288
306
  throw new Error(
289
- `Removal refused because new local data appeared after confirmation:\n${latestInventory.join("\n")}`,
307
+ `Removal refused because new protected local data appeared after confirmation:\n${latestInventory.protected.join("\n")}`,
308
+ );
309
+ }
310
+ if (!sameInventory(inventory.ignored, latestInventory.ignored)) {
311
+ throw new Error(
312
+ `Removal refused because ignored data changed after confirmation:\n${latestInventory.ignored.join("\n") || "(none)"}`,
290
313
  );
291
314
  }
292
315
  await assertDetachedHeadIsDurable(pi, ctx, latest);
293
- const latestAdministrativePath = await worktreeAdministrativeDirectory(
316
+ await assertAdministrativeHistoryUnchanged(
294
317
  pi,
318
+ ctx,
295
319
  latest.path,
296
- ctx.signal,
297
- );
298
- const latestHistoryRisks = historyRisks(
299
- latest.path,
300
- await unreachableAdministrativeHistoryOids(pi, ctx, latestAdministrativePath),
320
+ administrativePath,
321
+ approvedHistoryRisks,
301
322
  );
302
- if (
303
- !pathsEqual(administrativePath, latestAdministrativePath) ||
304
- !sameAdministrativeHistoryRisks(approvedHistoryRisks, latestHistoryRisks)
305
- ) {
306
- throw new Error(
307
- `Worktree ${selected.path} administrative recovery history changed after confirmation; select it again.`,
308
- );
309
- }
310
323
  await removeWorktree(pi, ctx.cwd, latest.path, ctx.signal);
311
324
  const updated = await listWorktrees(pi, ctx.cwd, ctx.signal);
312
325
  if (updated.some((record) => pathsEqual(record.path, selected.path))) {
@@ -350,11 +363,15 @@ async function pruneFlow(
350
363
  )) {
351
364
  await assertDetachedHeadIsDurable(pi, ctx, record);
352
365
  }
366
+ const beforePreviewHistoryRisks = await inspectAdministrativePruneCandidates(pi, ctx);
367
+ if (!sameAdministrativeHistoryRisks(approvedHistoryRisks, beforePreviewHistoryRisks)) {
368
+ throw new Error("Stale worktree metadata changed after confirmation; run prune again.");
369
+ }
353
370
  const latestPreview = await prunePreview(pi, ctx.cwd, ctx.signal);
354
- const latestHistoryRisks = await inspectAdministrativePruneCandidates(pi, ctx);
371
+ const finalHistoryRisks = await inspectAdministrativePruneCandidates(pi, ctx);
355
372
  if (
356
373
  latestPreview !== preview ||
357
- !sameAdministrativeHistoryRisks(approvedHistoryRisks, latestHistoryRisks)
374
+ !sameAdministrativeHistoryRisks(approvedHistoryRisks, finalHistoryRisks)
358
375
  ) {
359
376
  throw new Error("Stale worktree metadata changed after confirmation; run prune again.");
360
377
  }
@@ -366,6 +383,32 @@ async function pruneFlow(
366
383
  );
367
384
  }
368
385
 
386
+ async function assertAdministrativeHistoryUnchanged(
387
+ pi: ExtensionAPI,
388
+ ctx: ExtensionCommandContext,
389
+ selectedPath: string,
390
+ approvedAdministrativePath: string,
391
+ approvedHistoryRisks: readonly AdministrativeHistoryRisk[],
392
+ ): Promise<void> {
393
+ const latestAdministrativePath = await worktreeAdministrativeDirectory(
394
+ pi,
395
+ selectedPath,
396
+ ctx.signal,
397
+ );
398
+ const latestHistoryRisks = historyRisks(
399
+ selectedPath,
400
+ await unreachableAdministrativeHistoryOids(pi, ctx, latestAdministrativePath),
401
+ );
402
+ if (
403
+ !pathsEqual(approvedAdministrativePath, latestAdministrativePath) ||
404
+ !sameAdministrativeHistoryRisks(approvedHistoryRisks, latestHistoryRisks)
405
+ ) {
406
+ throw new Error(
407
+ `Worktree ${selectedPath} administrative recovery history changed after confirmation; select it again.`,
408
+ );
409
+ }
410
+ }
411
+
369
412
  async function inspectAdministrativePruneCandidates(
370
413
  pi: ExtensionAPI,
371
414
  ctx: ExtensionCommandContext,
@@ -440,10 +483,45 @@ function sameAdministrativeHistoryRisks(
440
483
 
441
484
  function formatAdministrativeRecoveryWarning(risks: readonly AdministrativeHistoryRisk[]): string {
442
485
  if (risks.length === 0) return "";
443
- const entries = risks.map((risk) => `${risk.label}: ${risk.oids.join(", ")}`).join("; ");
486
+ const entries = risks
487
+ .map(
488
+ (risk) =>
489
+ `${stripTerminalControls(risk.label)}: ${risk.oids.map(stripTerminalControls).join(", ")}`,
490
+ )
491
+ .join("; ");
444
492
  return ` Administrative recovery warning: these commits are not reachable from a branch, tag, or remote ref: ${entries}. Discarding their recovery pointers means they may later be garbage-collected.`;
445
493
  }
446
494
 
495
+ interface RemovalInventory {
496
+ ignored: string[];
497
+ protected: string[];
498
+ }
499
+
500
+ function classifyRemovalInventory(lines: readonly string[]): RemovalInventory {
501
+ const ignored: string[] = [];
502
+ const protectedData: string[] = [];
503
+ for (const line of lines) {
504
+ (line.startsWith("!! ") ? ignored : protectedData).push(line);
505
+ }
506
+ return {
507
+ ignored: normalizeInventory(ignored),
508
+ protected: normalizeInventory(protectedData),
509
+ };
510
+ }
511
+
512
+ function normalizeInventory(lines: readonly string[]): string[] {
513
+ return [...new Set(lines)].sort();
514
+ }
515
+
516
+ function sameInventory(left: readonly string[], right: readonly string[]): boolean {
517
+ return JSON.stringify(normalizeInventory(left)) === JSON.stringify(normalizeInventory(right));
518
+ }
519
+
520
+ function formatIgnoredDataWarning(ignored: readonly string[]): string {
521
+ if (ignored.length === 0) return "";
522
+ return ` Ignored files and directories that will be deleted:\n${ignored.map(stripTerminalControls).join("\n")}`;
523
+ }
524
+
447
525
  async function assertDetachedHeadIsDurable(
448
526
  pi: ExtensionAPI,
449
527
  ctx: ExtensionCommandContext,